以下のような構成で、S3からjsonデータを取得したり、保存したりできるアプリを作ります
WebBrowser -- AWS (API Gateway -- Lambda -- S3)
POST /api/save/ : S3にjsonデータを保存
GET /api/load/{key} : S3からjsonデータを取得
準備: インストール
$ pip install awscli
$ pip install chalice boto3
アプリの作成
$ chalice new-project <project-name>
$ nano app.py
app.py
from chalice import Chalice
import boto3
import json
BUCKET_NAME = 'msrks'
s3 = boto3.client('s3')
app = Chalice(app_name='save2s3')
app.debug = True
@app.route('/load/{key}')
def load(key):
key = str(key)+'.json'
resp = s3.get_object(Bucket=BUCKET_NAME, Key=key)
data = json.loads(resp['Body'].read())
return {'load': data}
@app.route('/save', methods=['POST'], content_types=['application/json'])
def save():
data = app.current_request.json_body
if 'key' not in data:
return {'error': 'please input key'}
key = str(data['key'])+'.json'
s3.put_object(Bucket=BUCKET_NAME, Key=key,
Body=json.dumps(data))
return {'save': data['key']}
デプロイ
$ chalice deploy
Regen deployment package.
Updating IAM policy for role: save2s3-dev
Updating lambda function: save2s3-dev
API Gateway rest API already found: m1iu65vuuk
Deploying to API Gateway stage: api
https://m1iu65vuuk.execute-api.ap-northeast-1.amazonaws.com/api/
動作確認
$ echo '{"key" : "lang", "val" : "python"}' | http https://m1iu65vuuk.execute-api.ap-northeast-1.amazonaws.com/api/save/
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 16
Content-Type: application/json
Date: Tue, 23 Jan 2018 06:16:02 GMT
Via: 1.1 10885a2c24fad0ff660a4e3a8e3fb801.cloudfront.net (CloudFront)
X-Amz-Cf-Id: ODXxBS7Yq7SNy5RBy3l0N10_awp-z8038s3an0DuMVvyKPbvXwxqXw==
X-Amzn-Trace-Id: sampled=0;root=1-5a66d322-d4765ed44785091e48df33ca
X-Cache: Miss from cloudfront
x-amzn-RequestId: e30e3c23-0004-11e8-88a5-537da6fd5cfc
{
"save": "lang"
}
$ http https://m1iu65vuuk.execute-api.ap-northeast-1.amazonaws.com/api/load/lang
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 42
Content-Type: application/json
Date: Tue, 23 Jan 2018 06:16:40 GMT
Via: 1.1 d653c211343f4b2370c41baf3b65bed6.cloudfront.net (CloudFront)
X-Amz-Cf-Id: FRpHr6jcrOO0-VzC_sZBrhr6G6tntH4OTZADPz9Ey_yeXOiR4v5rqA==
X-Amzn-Trace-Id: sampled=0;root=1-5a66d348-c33cfc4a6f0dc0538d6b2ed3
X-Cache: Miss from cloudfront
x-amzn-RequestId: f9a6b059-0004-11e8-ade1-b3a163f5bf18
{
"load": {
"key": "lang",
"val": "python"
}
}
参考:ダッシュボード画面
