Dọn dẹp tài nguyên

Thực hiện các bước dưới đây theo đúng thứ tự. Các lệnh CLI được chạy trong VS Code terminal, EC2 instance dùng làm môi trường làm việc sẽ được xóa ở cuối cùng.

Xóa các CloudFormation stack

  1. Truy cập CloudFormation.
    • Chọn Stacks.
    • Chọn stack cfn-aws-three-tier-demo.
    • Chọn Delete stack.

AWS CloudFormation

  1. Nhập cfn-aws-three-tier-demo

    • Chọn Delete stack AWS CloudFormation

    Đợi stack cfn-aws-three-tier-demo đạt trạng thái DELETE_COMPLETE trước khi tiếp tục:

    aws cloudformation wait stack-delete-complete --stack-name cfn-aws-three-tier-demo
    
  2. Xóa stack cfn-vpc-demo và đợi xóa xong:

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

AWS CloudFormation

  1. Xóa stack cfn-lambda-demo.
    • Đợi xóa xong.

    • Xóa log group được tạo khi test Lambda function (nếu có):

      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

Xóa các tài nguyên còn lại

  1. Khi xóa stack cfn-aws-three-tier-demo, RDS tạo một final snapshot cho database demo-database.
    • Tìm và xóa snapshot này:

      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. Khi upload template ở phần 5.1, CloudFormation tạo một S3 bucket có tên bắt đầu bằng cf-templates- để lưu template.
    • Tìm và xóa bucket này:

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

AWS CloudFormation

Chỉ xóa bucket cf-templates- nếu bucket này không còn được dùng cho các CloudFormation stack khác trong tài khoản.

  1. Xóa key pair cfn-keypair.
    • Xóa file cfn-keypair.pem trên máy tính cá nhân:

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

AWS CloudFormation

Xóa môi trường làm việc

  1. Truy cập EC2.
    • Chọn instance cfn-workspace-ec2.
    • Chọn Instance state → Terminate (delete) instance và xác nhận.

AWS CloudFormation

  1. Sau khi instance chuyển sang trạng thái Terminated, chọn Security Groups.
    • Xóa security group do launch wizard tạo ở phần 2 (tên có dạng launch-wizard-<n>).

AWS CloudFormation

  1. Chọn Key Pairs.
    • Xóa key pair cfn-workspace-key.
    • Trên máy tính cá nhân, xóa file cfn-workspace-key.pem.
    • Xóa mục Host cfn-workspace trong file ~/.ssh/config.

AWS CloudFormation

  1. Truy cập IAM.
    • Chọn Roles.
    • Xóa role IaC-Role.

AWS CloudFormation

Chúc mừng bạn đã hoàn thành workshop Giới thiệu Infrastructure as Code với AWS CloudFormation.