AWS Lambda カスタムレイヤーの使用
Tech Knowledge
公開: 2023-06-16
目次
-
- 1.1. レイヤーの作成
- 1.2. レイヤーを使用するファンクション側
手順
レイヤーの作成
- requirements.txt の内容は空でよい
- template.yaml
- 下記の場合は ArcLayer が レイヤーの名称になる
- ContentUri は各モジュール(*.py ファイル)の親フォルダを指定
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
Arc Common Functions Layer
Resources:
ArcLayer:
Type: AWS::Serverless::LayerVersion
Properties:
Description: Arc Common Functions Layer
ContentUri: "app/"
CompatibleRuntimes:
- python3.10
Metadata:
BuildMethod: python3.10
レイヤーを使用するファンクション側
- requirements.txt
- レイヤー内のモジュールの実行に必要なものはここに追記
- レイヤーで定義しているモジュールそのものについては追記不要
- template.yaml に Layers を追加(レイヤーの ARN を指定、末尾はバージョン番号)
Resources:
CurrentDataFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: "current-data"
・・・
Policies:
- arn:aws:iam::aws:policy/service-role/AWSLambdaRole
- arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
- arn:aws:iam::aws:policy/AmazonSESFullAccess
Environment:
・・・
Layers:
- arn:aws:lambda:ap-northeast-1:864883643265:layer:ArcLayer:5
スタックの削除
何かしらのエラーが発生してデプロイできない場合は CloudFormation のスタックごと削除する。
aws cloudformation delete-stack --stack-name myteststack
スタック名は samconfig.toml の stack_name
# More information about the configuration file can be found here:
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
version = 0.1
[default]
[default.global.parameters]
stack_name = "ArcLayer"
aws cloudformation delete-stack --stack-name ArcLayer