Monday 14 October 2019

Terraform in 10 mins


Terraform is  "infrastructure-as-code" and there is a list of many tools over the internet to do your job:

  • Puppet
  • SaltStack
  • Chef
  • Ansible
  • CloudFormation

Terraform can manage existing and popular service providers as well as custom in-house solutions. Configuration files describe to Terraform the components needed to run a single application or your entire datacenter.

Setup:

Download terraform from -> Terraform

Follow steps given in the link to setup your machine -> Install


First Terraform Script:

Create new file example.tf, here .tf is extension of terraform files

If you don't have an AWS account, create one now. I will be using resources which qualify under the AWS free-tier, meaning it will be free. 

If you already have an AWS account, you may be charged some amount of money, but it shouldn't be more than a few rupees at most.

once you have created new AWS account then go ahead and install AWS-CLI from -> Install AWS-CLI



As you have install AWS-CLI now configure your AWS environment
open command prompt and enter command.


 aws configure


$ aws configure

AWS Access Key ID [None]: <your Access key ID>

AWS Secret Access Key [None]: <your secret access key>

Default region name [None]: <your region>


Default output format [None]:



Use below code to create your first EC2 instance :


EC2 instance :

provider "aws" {
  profile    = "default"
  region     = "ap-south-1"
}

resource "aws_instance" "my_terraform_instance" {
  ami           = "ami-01469ab6166957456"
  tags {Name="Rakesh"}
  instance_type = "t2.micro"
}

NOTE : You need to change ami as per your region
you can find region and ami details from here -> AMI Locator


Now we are ready to create our first EC2 instance via terraform.

Run below command from the location of  example.tf file 

E:\home\Cloud_project\stage>terraform init

And you might encounter with below error 

Terraform initialized in an empty directory!

The directory has no Terraform configuration files. You may begin working
with Terraform immediately by creating Terraform configuration files.

To resolve this :
  • Make sure you are running command from the folder where your .tf file is located.
  • Make sure your file name extension is correct.
  • Make sure you have a valid internet connection and you are not on VPN.


E:\home\Cloud_project\stage>terraform init


Deploy EC2:

E:\home\Cloud_project\stage>terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + aws_instance.my_terraform_instance
      id:                           <computed>
      ami:                          "ami-01469ab6166957456"
      arn:                          <computed>
      associate_public_ip_address:  <computed>
      availability_zone:            <computed>
      cpu_core_count:               <computed>
      cpu_threads_per_core:         <computed>
      ebs_block_device.#:           <computed>
      ephemeral_block_device.#:     <computed>
      get_password_data:            "false"
      host_id:                      <computed>
      instance_state:               <computed>
      instance_type:                "t2.micro"
      ipv6_address_count:           <computed>
      ipv6_addresses.#:             <computed>
      key_name:                     <computed>
      network_interface.#:          <computed>
      network_interface_id:         <computed>
      password_data:                <computed>
      placement_group:              <computed>
      primary_network_interface_id: <computed>
      private_dns:                  <computed>
      private_ip:                   <computed>
      public_dns:                   <computed>
      public_ip:                    <computed>
      root_block_device.#:          <computed>
      security_groups.#:            <computed>
      source_dest_check:            "true"
      subnet_id:                    <computed>
      tags.%:                       "1"
      tags.Name:                    "Rakesh"
      tenancy:                      <computed>
      volume_tags.%:                <computed>
      vpc_security_group_ids.#:     <computed>


Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_instance.my_terraform_instance: Creating...
  ami:                          "" => "ami-01469ab6166957456"
  arn:                          "" => "<computed>"
  associate_public_ip_address:  "" => "<computed>"
  availability_zone:            "" => "<computed>"
  cpu_core_count:               "" => "<computed>"
  cpu_threads_per_core:         "" => "<computed>"
  ebs_block_device.#:           "" => "<computed>"
  ephemeral_block_device.#:     "" => "<computed>"
  get_password_data:            "" => "false"
  host_id:                      "" => "<computed>"
  instance_state:               "" => "<computed>"
  instance_type:                "" => "t2.micro"
  ipv6_address_count:           "" => "<computed>"
  ipv6_addresses.#:             "" => "<computed>"
  key_name:                     "" => "<computed>"
  network_interface.#:          "" => "<computed>"
  network_interface_id:         "" => "<computed>"
  password_data:                "" => "<computed>"
  placement_group:              "" => "<computed>"
  primary_network_interface_id: "" => "<computed>"
  private_dns:                  "" => "<computed>"
  private_ip:                   "" => "<computed>"
  public_dns:                   "" => "<computed>"
  public_ip:                    "" => "<computed>"
  root_block_device.#:          "" => "<computed>"
  security_groups.#:            "" => "<computed>"
  source_dest_check:            "" => "true"
  subnet_id:                    "" => "<computed>"
  tags.%:                       "" => "1"
  tags.Name:                    "" => "Rakesh"
  tenancy:                      "" => "<computed>"
  volume_tags.%:                "" => "<computed>"
  vpc_security_group_ids.#:     "" => "<computed>"
aws_instance.my_terraform_instance: Still creating... (10s elapsed)
aws_instance.my_terraform_instance: Still creating... (20s elapsed)
aws_instance.my_terraform_instance: Still creating... (30s elapsed)
aws_instance.my_terraform_instance: Creation complete after 38s (ID: i-0113dba004f0cdb16)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.


Voila..!!

you have created your first EC2 instance via terraform.
Now login into your AWS account and check your newly created EC2 instance running.


Clean up:

destroy your services via below command.

E:\home\Cloud_project\stage>terraform destroy






No comments:

Post a Comment

Spring boot with CORS

CORS (Cross-Origin Resource Sharing) errors occur when a web application running in a browser requests a resource from a different domain or...