AWS

ECS blue/green | jenkins

KK_Ryong 2023. 9. 18. 10:53

ECS 클러스터 생성 후 서비스 생성 시 

좌측 상단에 새로운 ECS 환경 체크 해제 후 생성 해줘야 블루그린 옵션 선택 가능

(서비스 생성 시 deploy 생성 가능 하며 따로 deploy 생성 후 연결 해줘도 됨)

 

■ESC update■

기존 ecs 서비스 강제 업데이트 시 deploy 에 물려있어 ecs 업데이트 명령어가 먹히지 않는다

deploy 업데이트 해주는 방식으로 변경

 

S3 appspec.yaml 생성 해서 넣어두기 (아래는 ecs 블루그린으로 생성 하면 자동으로 deploy 에 생성 되는 값을 S3에 위치

{
  "version": 1,
  "Resources": [
    {
      "TargetService": {
        "Type": "AWS::ECS::Service",
        "Properties": {
          "TaskDefinition": "Task ARN 적어주기",
          "LoadBalancerInfo": {
            "ContainerName": "컨테이너 이름",
            "ContainerPort": 8080
          }
        }
      }
    }
  ],
  "Hooks": []
}

S3에 yaml 실행 하도록 셋팅

--application-name ${deploy}   < codedeploy app 이름

--deployment-group-name ${deploy} < codedeploy 배포그룹 이름

--s3-location bucket=[버킷이름]

bundleType=yaml,key=****.yaml

aws ${profile} deploy create-deployment --application-name ${deploy} --deployment-group-name ${deploy} --s3-location bucket=walk-stg-codeploy,bundleType=yaml,key=api_appspec.yaml

 

■알림■

 

Codedeploy - SNS - Lambda  이용 하여 telegram 으로 받기 

 

■Lambda code■

import json
import os
import logging
from botocore.vendored import requests

# Initializing a logger and settign it to INFO
logger = logging.getLogger()
logger.setLevel(logging.INFO)

# Reading environment variables and generating a Telegram Bot API URL
TOKEN = os.environ['TOKEN']
USER_ID = os.environ['USER_ID']
TELEGRAM_URL = "https://api.telegram.org/bot{}/sendMessage".format(TOKEN)

# Helper function to prettify the message if it's in JSON
def process_message(input):
    try:
        # Loading JSON into a string
        raw_json = json.loads(input)
        # Outputing as JSON with indents
        output = json.dumps(raw_json, indent=4)
    except:
        output = input
    return output

# Main Lambda handler
def lambda_handler(event, context):
    # logging the event for debugging
    logger.info("event=")
    logger.info(json.dumps(event))

    # Basic exception handling. If anything goes wrong, logging the exception    
    try:
        # Reading the message "Message" field from the SNS message
        message = process_message(event['Records'][0]['Sns']['Message'])

        # Payload to be set via POST method to Telegram Bot API
        payload = {
            "text": message.encode("utf8"),
            "chat_id": USER_ID
        }

        # Posting the payload to Telegram Bot API
        requests.post(TELEGRAM_URL, payload)

    except Exception as e:
        raise e

■Lambda 구성에 환경변수 설정■

   TOKEN = 텔레그렘 API 값 

   USER_ID= 채팅방 값

 

■SNS 는 생성 해주고 람다 트리거에 연결■

■CD - 애플리케이션 - 설정 - 알람규칙 생성 해주기■

■CD - 애플리케이션 - 배포그룹 편집 - 아래 고급 - 트리거 셋팅 해주기■