[AWS] Lambdaのレイヤーサンプル

$ tree
.
└── common
├── layer
│   └── python
│   └── util.py
└── serverless.yml

serverless.yml

service: sample-layer

provider:
  name: aws

layers:
  samplelayer:
    path: layer

util.py

def hello():
	print('Hello, Lambda Layers World!')

serverless.yml

service: sample-function

provider:
  name: aws
  runtime: python3.7
  iamRoleStatements:
  - Effect: "Allow"
    Action:
    - "lambda:InvokeFunction"
    Resource: "*"

functions:
  samplefunction:
    handler: handler.handle_request
    layers:
      - {上記のLayerをsls deployした時に表示されるarn}

handler.py

import util

def handler_request(event, context):
	util.hello()

Layerはデプロイされる度にバージョンが上がる

LayersのARNを確認
$ aws lambda list-layer-versions –layer-name sample-layer –query “LayerVersions[*].LayerVersionArn”
$ aws lambda update-function-configuration –function-name testFunction3 –layers “LayersのARN”

なるほど、なんとなく仕組みと概念は理解した