Cleaning up resources

Follow the steps below in order. Run the CLI commands in the VS Code terminal; the EC2 instance used as the working environment is deleted at the very end.

Delete the CloudFormation stacks

  1. Open CloudFormation.
    • Choose Stacks.
    • Select the cfn-aws-three-tier-demo stack.
    • Choose Delete stack.

AWS CloudFormation

  1. Enter cfn-aws-three-tier-demo

    • Choose Delete stack AWS CloudFormation

    Wait until the cfn-aws-three-tier-demo stack reaches DELETE_COMPLETE before you continue:

    aws cloudformation wait stack-delete-complete --stack-name cfn-aws-three-tier-demo
    
  2. Delete the cfn-vpc-demo stack and wait for the deletion to finish:

    aws cloudformation delete-stack --stack-name cfn-vpc-demo
    aws cloudformation wait stack-delete-complete --stack-name cfn-vpc-demo
    

AWS CloudFormation

  1. Delete the cfn-lambda-demo stack.
    • Wait for the deletion to finish.

    • Delete the log group created when you tested the Lambda function (if it exists):

      aws cloudformation delete-stack --stack-name cfn-lambda-demo
      aws cloudformation wait stack-delete-complete --stack-name cfn-lambda-demo
      aws logs delete-log-group --log-group-name /aws/lambda/CfnLambdaDemo
      

AWS CloudFormation

Delete the remaining resources

  1. When the cfn-aws-three-tier-demo stack is deleted, RDS creates a final snapshot of the demo-database database.
    • Find and delete this snapshot:

      aws rds describe-db-snapshots --db-instance-identifier demo-database --query "DBSnapshots[].DBSnapshotIdentifier" --output text
      aws rds delete-db-snapshot --db-snapshot-identifier <snapshot_id>
      

AWS CloudFormation

  1. When you uploaded the template in section 5.1, CloudFormation created an S3 bucket whose name starts with cf-templates- to store the template.
    • Find and delete this bucket:

      aws s3 ls | grep cf-templates
      aws s3 rb s3://<cf_templates_bucket_name> --force
      

AWS CloudFormation

Only delete the cf-templates- bucket if it is no longer used by other CloudFormation stacks in your account.

  1. Delete the cfn-keypair key pair.
    • Delete the cfn-keypair.pem file on your computer:

      aws ec2 delete-key-pair --key-name cfn-keypair
      

AWS CloudFormation

Delete the working environment

  1. Open EC2.
    • Select the cfn-workspace-ec2 instance.
    • Choose Instance state → Terminate (delete) instance, and confirm.

AWS CloudFormation

  1. After the instance state changes to Terminated, choose Security Groups.
    • Delete the security group created by the launch wizard in section 2 (named like launch-wizard-<n>).

AWS CloudFormation

  1. Choose Key Pairs.
    • Delete the cfn-workspace-key key pair.
    • On your computer, delete the cfn-workspace-key.pem file.
    • Remove the Host cfn-workspace entry from the ~/.ssh/config file.

AWS CloudFormation

  1. Open IAM.
    • Choose Roles.
    • Delete the IaC-Role role.

AWS CloudFormation

Congratulations on completing the Introduction to Infrastructure as Code with AWS CloudFormation workshop.