Chalice와 Boto3을 사용하여 AWS Lambda에서 S3에 액세스

8134 단어 chalice파이썬boto3

  • Chalice : AWS Lambda를 사용하여 앱을 쉽게 만들 수있는 Python 라이브러리

  • Boto3 : S3에 액세스하기위한 파이썬 라이브러리

  • 다음과 같은 구성으로 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"
        }
    }
    

    참고: 대시보드 화면



    좋은 웹페이지 즐겨찾기