diff --git a/lambda/mbj-newdwh2021-staging-lambda-dataimport.py b/lambda/mbj-newdwh2021-staging-lambda-dataimport.py new file mode 100644 index 00000000..6b5b061d --- /dev/null +++ b/lambda/mbj-newdwh2021-staging-lambda-dataimport.py @@ -0,0 +1,69 @@ +import os +from datetime import datetime +import boto3 + +# 環境変数 +CLUSTER_NAME = os.environ["CLUSTER_NAME"] +TASK_NAME = os.environ["TASK_NAME"] +CONTAINER_NAME = os.environ["CONTAINER_NAME"] +SUBNET_ID_AP_NORTHEAST_1A = os.environ["SUBNET_ID_AP_NORTHEAST_1A"] +SUBNET_ID_AP_NORTHEAST_1D = os.environ["SUBNET_ID_AP_NORTHEAST_1D"] +SECURITY_GROUP_ID_ECRAPI = os.environ["SECURITY_GROUP_ID_ECRAPI"] +SECURITY_GROUP_ID_ECRDKR = os.environ["SECURITY_GROUP_ID_ECRDKR"] +SECURITY_GROUP_ID_LOGS = os.environ["SECURITY_GROUP_ID_LOGS"] +SECURITY_GROUP_ID_RDS = os.environ["SECURITY_GROUP_ID_RDS"] + +# クラス変数 +ecs_client = boto3.client('ecs') + + +def lambda_handler(event, context): + print(f'{str(datetime.now())} Info I-1 駆動処理開始') + + # イベント情報を取得する + s3_event = event["Records"][0]["s3"] + event_bucket_name = s3_event["bucket"]["name"] + event_object_key = s3_event["object"]["key"] + event_file_name = os.path.basename(event_object_key) + event_data_source_name = os.path.dirname(event_object_key).split('/')[0] + print(f'{str(datetime.now())} Info I-2 バケット名:{event_bucket_name}') + print(f'{str(datetime.now())} Info I-3 ファイル名:{event_file_name}') + print(f'{str(datetime.now())} Info I-4 データソース名:{event_data_source_name}') + + # ECSを起動する + response = ecs_client.run_task( + launchType='FARGATE', + cluster=CLUSTER_NAME, + taskDefinition=TASK_NAME, + networkConfiguration={ + "awsvpcConfiguration": { + "subnets": [ + SUBNET_ID_AP_NORTHEAST_1A, + SUBNET_ID_AP_NORTHEAST_1D, + ], + "securityGroups": [ + SECURITY_GROUP_ID_ECRAPI, + SECURITY_GROUP_ID_ECRDKR, + SECURITY_GROUP_ID_LOGS, + SECURITY_GROUP_ID_RDS, + ], + "assignPublicIp": 'ENABLED', + } + }, + overrides={ + "containerOverrides": [ + { + "name": CONTAINER_NAME, + "environment": [ + {"name": 'BUCKET_NAME', "value": event_bucket_name}, + {"name": 'TARGET_KEY', "value": event_object_key}, + {"name": 'DATA_SOURCE_NAME', "value": event_data_source_name}, + {"name": 'FILE_NAME', "value": event_file_name}, + ], + }, + ], + }, + ) + print(f'{str(datetime.now())} Info I-5 ECS起動レスポンス:{str(response)}') + + print(f'{str(datetime.now())} Info I-6 駆動処理終了')