Using AWS Lambda with AWS CloudFormation - AWS Lambda

Using AWS Lambda with AWS CloudFormation

In an AWS CloudFormation template, you can specify a Lambda function as the target of a custom resource. Use custom resources to process parameters, retrieve configuration values, or call other AWS services during stack lifecycle events.

The following example invokes a function that's defined elsewhere in the template.

Example – Custom resource definition
Resources: primerinvoke: Type: AWS::CloudFormation::CustomResource Version: "1.0" Properties: ServiceToken: !GetAtt primer.Arn FunctionName: !Ref randomerror

The service token is the Amazon Resource Name (ARN) of the function that AWS CloudFormation invokes when you create, update, or delete the stack. You can also include additional properties like FunctionName, which AWS CloudFormation passes to your function as is.

AWS CloudFormation invokes your Lambda function asynchronously with an event that includes a callback URL.

Example – AWS CloudFormation message event
{ "RequestType": "Create", "ServiceToken": "arn:aws:lambda:us-east-1:123456789012:function:lambda-error-processor-primer-14ROR2T3JKU66", "ResponseURL": "", "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/lambda-error-processor/1134083a-2608-1e91-9897-022501a2c456", "RequestId": "5d478078-13e9-baf0-464a-7ef285ecc786", "LogicalResourceId": "primerinvoke", "ResourceType": "AWS::CloudFormation::CustomResource", "ResourceProperties": { "ServiceToken": "arn:aws:lambda:us-east-1:123456789012:function:lambda-error-processor-primer-14ROR2T3JKU66", "FunctionName": "lambda-error-processor-randomerror-ZWUC391MQAJK" } }

The function is responsible for returning a response to the callback URL that indicates success or failure. For the full response syntax, see Custom resource response objects.

Example – AWS CloudFormation custom resource response
{ "Status": "SUCCESS", "PhysicalResourceId": "2019/04/18/[$LATEST]b3d1bfc65f19ec610654e4d9b9de47a0", "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/lambda-error-processor/1134083a-2608-1e91-9897-022501a2c456", "RequestId": "5d478078-13e9-baf0-464a-7ef285ecc786", "LogicalResourceId": "primerinvoke" }

AWS CloudFormation provides a library called cfn-response that handles sending the response. If you define your function within a template, you can require the library by name. AWS CloudFormation then adds the library to the deployment package that it creates for the function.

If your function that a Custom Resource uses has an Elastic Network Interface attached to it, add the following resources to the VPC policy where region is the Region the function is in without the dashes. For example, us-east-1 is useast1. This will allow the Custom Resource to respond to the callback URL that sends a signal back to the AWS CloudFormation stack.

arn:aws:s3:::cloudformation-custom-resource-response-region", "arn:aws:s3:::cloudformation-custom-resource-response-region/*",

The following example function invokes a second function. If the call succeeds, the function sends a success response to AWS CloudFormation, and the stack update continues. The template uses the AWS::Serverless::Function resource type provided by AWS Serverless Application Model.

Example – Custom resource function
Transform: 'AWS::Serverless-2016-10-31' Resources: primer: Type: AWS::Serverless::Function Properties: Handler: index.handler Runtime: nodejs16.x InlineCode: | var aws = require('aws-sdk'); var response = require('cfn-response'); exports.handler = function(event, context) { // For Delete requests, immediately send a SUCCESS response. if (event.RequestType == "Delete") { response.send(event, context, "SUCCESS"); return; } var responseStatus = "FAILED"; var responseData = {}; var functionName = event.ResourceProperties.FunctionName var lambda = new aws.Lambda(); lambda.invoke({ FunctionName: functionName }, function(err, invokeResult) { if (err) { responseData = {Error: "Invoke call failed"}; console.log(responseData.Error + ":\n", err); } else responseStatus = "SUCCESS"; response.send(event, context, responseStatus, responseData); }); }; Description: Invoke a function to create a log stream. MemorySize: 128 Timeout: 8 Role: !GetAtt role.Arn Tracing: Active

If the function that the custom resource invokes isn't defined in a template, you can get the source code for cfn-response from cfn-response module in the AWS CloudFormation User Guide.

For more information about custom resources, see Custom resources in the AWS CloudFormation User Guide.