简体   繁体   English

在嵌入式python中使用cfnresponse创建自定义支持的云形成资源时出错

[英]Error while creating custom backed cloud formation resource with cfnresponse in inline python

Trying to create inline python lambda function as custom backed cloudformation resource.. with cfnresponse to get the information of the resource creation..but my stack is rolling back with errors. 尝试使用cfnresponse创建内联python lambda函数作为自定义支持的cloudformation资源..以获取资源创建的信息。但是我的堆栈因错误而回滚。

Error: CustomResource attribute error: Vendor response doesn't contain Message key in object arn:aws:cloudformation:us-west-2:stack/Custom-lambda/8eaeead0-68b8-11e9-8e31-0247c451c136|CustomResource|3a7885fc-0959-4284-b4f6-8153fe6420df in S3 bucket cloudformation-custom-resource-storage-uswest2. 错误:CustomResource属性错误:供应商响应中的对象arn:aws:cloudformation:us-west-2:stack / Custom-lambda / 8eaeead0-68b8-11e9-8e31-0247c451c136 | CustomResource | 3a7885fc-0959- S3存储桶cloudformation-custom-resource-storage-uswest2中的4284-b4f6-8153fe6420df。 Rollback requested by user. 用户请求的回滚。

{
    "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": []
            }
        }
    }
}

Check the key Message in the Outputs section. 检查“ Outputs部分中的关键Message You are using intrinsic function GetAtt to get an attribute called Message from CustomResource . 您正在使用内部函数GetAttCustomResource获取名为Message的属性。 Resulting CloudFormation error tells us that the function doesn't have Message as a return value of the very resource. 发生的CloudFormation错误告诉我们该函数没有Message作为资源的返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过 Cloud Formation 创建 Amazon Elasticsearch 服务时出现 CloudWatch 资源访问策略错误 - CloudWatch resource access policy error while creating Amazon Elasticsearch Service via Cloud Formation 使用托管策略和内联策略创建角色时出现云形成错误 - Cloud formation error in creating the Role with managed policy and inline policy AWS Cloud Formation 在创建之前删除资源 - AWS Cloud formation delete resource before creating 云形成自定义资源创建失败 - Cloud formation custom resource creation fails 来自自定义资源的云形成功能 - Cloud Formation Function from Custom Resource 使用 Cloud Formation 时 Custom::LambdaTrigger 资源卡在 CREATE_IN_PROGRESS - Custom::LambdaTrigger resource is stuck at CREATE_IN_PROGRESS while using Cloud Formation 重用 Elastic Beanstalk 资源在第二个云形成堆栈中的一个云形成堆栈中创建 - Reuse Elastic Beanstalk resource creating in one cloud formation stack in a second cloud formation stack 使用云形成创建安全组时出现“ Property IpProtocol不能为空”错误 - Getting 'Property IpProtocol cannot be empty' error while creating security group using cloud formation 自定义资源的 Lambda 函数问题:没有名为 cfnresponse 的模块 - Issue with Lambda function for custom resource: No module named cfnresponse 在 Cloud Formation 中创建然后更改资源 - Create then change a resource in Cloud Formation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM