デプロイ済の Layer 構成
└─ nodejs/
├─ @layer/
│ └─ util/
│ ├─ index.js
│ ├─ package.json
│ └─ package-lock.json
│
├─ node_modules/
├─ package.json
└─ package-lock.json
デプロイ済の Layer 実装
index.js
module.exports = {
sample: value => {
return value;
},
};
Layer(ARN)をアタッチした Lambda 定義
※ Layer(ARN)を SSM に格納しておいた場合
stack.ts
import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import * as lambdaNodejs from '@aws-cdk/aws-lambda-nodejs'
import * as ssm from '@aws-cdk/aws-ssm';
export class AwsCdkTsStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const utilLayerArnParamName = '/layer/utilLayerArn';
const utilLayerArn = ssm.StringParameter.valueForStringParameter(this, utilLayerArnParamName);
const utilLayer = lambda.LayerVersion.fromLayerVersionArn(this, 'utilLayer', utilLayerArn);
new lambdaNodejs.NodejsFunction(this, 'api', {
layers: [utilLayer],
bundling: {
externalModules: ['@layer/util']
},
})
}
}
Layer を使用する Lambda 実装
stack.api.ts
import { Handler } from 'aws-lambda'
const util = require('@layer/util');
export const handler: Handler = async () => {
const result = util.sample(100);
}