LoginSignup
1
2

More than 3 years have passed since last update.

Lambdaの実行結果をS3にアップロードする

Posted at

記事について

  • 基本的なLambdaの設定などについては省略し、S3にアップロードする点についてのみ説明

環境

  • AWS Lambda
  • Serverless Framework standalone (
    • Framework Core: 2.11.1 (standalone)
    • Plugin: 4.1.2
    • SDK: 2.3.2
    • Components: 3.3.0

Serverless.ymlにS3アクセスの設定を追加する

provider:
  iamRoleStatements:
    - Effect: Allow
      Action:
        - s3:PutObject
      Resource: "arn:aws:s3:::<your-bucket>/*"
  environment:
    S3_BUCKET: <your-bucket>

実行ハンドラ

import boto3
import os
import json


def handler(event, context):
    s3 = boto3.resource('s3')

    key = 'test.json'
    obj = s3.Object(os.environ['S3_BUCKET'], key)
    json_dict = {'test': 'test'}
    r = obj.put(Body=json.dumps(json_dict))
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2