Lambda Layerにnode_modulesを含めてCloudFormationでLambdaデプロイする

Lambda Layerにnode_modulesを含めてCloudFormationでLambdaデプロイする

Lambdaレイヤーにnode_modulesを配置してLambdaレイヤーとLambdaをデプロイします。

ディレクトリ構造

├─layer
│  └─nodejs
│      ├─package.json
│      ├─package-lock.json
│      └─node_modules
│          └─luxon
│
├─node_modules ★local testしないなら不要
│  └─luxon
│
├─src
│  └─index.js
├─cfn.yml
├─package.json
└─package-lock.json

以下コマンドでプロジェクト作成します。

$ npm init -y
$ npm i luxon ★local testする場合のみ
$ cd layer/nodejs
$ npm init -y
$ npm i luxon

cfn.yml

AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Resources:
  LambdaLayer:
    Type: "AWS::Serverless::LayerVersion"
    Properties:
      LayerName: layername
      ContentUri: layer/
      CompatibleRuntimes:
        - nodejs18.x
      RetentionPolicy: Delete
  LambdaA:
    Type: "AWS::Serverless::Function"
    Properties:
      FunctionName: functionname
      Handler: index.handler
      Runtime: nodejs18.x
      CodeUri: src/
      Layers:
        - !Ref LambdaLayer

package & deploy

aws cloudformation packageコマンド、aws cloudformation deployコマンドでデプロイします。

$ aws cloudformation package --template-file cfn.yml --s3-bucket <バケット名> --output-template-file cfn_out.yml
Successfully packaged artifacts and wrote output template to file cfn_out.yml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file cfn_out.yml --stack-name 
$ aws cloudformation deploy --template-file cfn_out.yml --stack-name layer-test --capabilities CAPABILITY_IAM
Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - layer-test

layer-testというスタックが作成されます。

Lambda Layerにnode_modulesを含めてCloudFormationでLambdaデプロイする

functionnameというlambdaが作成されます。

Lambda Layerにnode_modulesを含めてCloudFormationでLambdaデプロイする

layernameというlayerが作成されます。

Lambda Layerにnode_modulesを含めてCloudFormationでLambdaデプロイする

Lambdaレイヤー確認

Lambdaレイヤーをダウンロードしzipを解凍して中身の構成を確認します。

Lambda Layerにnode_modulesを含めてCloudFormationでLambdaデプロイする

プロジェクトのlayer配下と同じ構成になっていることが確認できます。

コメント

株式会社CONFRAGE ITソリューション事業部をもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む

タイトルとURLをコピーしました