목차
접기
Codepipeline에서 lambda를 실행하려면, Codepipelin에게 완료되었는지 여부를 알려줘야 한다.
아래 코드는 단독실행 task를 위한 샘플 코드이다.
import urllib3
import json
import boto3
import json
import botocore.exceptions
def lambda_handler(event, context):
client = boto3.client("ecs", region_name="ap-northeast-2")
waiter = client.get_waiter('tasks_stopped')
code_pipeline = boto3.client('codepipeline')
job_id = event['CodePipeline.job']['id']
#ECS
task_id='<TaskId>'
cluster_id='<ClusterId>'
cp_id='<용량공급자ID>'
try:
response = client.run_task(
taskDefinition=task_id,
cluster=cluster_id,
count=1,
capacityProviderStrategy=[
{
'capacityProvider': cp_id,
'weight': 1,
'base': 1
}])
container_task_id = response["tasks"][0]["containers"][0]["taskArn"].split('/')[2]
print(container_task_id)
waiter.wait(
cluster=cluster_id,
tasks=[container_task_id],
WaiterConfig={
'Delay': 10,
'MaxAttempts': 10
}
)
except botocore.exceptions.ClientError as e:
code_pipeline.put_job_failure_result(jobId=job_id, failureDetails={'message': 'task 실행 실패', 'type': 'JobFailed'})
print(e.response['Error']['Message'])
else:
code_pipeline.put_job_success_result(jobId=job_id)
print('Function complete.')
return "Complete."
728x90