question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cloudformation custom resources' functions are not run on subsequent deploys

See original GitHub issue

This is a Bug Report

Description

For bug reports:

  • What went wrong? I created a cloudformation custom resource and set its handler to a serverless function (see steps to reproduce below). This is surprisingly easy to setup, and seems to work well on first deploy. I can confirm that the custom resource causes the lambda to run and its data property is received through cloudformation logs. Problem is that if I now change the function (e.g. have it return something else), and redeploy (sls deploy), the custom resource is not triggered again, and the reference to the custom resource value (Fn::GetAtt: [MyCustomResource, test]) remains the old value and not the updated value. There are no more logs in cloudformation, i.e. the function never run.

  • What did you expect should have happened? On subsequent deploys, I expect the function to be run again as it was updated and therefore the custom resource to now expose updated attributes as returned by the updated function.

  • What was the config you used?

serverless.yml

service: simple-cr

provider:
  name: aws
  runtime: nodejs6.10
  memorySize: 512
  timeout: 10
  stage: ${opt:stage, 'dev'}
  region: ${opt:region, 'eu-west-2'}
  
custom:
  customResourceValue:
    Fn::GetAtt: [MyCustomResource, test]
      
functions:
  customResource:
    handler: custom.handler
  
resources:
  Resources:
          
    MyCustomResource:
      Type: Custom::MyCustomResource
      Properties:
        ServiceToken:
          Fn::GetAtt: [CustomResourceLambdaFunction, Arn]

custom.js

const response = require('cfn-response');
 
exports.handler = function(event, context) {
  console.log('sending hello as custom resource lambda function handler response');
  return response.send(event, context, response.SUCCESS, {
    test: 'hello'
  });
};
  • What stacktrace or error message from your provider did you see?

Similar or dependent issues:

Steps to reproduce

  1. create above 2 files serverless.yml and custom.js
  2. run npm install --save cfn-response
  3. run sls deploy
  4. confirm that cloudwatch has log entries for the lambda function, i.e. the custom resource works
  5. change the output of the function from test: 'hello' to something else, like test: 'goodbye'
  6. run sls deploy
  7. notice that there are no cloudwatch entries and that the custom value Fn::GetAtt: [MyCustomResource, test] still refers to the old value returned by the previous function

Additional Data

  • Serverless Framework Version you’re using: 1.24.1
  • Operating System: OSX
  • Stack Trace:
  • Provider Error messages:

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
tommedemacommented, Dec 9, 2017

@simlu not really… I worked around it with a somewhat ugly hack, forcing it to recreate on each deploy using a unique parameter UUID:

serverless.yml

  Resources:
    
    # custom resource to retrieve SSL certificates
    CertificateResource:
      Condition: ShouldRequestCertificate
      Type: Custom::CertificateResource
      Properties:
        # a UUID forces a refetch from lambda on eacha deploy
        UUID: ${{file(./scripts/uuid.js)}}
        ...
        ServiceToken:
          Fn::GetAtt: [RequestCertificateLambdaFunction, Arn]

uuid.js

module.exports = () => require('uuid/v4')()
2reactions
patrickwerzcommented, Jan 22, 2020

You can set SomeRandomProperty: ${env:RANDOM} whereas set the env var first export RANDOM=$(date +%s).$RANDOM will then be the current timestamp. Not nice. But there is no other fix currently, right? Custom resource seems to work in pure CF. Why not here?

Read more comments on GitHub >

github_iconTop Results From Across the Web

AWS::CloudFormation::CustomResource
With AWS Lambda functions and custom resources, you can run custom code in response to stack events (create, update, and delete). The following...
Read more >
Implementing long running deployments with AWS ... - Noise
In this post, I'll demonstrate how to use AWS Step Functions to implement custom resources using AWS Cloud Development Kit (AWS CDK).
Read more >
Plug gaps in CloudFormation with Custom Resources
In this tutorial, I'll show you how to patch up CloudFormation with custom resources so you do not have to choose between version...
Read more >
Custom resource not running properly on deployment
1.The custom resource provider processes the AWS CloudFormation request and returns a response of SUCCESS or FAILED to the pre-signed URL. AWS ......
Read more >
How to use the power of CloudFormation custom resources for ...
You can use custom resources to run one-off tasks as part of every CloudFormation deployment. Which is something I wanted to do in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found