amazon-web-services - 在内联 python 中使用 cfnresponse 创建自定义支持的云形成资源时出错

标签 amazon-web-services aws-cloudformation

尝试创建内联 python lambda 函数作为自定义支持的 cloudformation 资源..使用 cfnresponse 来获取资源创建的信息..但我的堆栈正在回滚并出现错误。

错误:CustomResource 属性错误:供应商响应在对象 arn:aws:cloudformation:us-west-2:stack/Custom-lambda/8eaeead0-68b8-11e9-8e31-0247c451c136|CustomResource|3a7885fc 中不包含 Message key -0959-4284-b4f6-8153fe6420df 在 S3 存储桶 cloudformation-custom-resource-storage-uswest2 中。用户请求回滚。

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "AWS CloudFormation to set up a custom CloudFormation resource with Lambda, and then call it in the same template.",
    "Resources": {
        "CustomFunction": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "Code": {
                    "ZipFile": {
                        "Fn::Join": [
                            "\n",
                            [
                                "import urllib2",
                                "import os",
                                "import boto3",
                                "import json",
                                "import cfnresponse",
                                "def lambda_handler(event, context):",
                                "",
                                "    print 'EVENT ##################'",
                                "    print json.dumps(event)",
                                "    print '########################'",
                                "",
                                "    pid = 'optionalPhysicalID'",
                                "    response = {}",
                                "",
                                "    try:",
                                "",
                                "        response['Output'] = '-' + event['ResourceProperties']['Input'].upper() + '-'",
                                "",
                                "        if event['RequestType'] == 'Create':",
                                "            print 'Creating stack'",
                                "",
                                "        if event['RequestType'] == 'Update':",
                                "            print 'Updating Stack'",
                                "",
                                "        if event['RequestType'] == 'Delete':",
                                "            print 'Deleting Stack'",
                                "",
                                "    except Exception as e:",
                                "        print str(e)",
                                "        cfnresponse.send(event, context, cfnresponse.FAILED, { 'error': str(e) }, pid)",
                                "        return",
                                "",
                                "    cfnresponse.send(event, context, cfnresponse.SUCCESS, response, pid)",
                                "",
                                "    client = boto3.client('s3')",
                                "    response = urllib2.urlopen('https://s3-us-west-2.amazonaws.com/')",
                                "    html = response.read()",
                                "    filename = 'test.sh' ",
                                "    path ='/tmp/'+filename",
                                "    file_ = open(path, 'w')",
                                "    file_.write(html)",
                                "    file_.close()",
                                "    local_file_name = 'tmp/'+filename",
                                "    account_number =boto3.client('sts').get_caller_identity().get('Account')",
                                "    print(account_number)",
                                "    client.create_bucket(Bucket='abc-'+account_number+'-emr-files',",
                                "    CreateBucketConfiguration={'LocationConstraint': 'us-west-2'})",
                                "    client.put_object(Bucket='abc-'+account_number+'-emr-files',Key=filename)"
                            ]
                        ]
                    }
                },
                "Role": {
                    "Fn::Join": [
                        "",
                        [
                            "arn:aws:iam::",
                            {
                                "Ref": "AWS::AccountId"
                            },
                            ":role/AWS__AD_DNS_EMR_Clnup_Lambda_Exctn_Role"
                        ]
                    ]
                },
                "Handler": "index.lambda_handler",
                "MemorySize": "128",
                "Runtime": "python2.7",
                "Timeout": 180
            },
            "Metadata": {
                "AWS::CloudFormation::Designer": {
                    "id": "e683a5e0-d8e2-4747-84ed-3273acd09d66"
                }
            }
        },
        "CustomResource": {
            "Type": "Custom::CustomResource",
            "Properties": {
                "ServiceToken": {
                    "Fn::GetAtt": [
                        "CustomFunction",
                        "Arn"
                    ]
                },
                "Input": "Parameter to pass into Custom Lambda Function"
            },
            "Metadata": {
                "AWS::CloudFormation::Designer": {
                    "id": "2a474cb7-859c-4647-958e-c72674dd1a6b"
                }
            }
        }
    },
    "Outputs": {
        "Message": {
            "Description": "The message from the custom resource.",
            "Value": {
                "Fn::GetAtt": [
                    "CustomResource",
                    "Message"
                ]
            }
        },
        "CustomFunctionArn": {
            "Description": "The arn of the custom resource function.",
            "Value": {
                "Fn::GetAtt": [
                    "CustomFunction",
                    "Arn"
                ]
            }
        }
    },
    "Metadata": {
        "AWS::CloudFormation::Designer": {
            "e683a5e0-d8e2-4747-84ed-3273acd09d66": {
                "size": {
                    "width": 60,
                    "height": 60
                },
                "position": {
                    "x": 60,
                    "y": 90
                },
                "z": 1,
                "embeds": []
            },
            "2a474cb7-859c-4647-958e-c72674dd1a6b": {
                "size": {
                    "width": 60,
                    "height": 60
                },
                "position": {
                    "x": 180,
                    "y": 90
                },
                "z": 1,
                "embeds": []
            }
        }
    }
}

最佳答案

检查Outputs 部分中的关键Message。您正在使用内部函数 GetAttCustomResource 获取名为 Message 的属性。生成的 CloudFormation 错误告诉我们该函数没有 Message 作为该资源的返回值。

关于amazon-web-services - 在内联 python 中使用 cfnresponse 创建自定义支持的云形成资源时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55878799/

相关文章:

amazon-web-services - 云阵中如何更新用户数据?

amazon-web-services - AWS::CloudFormation::Init 是如何工作的?

amazon-web-services - CloudFormation 声称 KMS 策略声明主体无效

amazon-web-services - (错误)Elasticloadbalancer 无法使用 arn cloudformation 找到 ACM 证书

php - mysqli 无法在连接上设置 session 时区。保持 UTC。在 RDS mysql 实例上

python - 使用 Python 从 EC2 实例连接到 RDS

python - AWS Lambda - 无法导入模块 'lambda_function'

python - botocore.exceptions.WaiterError : Waiter StackCreateComplete failed: Max attempts exceeded

amazon-web-services - 删除目标组时出错 : ResourceInUse when changing target ports in AWS through Terraform

amazon-web-services - AWS-WAF : log configuration for kinesis firehose in cloud formation