4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AWS CDK - Lambda への Layer(ARN)のアタッチ

Last updated at Posted at 2022-03-17

デプロイ済の 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);
}
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?