首页 文章

使用Lambda的自定义触发器更新AWS CloudFormation

提问于
浏览
5

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

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

这是lambda的exports.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响应发布,但只是做了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"] }
        }
    },

谢谢!

1 回答

  • 5

    如果 its 属性之一发生更改,则会在 Custom::ThirdPartyResourcesTrigger 上触发更新 . 如果Lambda函数上的属性发生更改,它将 not 触发 Custom::ThirdPartyResourcesTrigger 上的更新 .

    因此,如果要在 Custom::ThirdPartyResourcesTrigger 上触发更新,则必须修改其属性 . 例如,您可以向 ThirdPartyResourcesTrigger 添加一个名为 ThingName 的属性,每当您更改 ThingName 的值时,将使用 Update 请求类型调用您的Lambda:

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

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

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

相关问题