14

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

Private
  • 1,661
  • 1
  • 20
  • 51
  • Here is some great sample code for building Custom Resources: [GitHub - stelligent/cloudformation-custom-resources](https://github.com/stelligent/cloudformation-custom-resources) It shows how to handle Updates, which might help you. – John Rotenstein Nov 15 '18 at 05:58
  • 1
    I can able to resolve with the help of this https://stackoverflow.com/questions/41452274/how-to-create-a-new-version-of-a-lambda-function-using-cloudformation – Private Nov 21 '18 at 04:01
  • 3
    @Private Which approach did you follow? I am stuck in a similar situation. How did you make the custom resource create/update every time? Thanks in advance? – Developer Mar 22 '19 at 16:04
  • @Developer Just follow the link that i mentioned in the above comment. Its the same thing that helped me to resolve this issue. – Private Mar 22 '19 at 16:07
  • 1
    @Private did you follow the accepted answer in the link you mentioned? Which answer in that link worked for you? – Developer Mar 22 '19 at 16:22
  • yes and also wjordan answer custom resource step 1 where he clearly explained that we need to change atleast one parameter. – Private Mar 22 '19 at 16:25
  • 2
    I tried following wjordan answer, but because my template file is being deployed from CodePipeline, I am not able to pass a random string/number as parameter to my cloudformation template. The template gets deployed every time there is a code pipeline runs. But I am not sure how to pass parameter to template file from code pipeline – Developer Mar 22 '19 at 16:35
  • You can use the `parameter overrides` field in the codepipeline. I think that will help you – Private Mar 22 '19 at 16:36
  • Glad it helped you – Private Mar 22 '19 at 18:29

1 Answers1

3

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.