简体   繁体   中英

Destroy an EC2 instance in Terraform?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM