Terraform : Creating New EC2 instance via Terraform



In this post, We will share – What is Terraform, How to install it, and How to Create EC2 instance via Terraform.


What is Terraform


Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.


The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.


Terraform supports Multiple Cloud providers, Oracle , AWS, Microsoft..List goes on https://www.terraform.io/docs/providers/index.html


terraform works based on or using configuration files with extension .tf 


How to Install


its Pretty Simple , 
1. Download the zip file which contains the executable based on your platform from https://www.terraform.io/


2. Extract the zip file


3. Set the Path Environment Variable mapping to location where you extracted the zip file


4. open bash prompt or command prompt, and check , terraform –version


C:Usersdummy> terraform –version
Terraform v0.11.13


How to create configuration file


The set of files used to describe infrastructure in Terraform is simply known as a Terraform configuration. We’re going to write our first configuration now to launch a single AWS EC2 instance.

The format of the configuration files is documented here. Configuration files can also be JSON, but we recommend only using JSON when the configuration is generated by a machine.
The entire configuration is shown below. We’ll go over each part after. Save the contents to a file named am1.tf. Verify that there are no other *.tf files in your directory, since Terraform loads all of them.
open notepad or vi , am1.tf , copy the below highlight contents and make required changes , and save it.
Below case we are using provider as aws , ie amazon web services.
Resource – Type of resource , we are planning to provision or manage

provider “aws” {
  access_key = “vandendapaalkaren”
  secret_key = “adaddaaapasumattapathipadaporen”
  region     = “us-east-2”
}


resource “aws_instance” “example” {
  ami           = “ami-0cd3dfa4e37921605”
  instance_type = “t2.micro”

}






Note: The above configuration is designed to work on most EC2 accounts, with access to a default VPC. For EC2 Classic users, please use t1.micro for instance_type, and ami-408c7f28 for the ami. If you use a region other than us-east-1 then you will need to choose an AMI in that region as AMI IDs are region specific.


As mentioned above , Replace the ACCESS_KEY_HERE and SECRET_KEY_HERE with your AWS access key and secret key, available from this page.




How to configure a resource in Cloud?


Once the configuration file is created, There are two step process


go to the location where created the *.tf file, Ensure there is no other tf created over there


Step 1 : init – Execute “terraform init” from the command prompt,


init will download the plugin required ,and initializes various local settings and data that will be used by subsequent commands


c:terraform>terraform init




Step 1.2  : Plan – if in case there are any changes to the configuration after init, then you need to Execute “terraform plan” from the command prompt,


In the same directory as the am1.tf file you created, run terraform plan

Step 2 : apply – Once all set with configuration, then we need to apply the configuration. Execute “terraform apply” from the command prompt,


 







Check now in your AWS Console….Congratulations.





  • April 1, 2019 | 20 views
  • Comments