【问题标题】:Updating an AWS CloudFormation with a custom trigger for Lambda使用 Lambda 的自定义触发器更新 AWS CloudFormation
【发布时间】:2017-03-24 14:22:36
【问题描述】:

我和一个团队成员有一个 CloudFormation 堆栈,其中包含一个 nodejs Lambda 支持的自定义资源。

在更新 lambda/parameters/trigger 时,我们希望 Lambda 首先删除它创建的第 3 方资源,然后根据新参数创建新的资源。

这是 lambda 的 export.handler。

if (event.RequestType == "Delete") {
    console.log("Request type == Delete")
    var successCallback = function(event, context) {
        sendResponse(event, context, "SUCCESS");
    }
    doDeleteThings(event, context, successCallback);
} else if (event.RequestType == "Create") {
    console.log("request type == create")
    doCreateThings(event, context);
} else if (event.RequestType == "Update") {
    console.log("request type == update")
    var successCallback = function(event, context) {
        doCreateThings(event, context);
    }
    doDeleteThings(event, context, successCallback);
} else {
    sendResponse(event, context, "SUCCESS");
}

我们已经测试了代码,它适用于 CloudFormation 中的创建和删除,以及无堆栈模式下的创建、删除和更新(我们设置:event.RequestType = process.env.RequestType 和 sendResponse 不执行通常的 CloudFormation 响应 POST,但只是执行 context.done()),但我们似乎无法让它在 CloudFormation 中进行更新。我开始认为我们误解了 Lambda 上的“更新”应该做什么。

我们以前从未能够看到 CloudFormation 创建的 Lambda 函数的 CloudWatch 日志,这无济于事。

这是 CloudFormation 模板的相关部分:

   "ManageThirdPartyResources": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
            "Code": {
                "S3Bucket": "<bucketname>",
                "S3Key": "<zipname>.zip"
            },
            "Description": { "Fn::Join": ["", ["Use cloudformation to automatically create third party resources for the ", { "Ref": "ENV" }, "-", { "Ref": "AWS::StackName" }, " environment"]] },
            "Environment": {
                "Variables": {
                    <environment variables that will probably be the things changing.>
                }
            },
            "FunctionName": {
                "Fn::Join": ["_", [{ "Ref": "AWS::StackName" }, "ManageThirdPartyResources"]]
            },
            "Handler": "index.handler",
            "Role": "<role>",
            "Runtime": "nodejs4.3",
            "Timeout": 30
        }
    },
    "ThirdPartyResourcesTrigger": {
        "Type": "Custom::ThirdPartyResourcesTrigger",
        "Properties": {
            "ServiceToken": { "Fn::GetAtt": ["ManageThirdPartyResources", "Arn"] }
        }
    },

谢谢!

【问题讨论】:

    标签: amazon-web-services lambda amazon-cloudformation


    【解决方案1】:

    如果 属性之一发生更改,将在您的 Custom::ThirdPartyResourcesTrigger 上触发更新。如果 Lambda 函数的属性发生变化,它不会触发Custom::ThirdPartyResourcesTrigger 的更新。

    所以如果你想在Custom::ThirdPartyResourcesTrigger 上触发更新,你必须修改它的属性。例如,您可以向ThirdPartyResourcesTrigger 添加一个名为ThingName 的属性,并且每当您更改ThingName 的值时,您的Lambda 将使用Update 请求类型调用:

    "ThirdPartyResourcesTrigger": {
        "Type": "Custom::ThirdPartyResourcesTrigger",
        "Properties": {
            "ServiceToken": { "Fn::GetAtt": ["ManageThirdPartyResources", "Arn"] },
            "ThingName": "some value"
        }
    },
    

    至于日志记录,请确保您的 Lambda 函数承担的 IAM 角色具有 CloudWatch 日志所需的权限:

    "Effect": "Allow"
    "Action": "logs:*"
    "Resource": "arn:aws:logs:*:*:*"
    

    【讨论】:

    • 日志是天赐之物,感谢一百万。您的触发器解决方案似乎有效,但随后某些事情导致 lambda 几乎立即被删除,所以我们必须弄清楚这一点,但我们真的很接近,所以再次感谢!
    • @L.D.你有没有想过为什么 CloudFormation 在Update 之后调用Delete?我有同样的问题,如果你有答案 - 请在这里发布:stackoverflow.com/q/50599602/53538
    猜你喜欢
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 2018-10-02
    • 2020-10-09
    • 1970-01-01
    • 2021-11-14
    • 2022-11-03
    • 2022-01-12
    相关资源
    最近更新 更多