๐Ÿš€ Languege/DevOps

CodePipeline ECR Rolling Update๋ฅผ ์œ„ํ•œ TaskDefinition Update Lambda ์ฝ”๋“œ

mini_world 2022. 8. 10. 22:26
๋ชฉ์ฐจ ์ ‘๊ธฐ

 

CodePipelin ๊ตฌ์„ฑ ์‹œ,
๋นŒ๋“œ๊ณผ์ • ์—†์ด ECR -> ECS ์— ๋ฐ”๋กœ ๋ฐฐํฌํ•œ๋‹ค๋ฉด TaskDefinition ํŒŒ์ผ์„ ์—…๋ฐ์ดํŠธ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

๊ทธ๋Ÿฌ์ž๊ณ  Codebuild๋ฅผ ์“ฐ์ž๋‹ˆ, ํŒŒ์ผ ๋งŒ๋“œ๋Š”๋ฐ ๋ช‡์ดˆ๋ฉด ๋๋‚˜๋Š”๋ฐ, ๋น„์šฉ๋„ ๋น„์šฉ์ด๊ณ  codebuild๋Š” ๋ฌด๊ฒ๋‹ค ใ…  (์ด๊ฑฐ ๋Œ๋ฆฌ๋Š”๋ฐ 4๋ถ„๊ฑธ๋ฆฐ๋‹ค..)

lambda๋กœ ํ•˜๋ฉด 30์ดˆ๋ฉด ๋๋‚˜๋‹ˆ ์ด๋ ‡๊ฒŒ ํ•ด๋ณด๋Š”๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.

===========================================
Source ------>  Def-Step ------> ECS (rolling update)
[ECR]                  [Lambda]             
===========================================

 

import json
import boto3
import zipfile
import tempfile
import os
from subprocess import call

code_pipeline = boto3.client('codepipeline')
s3           = boto3.client('s3')

def lambda_handler(event, context):
    job_id = event['CodePipeline.job']['id']

    bucketN = event['CodePipeline.job']['data']['inputArtifacts'][0]['location']['s3Location']['bucketName']
    objectK = event['CodePipeline.job']['data']['inputArtifacts'][0]['location']['s3Location']['objectKey']

    outputN = event['CodePipeline.job']['data']['outputArtifacts'][0]['location']['s3Location']['bucketName']
    outputK = event['CodePipeline.job']['data']['outputArtifacts'][0]['location']['s3Location']['objectKey']

    tmp_file = tempfile.NamedTemporaryFile()
    
    with tempfile.NamedTemporaryFile() as tmp_file:
        s3.download_file(bucketN, objectK, tmp_file.name)
        with zipfile.ZipFile(tmp_file.name, 'r') as zip:

            json_data = zip.read('imageDetail.json')
            obj = json.loads(json_data)
            #print(obj)
            
            image_uri = obj['ImageURI'].split('@')[0]
            ImageDigest = obj['ImageDigest']
            repo_name = obj['RepositoryName']

            image = f"{image_uri}@{ImageDigest}"
            #print(image)

            definition = [{
                'name': repo_name,
                'imageUri': image
            }]
            #print(definition)
            
            with open('/tmp/imagedefinitions.json', 'w') as outfile:
                json.dump(definition, outfile)
                
            # taskdef ํŒŒ์ผ ์ค€๋น„
            for i in ['TaskDef-1', 'TaskDef-2', 'TaskDef-4']:
              file_name = '/tmp/imagedefinitions-'+i+'.json'
              file_data = [{
                'name': "alpha-"+i,
                'imageUri': image
              }]
              with open(file_name, 'w') as outfile:
                json.dump(file_data, outfile)

            # ์••์ถ•
            file_path = '/tmp/'
            zip_file = zipfile.ZipFile('/tmp/imagedefinitions.zip', mode='w')
            os.chdir("/tmp/")
            for file in os.listdir(file_path):
              if file.endswith('.json'):
                zip_file.write(file)
            zip_file.close()
            
            # jsonํŒŒ์ผ ์ž˜ ์ƒ๊ฒผ๋Š”์ง€ ํ™•์ธ
            path_dir = '/tmp'
            file_list = os.listdir(path_dir)
            print(file_list)

            # s3์— ์— ์ถœ๋ ฅ ์•„ํ‹ฐํŽ™ํŠธ๋กœ ์—…๋กœ๋“œ
            response = s3.upload_file('/tmp/imagedefinitions.zip', outputN, outputK)
            #์ฝ”๋“œํŒŒ์ดํ”„๋ผ์ธ์— success ์ „๋‹ฌ
            code_pipeline.put_job_success_result(jobId=job_id)
            #/tmp๊ฒฝ๋กœ clear
            call('rm -rf /tmp/*', shell=True)
    return True
728x90