gpt4 book ai didi

amazon-web-services - 内联 Lambda CFT "Fn::Join"错误 - JSON

转载 作者:行者123 更新时间:2023-12-03 07:22:30 24 4
gpt4 key购买 nike

我正在编写一个 JSON 格式的云形成模板。该模板具有一个类型为 FSx 文件系统 ID 的 CommaDelimitedList 的参数和一个 Lambda 函数资源,该资源引用 FSx 文件系统 ID 的参数并检查其状态。我正在模板中内联编写此 Lambda 函数。

我收到以下错误:

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.

这是我的模板:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"FSIds": {
"Type": "CommaDelimitedList",
"Description": "Enter the file system IDs separated by commas"
}
},
"Resources": {
"FSxStatusLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "test-FSx",
"Architectures": [ "x86_64" ],
"Description": "Lambda function that checks FSx file systems status and sends notification",
"Handler": "index.handler",
"Code": {
"ZipFile": {
"Fn::Join": [
"\n",
[
"import boto3",
"fsx = boto3.client('fsx')",
"#Publish message to SNS topic",
"def publish_message(FS_ID , status):",
" sns = boto3.client('sns')",
" topic = \"arn:aws:sns:us-east-1:123456789:FSx-Notification\"",
" msg = \"{FS_ID} is in {status} state\".format(FS_ID=FS_ID , status=status)",
" response = sns.publish(",
" TopicArn = topic,",
" Message = msg",
" )",
"#Lambda handler function",
"def lambda_handler(event, context):",
" res = fsx.describe_file_systems(",
" FileSystemIds = ",
{
"Ref": "FSIds"
},
" )",
" for i in range(len(res[\"FileSystems\"])):",
" FS_ID = res[\"FileSystems\"][i][\"FileSystemId\"]",
" status = res[\"FileSystems\"][i][\"Lifecycle\"]",
" if status == \"MISCONFIGURED\" or status == \"FAILED\":",
" publish_message(FS_ID , status)",
]
]
}
},
"Role": "arn:aws:iam::123456789:role/FSx-LambdaRole",
"Runtime": "python3.9",
"Timeout": "60",
"Tags": [
{
"Key": "Owner",
"Value": "God"
}
]
}
}
}
}

在创建堆栈时,我将参数的输入指定为:fs-123,fs-456

请帮忙!!

最佳答案

该错误是由参数的 CommaDelimitedList 类型间接引起的。它导致 FSIds 成为列表/数组而不是字符串,这意味着 Fn::Join 字符串列表中的此项不是字符串:

    {
"Ref": "FSIds"
},

如果您将参数类型更改为String,它将为您创建堆栈。

或者,您可以保留 CommaDelimitedList 并将上面的内容更改为:

    {
"Fn::Join": [ ",", { "Ref": "FSIds" } ]
},

其中任何一个至少都会让您克服当前遇到的错误。

更新:

正如您所指出的,FileSystemIds 实际上需要是一个列表,因此您有几个选项:

  1. 更改 CF 模板,虽然很痛苦,但可以做到。像这样的东西(未经测试):
                {
"Fn::Join": [
"",
[
" FileSystemIds = [\"",
{
"Fn::Join": [
"\", \"",
{
"Ref": "FSIds"
}
]
},
"\""
]
]
},
  • 类似,但让 Python 使用 str.split() 完成一些工作,例如像这样(请注意,这两者都假设 FSIds 仍然是 CommaDelimitedList):
  •                   {
    "Fn::Join": [
    "",
    [
    " FileSystemIds = str.split(\"",
    { "Fn::Join": [ ",", { "Ref": "FSIds" } ] },
    "\", \",\")"
    ]
    },
    },
  • 以不同的方式传递参数,不涉及通过 CF 编写精确的 Python 代码,例如 Lambda 的环境变量:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-environment.html
  • 关于amazon-web-services - 内联 Lambda CFT "Fn::Join"错误 - JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73251898/

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