てっくらのーとは、触れた技術のメモと日常の記録が少し合わさった個人のサイトです。
layer と serverless のメモ
lambda layer
lambda layer 使う際のメモ。
- layer は opt 配下に展開される。
/opt/(zipパッケージ名)/file となる。
- sys.path に /opt/pythonがある。
import sys
def lambda_handler(event, context):
    
    print(sys.path)
# ['/var/task', '/opt/python/lib/python3.7/site-packages', '/opt/python', '/var/runtime', '/var/lang/lib/python37.zip', '/var/lang/lib/
# python3.7', '/var/lang/lib/python3.7/lib-dynload', '/var/lang/lib/python3.7/site-packages', '/opt/python/lib/python3.7/site-packages']なので、layer としてあげるパッケージは python ディレクトリを圧縮するとあまり悩まなくて済む。
serverless framework cli
CLI イメージのメモ。
- create
プロジェクト作成。
sls create --template aws-python
- deploy
デプロイ。
sls deploy --aws-profile (profile name)
--aws-profile は serverless.yml に書いてあればいらない。
provider:
# you can overwrite defaults here
  stage: dev
  region: us-east-1sls deploy すると同じディレクトリにあるファイルやディレクトリも lambda にあがるっぽい。
serverless.yml で制御可能。
package:
#  include:
#    - include-me.py
#    - include-me-dir/**
  exclude:
    - Pipfile
    - python/**
    - python.zip- invoke
起動。
sls invoke -f hello
-f は serverless.yml の functions 定義。
functions:
  *hello*: 
    handler: handler.helloevent を投げて invoke する場合。
-p で (file)path
sls invoke -f hello -p event.json
-d で data
sls invoke -f hello -d "test"
- remove
削除。
sls remove
- log
sls logs -f (function)
- deploy list
Serverless: Listing deployments:
Serverless: -------------
Serverless: Timestamp: 1561802225906
Serverless: Datetime: 2019-06-29T09:57:05.906Z
Serverless: Files:
Serverless: - aws-python.zip
Serverless: - compiled-cloudformation-template.json- deploy list functions
Serverless: Listing functions and their last 5 versions:
Serverless: -------------
Serverless: hello: $LATEST, 5- info
Service Information
service: aws-python
stage: dev
region: us-east-1
stack: aws-python-dev
resources: 5
api keys:
  None
endpoints:
  None
functions:
  hello: aws-python-dev-hello
layers:
  None- metrics
Service wide metrics
June 28, 2019 7:04 PM - June 29, 2019 7:04 PM
Invocations: 7
Throttles: 0
Errors: 0
Duration (avg.): 1.09msservice: aws-python
provider:
  stage: dev
  region: us-east-1
  name: aws
  runtime: python3.7
package:
  exclude:
    - Pipfile
    - python/**
    - python.zip
functions:
  hello:
    handler: handler.hello
    events: []
    name: aws-python-dev-hello- sls invoke -f hello -l true
(略)
--------------------------------------------------------------------
START RequestId: b6cc890b-ce85-4728-a81a-3676385a2c7d Version: $LATEST
END RequestId: b6cc890b-ce85-4728-a81a-3676385a2c7d
REPORT RequestId: b6cc890b-ce85-4728-a81a-3676385a2c7d  Duration: 1.36 ms       Billed Duration: 100 ms         Memory Size: 1024 MB    Max Memory Used: 54 MBserverless framework layer
layer を使いたいとき
ドキュメントでは Ref の設定は layer の名前を TitleCased で記載とあるけど実際は、TitleCase+LambdaLayer のように見える。そもそも TitleCased をよく知らないのだけど、単語の頭文字を大文字にするだけではないのかな。
https://en.wikipedia.org/wiki/Letter_case#Title_Case
layers:
  layerOne:
    path: python
    name: ${self:provider.stage}-${self:provider.region}-layer
    description: Description
    compatibleRuntimes:
      - python3.7
functions:
  hello:
    handler: handler.hello
    layers:
      - {Ref: LayerOneLambdaLayer}layer のモジュールが import できない
以下の構成だと、/opt 配下に格納されてしまったので import 出来なかったようです。feedparser を例に。
layers:
  layerOne:
    path: python.
├── Pipfile
├── event.json
├── handler.py
├── python
│   ├── __pycache__
│   │   └── feedparser.cpython-35.pyc
│   ├── feedparser-5.2.1.dist-info
│   │   ├── DESCRIPTION.rst
│   │   ├── INSTALLER
│   │   ├── METADATA
│   │   ├── RECORD
│   │   ├── WHEEL
│   │   ├── metadata.json
│   │   └── top_level.txt
│   └── feedparser.py
├── python.zip
└── serverless.yml以下に変更すると import で使用できた。
layers:
  layerOne:
    path: module.
├── Pipfile
├── event.json
├── handler.py
├── module
│   └── python
│       ├── __pycache__
│       │   └── feedparser.cpython-35.pyc
│       ├── feedparser-5.2.1.dist-info
│       │   ├── DESCRIPTION.rst
│       │   ├── INSTALLER
│       │   ├── METADATA
│       │   ├── RECORD
│       │   ├── WHEEL
│       │   ├── metadata.json
│       │   └── top_level.txt
│       └── feedparser.py
└── serverless.yml