トリガーイベント: S3へのファイル投入 ※management console上で操作する
def lambda_handler(event, context): print("Lambdaが呼ばれました!") input_bucket = event['Records'][0]['s3']['bucket']['name'] input_key = event['Records'][0]['s3']['object']['key'] print("bucket =", input_bucket) print("key =", input_key)
testして、Deploy, cloudwatch logsで挙動を確認
import boto3 s3 = boto3.client('s3') def read_file(bucket_name, file): response = s3.get_object(Bucket=bucket_name, Key=file_key) return response[u'Body'].read().decode('utf-8') def upload_file(bucket_name, file_key, bytes): out_s3 = boto3.resource('s3') s3Obj = out_s3.Object(bucket_name, file_key) res = s3Obj.put(Body = bytes) return res def lambda_handler(event, context): print("Lambdaが呼ばれました!") input_bucket = event['Records'][0]['s3']['bucket']['name'] input_key = event['Records'][0]['s3']['object']['key'] print("bucket =", input_bucket) print("key =", input_key) text = read_file(input_bucket, input_key) output_key = "outputs/" + input_key upload_file(input_bucket, output_key, bytes(text, 'UTF-8'))
なるほど、lamdbaの基本機能はなんとなく理解した