Problem:
Using the AWS CDK v2, how to deploy the "Canvas Layer for AWS Lambda" serverless application and attach the layer to a Lambda function.
lambda-layer-canvas-nodejs
https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:990551184979:applications~lambda-layer-canvas-nodejs
https://charoitel.github.io/lambda-layer-canvas-nodejs/
To get the latest semanticVersion:
- Open the AWS Management Console.
- Navigate to the Serverless Application Repository.
- Search for
lambda-layer-canvas-nodejs
. - View the application details to find the latest version.
Solution:
import { Stack } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { CfnApplication } from 'aws-cdk-lib/aws-sam';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as lambdaNJS from 'aws-cdk-lib/aws-lambda-nodejs';
export default class ChartStack extends Stack {
constructor(scope: Construct, id: string, props: cdk.StackProps) {
super(scope, id, props);
const chartJsApp = new CfnApplication(this, 'ChartJsApp', {
location: {
applicationId: 'arn:aws:serverlessrepo:us-east-1:990551184979:applications/lambda-layer-canvas-nodejs',
semanticVersion: '2.11.3'
}
});
const layerArn = chartJsApp.getAtt('Outputs.LayerVersion').toString();
const njs = new lambdaNJS.NodejsFunction(this, 'ChartLambda', {
entry: 'main.ts',
handler: 'handler',
runtime: lambda.Runtime.NODEJS_18_X,
layers: [lambda.LayerVersion.fromLayerVersionArn(this, 'ChartJsLayer', layerArn)]
});
// Add dependency to ensure the layer is deployed before the function
njs.node.addDependency(chartJsApp);
}
}