Destroy an EC2 instance in Terraform?

Todd

I'm creating an EC2 instance and loading it up with some "on start" scripts as well as installing desired software. This becomes the base image to be used when spinning up new instances.

Once created, I create an AMI. At this point, there is no need to keep the EC2 image used to create the AMI.

So, the sequence is:

Create EC2
Install packages
Configure packages
Create AMI
Destroy EC2

How do I tell Terraform to then delete the EC2 in the final step?

Adil B

Have you taken a look at Hashicorp's Packer tool? It specializes in building AMIs for use in Terraform and could save you a few manual steps here.

In the meantime though, you can issue a terraform destroy -target aws_instance.<NAME> (substitute your Terraformed EC2 instance's name) command to destroy your instance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is it possible to use terraform to destroy ec2 instance without a state file

Terraform redeploys EC2 instance

httpserver in EC2 instance via terraform

Terraform one EC2 instance with two subnets

terraform copy/upload files to aws ec2 instance

Terraform cannot ssh into EC2 instance to upload files

Terraform AWS EC2 instance - Ubuntu error

How to create EC2 instance on LocalStack with terraform?

Assign fixed private IP for ec2 instance in terraform

Can i update an existing ec2 instance with terraform

Granting aws ec2 instance access to aws secretsmanager in terraform

provisioning an EC2 instance with terraform InvalidKeyPair.NotFound

Added existing IAM role to EC2 instance using Terraform

Terraform Route53 record delay for EC2 instance

terraform aws ec2 instance ip address assignment

Creating terraform aws rds first then create ec2 instance

Terraform - assign multiple subnets to EC2 instance

Ec2 instance creation using terraform assigns public IP

Unable to register ec2 instance into ECS using terraform

how to add a load balancer to an ec2 instance using terraform

unable to access EC2 instance created using terraform

Reffering the IP of an EC2 Instance to a Inbound Rule Using Terraform

Assign aws name identification to an ec2 instance using Terraform

What changes in EC2 require instance recreation in Terraform?

How to get an instance id / arn of an existing ec2 instance via terraform?

Does Terraform expose the instance ID for an EC2 instance used in an ECS launch type?

Why can I not ping my EC2 instance when I've set up the VPC and EC2 via Terraform?

How to uploa local file to the ec2 instance with the module terraform-aws-modules/ec2-instance/aws?

How to create an EC2 instance with disktype "NVMe SSD" through terraform

TOP Ranking

HotTag

Archive