Lambdaのお勉強がてらに。
s3バケットにアップロードされたgzファイルを展開して、CloudWatchLogsに出力する。
analyze_s3logs from __future__ import print_function import json import urllib import boto3 import gzip print('Loading function') s3 = boto3.client('s3') def lambda_handler(event, context): print("[LambdaLog] Received event: " + json.dumps(event, indent=2)) # Get the object from the event and show its content type bucket = event['Records'][0]['s3']['bucket']['name'] key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8') try: s3.download_file(bucket, key, '/tmp/file.gz') f = gzip.open('/tmp/file.gz', 'rb') content = f.read() f.close print(content) return 0 except Exception as e: print(e) print('[LambdaLog] Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket)) raise e
元記事はこちら
「s3に圧縮ファイルがアップされた際に、解凍して要素をCloudWatchLogsに出力するLambda Function(python版)」