How to destroy a resource with terraform TF file - amazon-web-services

I create a terraform file that create AWS EC2 instance on AWS console then i create an AMI from that instance, know i want to destroy running instance and keep AMI with terraform language, what should i do?
Thanks you attentions

Try
terraform destroy -target aws_instance.NAME
substitute your Terraformed EC2 instance's name

Related

Terraform EC2 Auto Scaling with EC2 instances that are already build and running

I want to use Terraform to deploy AWS Auto Scaling for a number of EC2 Linux instances that are already build and running.
Can I do this with Terraform or Terraform only allows to create AWS autoscaling using new EC2 instances only?
To address your issue I used Terraformer to import existing resources to the Terraform state. In your case you can import EC2 instances and an autoscaling group using this command:
terraformer import aws --resources=ec2_instance,auto_scaling
--regions=your-region

How to power on AWS instance with Terraform

Sorry I am very new to Terraform.
I create AWS instance with Terraform successfully.
Then I power off the instance in AWS web management console.
How to power on the instance with Terraform?
You would have to use local-exec to run AWS CLI's start-instances.
You can use the instance_type value of aws_instance terraform resource.
instance_type : (Optional) The instance type to use for the instance.
Updates to this field will trigger a stop/start of the EC2 instance.

aws ec2 import-image via Terraform

I'm searching for the Terraform equivalent of "aws ec2 import-image --description "Windows 2008 OVA" --license-type --disk-containers file://containers.json" but cannot find a matching resource command in the documentation.
The purpose is to lift an OVA image out of S3 and covert to AMI so it can be used to launch EC2 instances.
There is an option in packer where you can create OVA by a builder and upload it to S3 and then create an AMI. Once you have the AMI, you can inject into your terraform script to launch it.
https://www.packer.io/docs/post-processors/amazon-import.html
https://github.com/terraform-providers/terraform-provider-aws/issues/5998
Right now terraform not supporting for ec2 image import

Can i start my AWS RDS instance in bootstrap action of EMR creation?

I am using a RDS instance as an external hive metastore, can i start my AWS RDS instance in bootstrap action of EMR creation?
Yes. you can. All EMR's EC2 instances will assume instance profile role - EMR_EC2_DefaultRole by default. Which means if you have proper permissions on that EC2 instance profile role that you use for EMR, you can run AWS CLI commands on your Bootstrap Action script to start RDS Instance.
https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-iam-roles.html
https://docs.aws.amazon.com/cli/latest/reference/rds/start-db-instance.html

How can I set existing EIPs to Auto scaled instances in AWS when they launch automatically?

I have cloud formation template which creates auto-scaling group with desired state 2. I need instances to be attached to existing eips when they get launched. How can I do this?
You need to write a custom user data script that assigns the elastic IP to the instance. You can not do this using CloudFormation templates yet. The AWS CLI to be used is: aws ec2 associate-address. For this, the best practice would be to assign and IAM role with ec2:AssociateAddress permission.
The command will look like this: aws ec2 associate-address --instance-id $INSTANCE_ID --allocation-id $ALLOCATION_ID --allow-reassociation
While the allocation id will need to be hardcoded in the template, you can get the instance id within the instance using the command: curl -s http://169.254.169.254/latest/meta-data/instance-id. Refer this thread
for more details.