AWS CloudFormation Custom Resource

Karanbir Singh
3 min readMay 19, 2021
AWS CloudFormation Custom Resource

AWS & cloud are the two words you will hear together in the area of cloud.

And one awesome thing which I interacted recently with was AWS custom resources for the AWS CloudFormation.

This story/ article is going to focus mainly on custom resource for the AWS CloudFormation & AWS Lambda Functions using the AWS CDK & Serverless framework respectively.

Prerequisites

  1. Zeal to learn & explore 😄.
  2. Experience with NodeJS, Typescript, Serverless framework(some knowledge), AWS CDK(some little knowledge should be fine).
  3. Experience with AWS and some components like, AWS Cognito, AWS Lambda Functions, Dynamo DB, CloudFormation.
  4. And most important one AWS Account 😈.

Example Scenario

  1. Create AWS Cognito User Pool.
  2. Create Client in the same User Pool.
  3. Create a custom Resource backed by Lambda function to create new Dynamo Table and save app client settings as an item(entry) in the same table
Basic Architecture Diagram — AWS CloudFormation Custom Resource

Code Repositories

Had created two separate repositories:-

  1. First one for AWS CDK, for CloudFormation stack. Link here
  2. And second one is for AWS Lambda function(& it’s stack). Link here

The code repos are kind of straight forward for developers & the most expected readers of this article. But will only share the most important parts here.

Instructions for setup (will not cover obvious ones)

  1. For Lambda Function run following commands
    1. (Obviously) Setup dependencies — npm install
    2. Use the same AWS profile as per other stack.
    3. To deploy you will need to user the command serverless deploy
    4. Note — for serverless to work properly one will need to install serverless as global dependency on the system using npm install -g serverless
  2. CloudFormation CDK code run following commands
    1. (Obviously) Setup dependencies — npm install
    2. Setup the AWS profile using your account and using command — aws config
    3. To deploy the example stack in the code use the command as below replace the profile as per your setup on system etc.
    cdk deploy AwsCustomResourceCdkStack — profile <profile_name>— path-metadata false — version-reporting false
    Important Note — Lambda need to be deployed First!

Some important points for the flow (tried my best to create a flow diagram for that)

Flow diagram
  1. Custom Resource triggers the Lambda Function based on provided Lambda function’s ARN as Service Token. (Explore the code you will understand it more)
  2. Lambda function manages few things related to dynamo DB And conditionally manages status which will be sent to response URL.
  3. Lambda function receives a response url which is a Signed S3 URL where lambda function will push the status of custom resource i.e. — SUCCESS or FAILED
  4. Custom resource fetches the response from the S3 bucket(internally) based on certain parameters and then continues stack creation/flow.

--

--

Karanbir Singh

API developer + Web Application developer + Devops Engineer = Full Stack Developer