As per the AWS documentation here (See: RequestType) a custom Lambda function can be triggered by CloudFormation operations of the types: create-stack
, update-stack
, or delete-stack
operations.
My lambda is configured to only do its task on create-stack
, but when I added this Custom Resource and Lambda to my (pre-existing) CloudFormation stack and updated it, the lambda ran as if the stack was created, even though it was updated.
Should it not run only for the first create? Why would it work for the first update?
Note: It did not run for subsequent updates
Relevant Code Snippets
CloudFormation Resources:
LambdaUpdateNodeStatusFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Ref LambdaUpdateNodeStatusFunctionName
CodeUri: ../../../bin/handlers/espnode/lambdaupdatenodesstatus_c
Handler: lambdaupdatenodesstatus
Role: !GetAtt LambdaUpdateNodeStatusExecutionRole.Arn
MemorySize: 512 #MB
Timeout: 900
LambdaUpdateNodeStatusFunctionLog:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join ["", ["/aws/lambda/", !Ref LambdaUpdateNodeStatusFunctionName]]
LambdaUpdateNodeStatusCustomResource:
Type: Custom::LambdaUpdateNodeStatusCustomResource
Properties:
ServiceToken: !GetAtt LambdaUpdateNodeStatusFunction.Arn
Version: !Ref BuildVersion
Lambda Code:
type CustomLambdaRequest struct {
RequestType string `json:"RequestType"`
ResponseURL string `json:"ResponseURL"`
StackId string `json:"StackId"`
RequestId string `json:"RequestId"`
ResourceType string `json:"ResourceType"`
LogicalResourceId string `json:"LogicalResourceId"`
PhysicalResourceId string `json:"PhysicalResourceId"`
}
func Handler(customLambdaRequest CustomLambdaRequest) {
if customLambdaRequest.RequestType == "Create" {
// Do some processing here
} else {
// Do Nothing here
}
}
CodePudding user response:
The Lambda-backed custom resources are triggered, as you mentioned, in the create-stack
, update-stack
, and delete-stack
operations.
But they are triggered for each custom resource that is part of your template.
That means that when you add a new Custom::
resource to your template, it will call the lambda with RequestType == Create
and save the LogicalResourceId
in the cloud formation state. Later when you update the stack it will trigger the function with the update request type and so on.