Terraform

Launching your first aws ec2 instance through Terraform

Before launching aws ec2 instance need to setup aws iam user account . You can refer this article how to setup aws iam user account.

1.You can create “launch” directory (any name you can select)
2.Create instance.tf (any name.tf) file and update aws instance related information

instance.tf
provider "aws" {
  region     = "us-west-2"
  access_key = "REPLACE-YOUR-ACCESS-KEY-HERE"
  secret_key = "REPLACE-YOUR-SECRET-KEY-HERE"
}

resource "aws_instance" "firstinstance" {
ami = "ami-081bb417559035fe8"
instance_type = "t2.micro"
}

You have to mention accesskey, secretkey ,AMI id and Instance type .

3.When new directory created , you have to execute the below command

#terraform init

4.plan command will give clear information what’s going to be executed in aws environment before applying.

#terraform plan

5. Use below command to apply and create instance , type “yes” when prompts

#terraform apply

6.New instance launched in AWS

Thats all!. Your first aws instance launched through Terraform.

7.Now you can use below command to destroy . Type “yes” when it prompts

#terraform destroy

Now new instance getting terminated.

Optional:

You can make terraform plan as output file , and you can execute terraform apply by referencing a plan file.

#terraform plan -out outfile.terraform

Now apply with file

Now new aws instance created with the id as shown. You can again execute
#terraform destroy command to delete the instance.

Tagged

Leave a Reply