newdwh2021/lambda/mbj-newdwh2021-staging-lambda-dataimport.py

72 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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"]
MODE = os.environ["MODE"]
# クラス変数
ecs_client = boto3.client('ecs')
def lambda_handler(event, context):
print(f'{datetime.now():%Y-%m-%d %H:%M:%S} 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'{datetime.now():%Y-%m-%d %H:%M:%S} Info I-2 バケット名:{event_bucket_name}')
print(f'{datetime.now():%Y-%m-%d %H:%M:%S} Info I-3 ファイル名:{event_file_name}')
print(f'{datetime.now():%Y-%m-%d %H:%M:%S} 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},
{"name": 'MODE', "value": MODE},
],
},
],
},
)
print(f'{datetime.now():%Y-%m-%d %H:%M:%S} Info I-5 ECS起動レスポンス{str(response)}')
print(f'{datetime.now():%Y-%m-%d %H:%M:%S} Info I-6 駆動処理終了')