in blogs/ecs-canary-deployments-pipeline/setup/scripts/delete_blog_contents.py [0:0]
def main():
""" Main function. """
apps = [
'yelb-ui',
'yelb-appserver',
'yelb-redisserver',
'yelb-db'
]
#1. List the CFN Stacks and filter by the prefix 'ecs-blogpost'(which was the default EnvironmentName)
stacks_tobe_deleted = _list_stacks()
#2.1 Update the VR to set the routes to empty
_delete_route(apps)
#2.2 Delete the SSM Parameters created by Canary Stack.
_delete_ssm_params(apps)
#2.3 Iterate through the stacks_tobe_deleted, filter the canary stacks and delete them.
for app in apps:
for stack in stacks_tobe_deleted:
pattern = '{}-{}-'.format(ENVIRONMENT_NAME, app)
if stack.startswith(pattern):
_delete_cfn_stack(stack)
stacks_tobe_deleted.remove(stack)
else:
continue
#3. Iterate through the stacks_tobe_deleted, filter the app specific pipeline stack.
if _delete_ecr_images(apps):
for app in apps:
for stack in stacks_tobe_deleted:
pattern = '{}-pipeline-{}'.format(ENVIRONMENT_NAME, app)
if stack.startswith(pattern):
_delete_cfn_stack(stack)
stacks_tobe_deleted.remove(stack)
#4. Find the S3 bucket which hosted Pipeline artifacts.
for stack in stacks_tobe_deleted:
pattern = '{}-deployment-stepfunctions'.format(ENVIRONMENT_NAME)
if stack.startswith(pattern):
response = CFN_CLIENT.describe_stacks(
StackName=stack
)['Stacks'][0]['Outputs']
for output in response:
pattern = '{}-deployment-'.format(ENVIRONMENT_NAME)
if (output['OutputValue']).startswith(pattern):
if S3_RESOURCE.Bucket(output['OutputValue']) in S3_RESOURCE.buckets.all():
bucket = S3_RESOURCE.Bucket(output['OutputValue'])
bucket.objects.all().delete()
else:
break
_delete_cfn_stack(stack)
stacks_tobe_deleted.remove(stack)
#4.1 Find the S3 bucket which we created externally to support artifacts attachment into Lambda functions.
pattern = 'ecs-canary-blogpost-cloudformation-files-'
buckets_list = S3_CLIENT.list_buckets()
for bucket in buckets_list['Buckets']:
if bucket["Name"].startswith(pattern):
print(f'{bucket["Name"]} is about to be deleted')
bucket = S3_RESOURCE.Bucket(bucket["Name"])
bucket.objects.all().delete()
bucket.delete()
#5. Delete the Monitoring Stack.
for stack in stacks_tobe_deleted:
pattern = '{}-monitoring-resources'.format(ENVIRONMENT_NAME)
if stack.startswith(pattern):
_delete_cfn_stack(stack)
stacks_tobe_deleted.remove(stack)
#6. Delete the clusterresources Stack.
for stack in stacks_tobe_deleted:
pattern = '{}-clusterresources'.format(ENVIRONMENT_NAME)
if stack.startswith(pattern):
_delete_cfn_stack(stack)
stacks_tobe_deleted.remove(stack)
#7. Delete the VPC Stack.
for stack in stacks_tobe_deleted:
pattern = '{}-vpc'.format(ENVIRONMENT_NAME)
if stack.startswith(pattern):
_delete_cfn_stack(stack)
stacks_tobe_deleted.remove(stack)
print("Cleanup Done Successfully...Have a good day!")