cf. https://github.com/aws/aws-sam-cli/issues/137
要望はめっちゃあるのに対応する気なさそうなので、
ごり押し
template.yaml
は sam init
したサンプルそのままです。
template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
python3.8
Sample SAM Template for sam-app
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
Architectures:
- x86_64
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
Metadata:
Dockerfile: Dockerfile
DockerContext: ./hello_world
DockerTag: python3.8-v1
app.py
import os
import json
def lambda_handler(event, context):
print(event["requestContext"]["authorizer"])
return {
"statusCode": 200,
"body": json.dumps(
{
"message": "hello world",
}
),
}
if os.getenv("AWS_SAM_LOCAL"):
def phony_authorizer(authorizer):
def _inner(f):
def _wrapper(event, context):
event["requestContext"]["authorizer"] = authorizer
return f(event, context)
return _wrapper
return _inner
lambda_handler = phony_authorizer(
{
"key": "value",
"principalId": None,
"integrationLatency": 5563,
}
)(lambda_handler)
確認
bash
$ sam build && sam local start-api
で api を起動して、別のターミナルから
bash
$ curl http://127.0.0.1:3000/hello
{"message": "hello world"}
とやると、 start-api 側のターミナルに
bash
Invoking Container created from helloworldfunction:python3.8-v1
Building image.................
Skip pulling image and use local one: helloworldfunction:rapid-1.37.0-x86_64.
{'key': 'value', 'principalId': None, 'integrationLatency': 5563}
END RequestId: 08d36d6d-ae15-4cd8-af8b-bf561d826156
REPORT RequestId: 08d36d6d-ae15-4cd8-af8b-bf561d826156 Init Duration: 0.52 ms Duration: 76.82 ms Billed Duration: 77 ms Memory Size: 128 MB Max Memory Used: 128 MB
No Content-Type given. Defaulting to 'application/json'.
2022-01-15 09:14:47 127.0.0.1 - - [15/Jan/2022 09:14:47] "GET /hello HTTP/1.1" 200 -
とか出るはず。