newdwh2021/lambda/mbj-newdwh2021-staging-lambda-dataimport.py
*lcOeIaePm0 1d8cdace48 feat:VPCエンドポイント追加のため、セキュリティグループを追加した
feat:引数の追加のため、ECS起動パラメータに値を追加した
2021-10-20 16:13:13 +09:00

66 lines
2.5 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
ecs_client = boto3.client('ecs')
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']
def lambda_handler(event, context):
print(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(str(datetime.now()) + 'Info I-2 バケット名:' + event_bucket_name)
print(str(datetime.now()) + 'Info I-3 ファイル名:' + event_file_name)
print(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,
],
'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(str(datetime.now()) + 'Info I-5 ECS起動レスポンス' + str(response))
print(str(datetime.now()) + 'Info I-6 駆動処理終了')