Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Custom resource whenever i update my CFN Stack

I have a custom resource used to get the API key from API gateway and send it as a header to Cloudfront. When i am creating a stack my custom:resource is triggering since it is creating logical ID for first time. But when i update the stack(i.e Changing the API KEY name) Then the API key resource of type AWS::ApiGateway::ApiKey will create a new Logical ID when in turn create a new API key, At this point my custom:resource is not invoking since it has same same logical ID because of this my cloudfront is having old API Key rather than new one.

Is there a way to invoke my custom:resource everytime a update happened to my stack? As a workaround i am changing the Logical Id of custom:resource to trigger it whenever i am updating a resource in my stack. But this is little difficult since logicalId is shared as a reference to many resources.

BTW my custom resource is attached to a lambda function. I even tried changing the Version field and also tried adding values to properties field (i.e.Stackname,parameters etc) but still it is not invoking.

{
   "AWSTemplateFormatVersion" : "2010-09-09",
   "Resources" : {
      "MyFrontEndTest" : {
         "Type": "Custom::PingTester",
         "Version" : "1.0", -->Even changed the version to 2.0 
         "Properties" : {
            "ServiceToken": "arn:aws:lambda:us-east-1:*****",
            "InputparameterName" :  "MYvalue" -->Added this field
         }
      }
   }

Thanks Any help is appreciated

like image 834
Private Avatar asked Nov 14 '18 12:11

Private


People also ask

What happens when a CF stack is updated?

AWS CloudFormation provides two methods for updating stacks: direct update or creating and executing change sets. When you directly update a stack, you submit changes and AWS CloudFormation immediately deploys them. Use direct updates when you want to quickly deploy your updates.

How do I update resources in CloudFormation stack?

Sign in to the Amazon Web Services Management Console and open the Amazon CloudFormation console at https://console.amazonaws.cn/cloudformation . In the Amazon CloudFormation console , from the list of stacks, select the running stack that you want to update. In the stack details pane, choose Update.

Can CloudFormation update existing resource?

Update with No Interruption. AWS CloudFormation updates the resource without disrupting operation of that resource and without changing the resource's physical ID. For example, if you update certain properties on an AWS::CloudTrail::Trail resource, AWS CloudFormation updates the trail without disruption.


1 Answers

One trick to get the custom resource to execute a lambda when the stack is updated is to configure the custom resource to pass all stack parameters to the lambda function. If any parameters change on stack update, the custom resource will change and trigger the lambda. Just ignore the unneeded keys in the lambda event data. This won't do anything for the scenario when just the template is updated.

like image 113
Andy Hendrickson Avatar answered Sep 20 '22 15:09

Andy Hendrickson