EC2 Blue/Green 배포 실패 시 생성된 ASG 자동 삭제 방법
By Bys on April 25, 2024
- Architecture
- 배포 생성
- CodeDeploy 서비스에 의해 ASG 그룹 복제
- 배포 실패
- CodeDeploy 서비스에서 DeploymentFailure 이벤트에 수신 후, SNS topic 트리거
- SNS 서비스를 통해 Lambda function invoke
위 과정을 통해 자동삭제를 지원한다.
- Demo
1. Create SNS Topic.
- Move to menu (Amazon SNS -> Topics -> Create topic)
- Input
- Type:
Standard
- Name:
CodeDeploy_CDP_BG_FailedTopic
- Type:
- Create Topic
2. Configure CodeDeploy Trigger
- Move to CodeDeploy (CodeDeploy > Applications > Click
> Click > Edit > Advanced > Triggers > Create trigger) - Input
- Name:
Trigger-CDP-BG-failed
- Events:
DeploymentFailure, DeploymentStop
- Amazon SNS topics: choose
CodeDeploy_CDP_BG_FailedTopic
created in step one.
- Name:
- Save changes
3. Create AWS Lambda function and IAM Role
- Move to Lambda (Lambda > Functions > Create function)
- Input
- Function name:
CodeDeployDeploymentHandler
- Runtime:
Python
(It depends on users) - Architecture:
x86_64
(It depends on users)
- Function name:
-
Create function
- Move to IAM (IAM > Roles > Search ‘CodeDeployDeploymentHandler-role’ > Click CodeDeployDeploymentHandler-role-
> Permission tab > Add permission > Attach policies) - Input
- Search:
AutoScalingFullAccess
- Search:
- Add permissions
4. Create subscription in SNS topic
- Move to subscription (Amazon SNS > Topics > CodeDeploy_CDP_BG_FailedTopic(Created in step one) > Subscriptions tab > Create subscription)
- Input
- Protocol:
AWS Lambda
- Endpoint:
arn:aws:lambda:ap-northeast-2:111122223333:function:CodeDeployDeploymentHandler
(ARN of Lambda function created in step three)
- Protocol:
- Create subscription
5. Modify AWS Lambda code
- Move to Lambda (Lambda > Functions > CodeDeployDeploymentHandler)
- Input
- Code source: Copy and paste below source code at ‘lambda_function.py’ and click the button ‘Deploy’ ```python import json import boto3
def lambda_handler(event, context): # TODO implement message = event[‘Records’][0][‘Sns’][‘Message’]
# Print the message content for debugging purposes (optional)
print(f"Received SNS message: {message}")
# Parse the JSON message (if applicable)
try:
sns_data = json.loads(message)
except json.JSONDecodeError:
sns_data = 'JSONDecodeError Occurred' # Handle non-JSON messages
print(f"Processing message data: {sns_data}")
deploymentGroupName = sns_data['deploymentGroupName']
deploymentId = sns_data['deploymentId']
print(f"deploymentGroupName: {deploymentGroupName}")
print(f"deploymentId: {deploymentId}")
response = delete_failed_asg(deploymentGroupName, deploymentId)
return response
def delete_failed_asg(deploymentGroupName, deploymentId): asg_client = boto3.client(‘autoscaling’) asg_name = “CodeDeploy_” + deploymentGroupName + “_” + deploymentId
print(f"asg_name: {asg_name}")
response = asg_client.delete_auto_scaling_group(
AutoScalingGroupName=asg_name,
ForceDelete=True
)
return response ```
6. Test
Deploy a new deployment using EC2 Autoscaling Group deployment group and make it fails. Then, CodeDeploy trigger SNS and Lambda function subscribing SNS works. Now, you can see the ASG created by CodeDeploy as a part of failure is removed automatically.
The Lambda code is sample source code. You can develop on your own language or change.
codedeploy
deployment
]