"Fn :: Join"和参数中的分隔符?

Amo*_*ira 5 amazon-web-services aws-cloudformation

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

"Name" : { "Fn::Join" : [ ".", [ 
           { "Ref":"serviceName"}, { "Ref": "environment" } ] ] },
Run Code Online (Sandbox Code Playgroud)

哪个效果很好,但后来我改为:

"Name" : { "Fn::Join" : [ {"Ref":"HostNameSeparator"}, [
           { "Ref":"serviceName"}, { "Ref": "environment" } ] ] },
Run Code Online (Sandbox Code Playgroud)

我从验证阶段得到以下错误:

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.
Run Code Online (Sandbox Code Playgroud)

是否可以做我想要的,即将Join delimiter作为模板参数传递?

(为了清楚起见,我已经缩短了上面的例子,请忽略错别字)

pol*_*ene 2

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

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