0

I have to trigger a lambda function once specific stack is created.

I have created the below CloudWatch event rule and associated the target to that lambda function but it is not triggering the lambda.

{
  "source": [
    "aws.cloudformation"
  ],
  "detail-type": [
    "AWS API Call via CloudTrail"
  ],
  "detail": {
    "eventSource": [
      "cloudformation.amazonaws.com"
    ],
    "eventName": [
      "CreateStack"
    ],
    "stackName": [
      "sql-automate-04-08"
    ]
  }
}

Please let me know if i am missing anything here.

2 Answers2

2

This doesn’t work using CloudWatch Event Rules because the CloudFormation stack’s lifecycle events don’t reflect individual API calls.

However, you can configure CloudFormation to send stack events to an Amazon SNS topic via its NotificationARNs property. An AWS Lambda function subscribed to that topic can then filter and process the events.

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • Yeah, i used that approach also but then i can see on my stack creation lambda is getting triggered multiple times. I have also set the Retry attemps to 0 but still it is triggering the lambda multiple times. – Chaitanya Vats Aug 07 '20 at 12:35
  • Yes, it’s triggered by each event, not only when the stack creation is complete. To address this you can apply an SNS filter policy: https://docs.aws.amazon.com/sns/latest/dg/sns-message-filtering.html – Dennis Traub Aug 07 '20 at 12:38
  • I have used the below subscription policy but now it is not triggering the lambda. { "ResourceStatus": [ "CREATE_COMPLETE" ] } – Chaitanya Vats Aug 07 '20 at 12:56
0

This EventBridge Rule has worked for me:

{
  "source": ["aws.cloudformation"],
  "detail-type": ["CloudFormation Stack Status Change"],
  "region": ["us-east-1"],
  "detail": {
    "status-details": {
      "status": ["CREATE_COMPLETE"]
    }
  }
}
bnrosa
  • 1