Tools: 30.Delete EC2 Instance Using Terraform

Tools: 30.Delete EC2 Instance Using Terraform

Source: Dev.to

Lab Information ## Lab Solutions ## Step 1: Check Main Terraform Configuration ## Step2: To deploy this configuration ## Navigate to the Terraform directory: ## Initialize Terraform: ## Destroy the configuration: ## Resources & Next Steps ## πŸ“¦ Full Code Repository: KodeKloud Learning Labs ## πŸ“– More Deep Dives: Whispering Cloud Insights - Read other technical articles ## πŸ’¬ Join Discussion: DEV Community - Share your thoughts and questions ## πŸ’Ό Let's Connect: LinkedIn - I'd love to connect with you ## Credits ## β€’ All labs are from: KodeKloud ## β€’ I sincerely appreciate your provision of these valuable resources. During the migration process, several resources were created under the AWS account. Some of these test resources are no longer needed at the moment, so we need to clean them up temporarily. One such instance is currently unused and should be deleted. 1) Delete the ec2 instance named datacenter-ec2 present in us-east-1 region using terraform. Make sure to keep the provisioning code, as we might need to provision this instance again later. 2) Before submitting your task, make sure instance is in terminated state. The Terraform working directory is /home/bob/terraform. Note: Right-click under the EXPLORER section in VS Code and select Open in Integrated Terminal to launch the terminal. Then type yes when prompted to confirm the creation of the snapshot. Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse COMMAND_BLOCK: # main.tf # Provision EC2 instance resource "aws_instance" "ec2" { ami = "ami-0c101f26f147fa7fd" instance_type = "t2.micro" vpc_security_group_ids = [ "sg-4e8e273eca9981051" ] tags = { Name = "datacenter-ec2" } } Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: # main.tf # Provision EC2 instance resource "aws_instance" "ec2" { ami = "ami-0c101f26f147fa7fd" instance_type = "t2.micro" vpc_security_group_ids = [ "sg-4e8e273eca9981051" ] tags = { Name = "datacenter-ec2" } } COMMAND_BLOCK: # main.tf # Provision EC2 instance resource "aws_instance" "ec2" { ami = "ami-0c101f26f147fa7fd" instance_type = "t2.micro" vpc_security_group_ids = [ "sg-4e8e273eca9981051" ] tags = { Name = "datacenter-ec2" } } CODE_BLOCK: cd /home/bob/terraform Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: cd /home/bob/terraform CODE_BLOCK: cd /home/bob/terraform CODE_BLOCK: terraform init Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: terraform init CODE_BLOCK: terraform init CODE_BLOCK: bob@iac-server ~/terraform via πŸ’  default ➜ terraform init Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "5.91.0"... - Installing hashicorp/aws v5.91.0... - Installed hashicorp/aws v5.91.0 (signed by HashiCorp) Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: bob@iac-server ~/terraform via πŸ’  default ➜ terraform init Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "5.91.0"... - Installing hashicorp/aws v5.91.0... - Installed hashicorp/aws v5.91.0 (signed by HashiCorp) Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. CODE_BLOCK: bob@iac-server ~/terraform via πŸ’  default ➜ terraform init Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "5.91.0"... - Installing hashicorp/aws v5.91.0... - Installed hashicorp/aws v5.91.0 (signed by HashiCorp) Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. CODE_BLOCK: terraform destroy Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: terraform destroy CODE_BLOCK: terraform destroy COMMAND_BLOCK: bob@iac-server ~/terraform via πŸ’  default ➜ terraform destroy aws_instance.ec2: Refreshing state... [id=i-5e6bb7fd83c9f0057] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: # aws_instance.ec2 will be destroyed - resource "aws_instance" "ec2" { - ami = "ami-0c101f26f147fa7fd" -> null - arn = "arn:aws:ec2:us-east-1::instance/i-5e6bb7fd83c9f0057" -> null - associate_public_ip_address = true -> null - availability_zone = "us-east-1a" -> null - disable_api_stop = false -> null - disable_api_termination = false -> null - ebs_optimized = false -> null - get_password_data = false -> null - hibernation = false -> null - id = "i-5e6bb7fd83c9f0057" -> null - instance_initiated_shutdown_behavior = "stop" -> null - instance_state = "running" -> null - instance_type = "t2.micro" -> null - ipv6_address_count = 0 -> null - ipv6_addresses = [] -> null - monitoring = false -> null - placement_partition_number = 0 -> null - primary_network_interface_id = "eni-c31c4e92a1acbff85" -> null - private_dns = "ip-10-53-157-57.ec2.internal" -> null - private_ip = "10.53.157.57" -> null - public_dns = "ec2-54-214-90-237.compute-1.amazonaws.com" -> null - public_ip = "54.214.90.237" -> null - secondary_private_ips = [] -> null - security_groups = [ - "default", ] -> null - source_dest_check = true -> null - subnet_id = "subnet-41298989f52a321cc" -> null - tags = { - "Name" = "datacenter-ec2" } -> null - tags_all = { - "Name" = "datacenter-ec2" } -> null - tenancy = "default" -> null - user_data_replace_on_change = false -> null - vpc_security_group_ids = [ - "sg-4e8e273eca9981051", ] -> null # (8 unchanged attributes hidden) - metadata_options { - http_endpoint = "enabled" -> null - http_protocol_ipv6 = "disabled" -> null - http_put_response_hop_limit = 1 -> null - http_tokens = "optional" -> null - instance_metadata_tags = "disabled" -> null } - root_block_device { - delete_on_termination = true -> null - device_name = "/dev/sda1" -> null - encrypted = false -> null - iops = 0 -> null - tags = {} -> null - tags_all = {} -> null - throughput = 0 -> null - volume_id = "vol-aecbbe533be995fe4" -> null - volume_size = 8 -> null - volume_type = "gp2" -> null # (1 unchanged attribute hidden) } } Plan: 0 to add, 0 to change, 1 to destroy. Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes aws_instance.ec2: Destroying... [id=i-5e6bb7fd83c9f0057] aws_instance.ec2: Still destroying... [id=i-5e6bb7fd83c9f0057, 10s elapsed] aws_instance.ec2: Destruction complete after 10s Destroy complete! Resources: 1 destroyed. Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: bob@iac-server ~/terraform via πŸ’  default ➜ terraform destroy aws_instance.ec2: Refreshing state... [id=i-5e6bb7fd83c9f0057] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: # aws_instance.ec2 will be destroyed - resource "aws_instance" "ec2" { - ami = "ami-0c101f26f147fa7fd" -> null - arn = "arn:aws:ec2:us-east-1::instance/i-5e6bb7fd83c9f0057" -> null - associate_public_ip_address = true -> null - availability_zone = "us-east-1a" -> null - disable_api_stop = false -> null - disable_api_termination = false -> null - ebs_optimized = false -> null - get_password_data = false -> null - hibernation = false -> null - id = "i-5e6bb7fd83c9f0057" -> null - instance_initiated_shutdown_behavior = "stop" -> null - instance_state = "running" -> null - instance_type = "t2.micro" -> null - ipv6_address_count = 0 -> null - ipv6_addresses = [] -> null - monitoring = false -> null - placement_partition_number = 0 -> null - primary_network_interface_id = "eni-c31c4e92a1acbff85" -> null - private_dns = "ip-10-53-157-57.ec2.internal" -> null - private_ip = "10.53.157.57" -> null - public_dns = "ec2-54-214-90-237.compute-1.amazonaws.com" -> null - public_ip = "54.214.90.237" -> null - secondary_private_ips = [] -> null - security_groups = [ - "default", ] -> null - source_dest_check = true -> null - subnet_id = "subnet-41298989f52a321cc" -> null - tags = { - "Name" = "datacenter-ec2" } -> null - tags_all = { - "Name" = "datacenter-ec2" } -> null - tenancy = "default" -> null - user_data_replace_on_change = false -> null - vpc_security_group_ids = [ - "sg-4e8e273eca9981051", ] -> null # (8 unchanged attributes hidden) - metadata_options { - http_endpoint = "enabled" -> null - http_protocol_ipv6 = "disabled" -> null - http_put_response_hop_limit = 1 -> null - http_tokens = "optional" -> null - instance_metadata_tags = "disabled" -> null } - root_block_device { - delete_on_termination = true -> null - device_name = "/dev/sda1" -> null - encrypted = false -> null - iops = 0 -> null - tags = {} -> null - tags_all = {} -> null - throughput = 0 -> null - volume_id = "vol-aecbbe533be995fe4" -> null - volume_size = 8 -> null - volume_type = "gp2" -> null # (1 unchanged attribute hidden) } } Plan: 0 to add, 0 to change, 1 to destroy. Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes aws_instance.ec2: Destroying... [id=i-5e6bb7fd83c9f0057] aws_instance.ec2: Still destroying... [id=i-5e6bb7fd83c9f0057, 10s elapsed] aws_instance.ec2: Destruction complete after 10s Destroy complete! Resources: 1 destroyed. COMMAND_BLOCK: bob@iac-server ~/terraform via πŸ’  default ➜ terraform destroy aws_instance.ec2: Refreshing state... [id=i-5e6bb7fd83c9f0057] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: # aws_instance.ec2 will be destroyed - resource "aws_instance" "ec2" { - ami = "ami-0c101f26f147fa7fd" -> null - arn = "arn:aws:ec2:us-east-1::instance/i-5e6bb7fd83c9f0057" -> null - associate_public_ip_address = true -> null - availability_zone = "us-east-1a" -> null - disable_api_stop = false -> null - disable_api_termination = false -> null - ebs_optimized = false -> null - get_password_data = false -> null - hibernation = false -> null - id = "i-5e6bb7fd83c9f0057" -> null - instance_initiated_shutdown_behavior = "stop" -> null - instance_state = "running" -> null - instance_type = "t2.micro" -> null - ipv6_address_count = 0 -> null - ipv6_addresses = [] -> null - monitoring = false -> null - placement_partition_number = 0 -> null - primary_network_interface_id = "eni-c31c4e92a1acbff85" -> null - private_dns = "ip-10-53-157-57.ec2.internal" -> null - private_ip = "10.53.157.57" -> null - public_dns = "ec2-54-214-90-237.compute-1.amazonaws.com" -> null - public_ip = "54.214.90.237" -> null - secondary_private_ips = [] -> null - security_groups = [ - "default", ] -> null - source_dest_check = true -> null - subnet_id = "subnet-41298989f52a321cc" -> null - tags = { - "Name" = "datacenter-ec2" } -> null - tags_all = { - "Name" = "datacenter-ec2" } -> null - tenancy = "default" -> null - user_data_replace_on_change = false -> null - vpc_security_group_ids = [ - "sg-4e8e273eca9981051", ] -> null # (8 unchanged attributes hidden) - metadata_options { - http_endpoint = "enabled" -> null - http_protocol_ipv6 = "disabled" -> null - http_put_response_hop_limit = 1 -> null - http_tokens = "optional" -> null - instance_metadata_tags = "disabled" -> null } - root_block_device { - delete_on_termination = true -> null - device_name = "/dev/sda1" -> null - encrypted = false -> null - iops = 0 -> null - tags = {} -> null - tags_all = {} -> null - throughput = 0 -> null - volume_id = "vol-aecbbe533be995fe4" -> null - volume_size = 8 -> null - volume_type = "gp2" -> null # (1 unchanged attribute hidden) } } Plan: 0 to add, 0 to change, 1 to destroy. Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes aws_instance.ec2: Destroying... [id=i-5e6bb7fd83c9f0057] aws_instance.ec2: Still destroying... [id=i-5e6bb7fd83c9f0057, 10s elapsed] aws_instance.ec2: Destruction complete after 10s Destroy complete! Resources: 1 destroyed.