Skip to main content

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






Comments

Popular posts from this blog

Extent report plugin for cucumber framework

Extent Reports  are the most popular  reporting  used with Selenium. ExtentReport API makes our life easy to generate interactive  report  with simple configuartions. It supports almost all Java and .NET test frameworks such as TestNG , JUnit , NUnit etc Here we are discussing about  a plugin which is build on  Extent Report specially for Cucumber. This plugin is used to simple out the implementation of  Extent Report  in  Cucumber Framework .  We are creating a maven project to implement the integration of our plugin with cucumber 1. Create new maven project in any tool eclipse/sts/intellij 2. Open pom.xml and update below entries. Step 1 : Add Cucumber Extent Reporter library to Maven Project Add  cucumber-extentsreport <dependency>      <groupId> com.vimalselvam </groupId>      <artifactId> cucumber-extentsreport </artif...

java: You aren't using a compiler supported by lombok, so lombok will not work and has been disabled.

  In order to make projects compile with the existing builds of Lombok processor, as a workaround you can use the flag -Djps.track.ap.dependencies=false which should be added to File | Settings | Build, Execution, Deployment | Compiler | Build process VM options field. This will disable collection of dependencies specified by an annotation processor when Filer methods are called

Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.2.3.RELEASE:repackage failed: Unable to find main class

Solutions:  Solution 1 : You needed to change the packaging parameter to jar from pom. Also, the repositories , pluginRepositories , the maven-compiler-plugin and the spring-boot-maven-plugin's version and executions weren't needed. Solution 2:  Try mvn install and see if it works Solution 3: Preview: <properties> <!-- The main class to start by executing java -jar --> <start-class> com.mycorp.starter.HelloWorldApplication </start-class> </properties> Solution 4: Enable the main() method in your Application.java. Configure spring-boot-maven-plugin to specify the class with the main class (Spring should find it anyway if you have one, but good to be explicit): Preview: <plugin> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-maven-plugin </artifactId> <version> ${spring-boot-version} </version>...