gpt4 book ai didi

amazon-web-services - "Fn::Join"带有参数分隔符?

转载 作者:行者123 更新时间:2023-12-01 16:30:32 25 4
gpt4 key购买 nike

我正在尝试参数化 Fn::Join 使用的分隔符,例如起初我有:

"Name" : { "Fn::Join" : [ ".", [ 
{ "Ref":"serviceName"}, { "Ref": "environment" } ] ] },

效果很好,但后来我将其更改为:

"Name" : { "Fn::Join" : [ {"Ref":"HostNameSeparator"}, [
{ "Ref":"serviceName"}, { "Ref": "environment" } ] ] },

我在验证阶段收到以下错误:

A client error (ValidationError) occurred when calling the
ValidateTemplate operation: Template error: every Fn::Join object
requires two parameters, (1) a string delimiter and (2) a list of
strings to be joined or a function that returns a list of strings
(such as Fn::GetAZs) to be joined.

是否可以执行我想要的操作,即将连接分隔符作为模板参数传递?

(为了清楚起见,我缩短了上面的示例,请忽略拼写错误)

最佳答案

您可以通过使用 CustomResource 来实现此目的。此 CloudFormation 模板已准备好运行,并说明了它的工作原理。分隔符作为参数传递,您将在堆栈的输出中找到连接的字符串。

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Example Join",
"Parameters": {
"Delimiter": {
"Type": "String"
}
},
"Resources": {
"LambdaExecutionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": ["lambda.amazonaws.com"]
},
"Action": ["sts:AssumeRole"]
}
]
},
"Path": "/",
"Policies": [
{
"PolicyName": "root",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"cloudformation:DescribeStacks"
],
"Resource": "*"
}
]
}
}
]
}
},
"LambdaJoin": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": { "Fn::Join": ["\n", [
"var response = require('cfn-response');\n",
"exports.handler = function (event, context) {\n",
"if (event.RequestType === 'Delete') {\n",
"return response.send(event, context, response.SUCCESS, {}, event.PhysicalResourceId);\n",
"}\n",
"var delimiter = event.ResourceProperties.delimiter || '';\n",
"var strings = event.ResourceProperties.strings || [];\n",
"return response.send(event, context, response.SUCCESS, { string: strings.join(delimiter) }, event.PhysicalResourceId);\n",
"};\n"
]]}
},
"Handler": "index.handler",
"Runtime": "nodejs",
"Timeout": "10",
"Role": { "Fn::GetAtt" : ["LambdaExecutionRole", "Arn"] }
}
},
"CustomJoin": {
"Type": "Custom::Join",
"Version": "1.0",
"Properties": {
"ServiceToken": { "Fn::GetAtt": ["LambdaJoin", "Arn"] },
"delimiter": { "Ref": "Delimiter" },
"strings": ["first", "second", "third"]
},
"DependsOn": ["LambdaJoin"]
}
},
"Outputs": {
"JoinedString": {
"Value": { "Fn::GetAtt": ["CustomJoin", "string"] }
}
}
}

关于amazon-web-services - "Fn::Join"带有参数分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29666817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com