Sunday, September 10, 2017

Launching AWS instance using Terraform


Terraform is an powerful and user friendly infrastructure as a code(IaC) tool. Using this tool you can do the infrastructure automation in hybrid cloud environment.
In this lab I am going to create and destroy t2.micro instance in AWS cloud using my terraform engine configured in my laptop.

I need my AWS account details (Private key,access key and security key to connect my AWS account)
Terraform engine
terraform file (contains my AWS instance configuration)

G:\Lab-Test\terraform>dir
 Volume in drive G is New Volume
 Volume Serial Number is 4E18-7F0B

 Directory of G:\Lab-Test\terraform

09/09/2017  11:51 PM    <DIR>          .
09/09/2017  11:51 PM    <DIR>          ..
09/09/2017  10:51 PM    <DIR>          .terraform
09/09/2017  11:46 PM             1,328 moduleone.tf
09/09/2017  10:06 PM             1,692 rengsaws.pem
09/06/2017  08:23 PM        55,886,848 terraform.exe
               3 File(s)     55,889,868 bytes
               3 Dir(s)  78,159,544,320 bytes free


Terraform file for AWS instance

##################################################################################

# PROVIDERS

##################################################################################

provider "aws" {

 access_key = "AxxxxxxxxxxxxxxxXXX"

 secret_key = "XXXXXXXXXXXXXXXXxxxX"

 region = "us-east-2"

}

###########################################################################

# RESOURCES

##################################################################################

resource "aws_instance" "nginx" {

ami = "ami-ea87a78f"

instance_type = "t2.micro"

key_name = "rengsaws"

 connection {

 user = "ec2-user"

 private_key = "rengsaws.pem"

  }

  provisioner "remote-exec" {

    inline = [

      "sudo yum install nginx -y",

      "sudo service nginx start"

    ]

  }

}

##################################################################################

# OUTPUT

##################################################################################

 output "aws_instance_public_dns" {

    value = "${aws_instance.nginx.public_dns}"

}

you may store your AWS credential and variables in separate variable file "<filename>.tfvars" or any ".auto.tfvars". In this examples I applied all variables and values in terraform file itself. 

Testing my Terraform configuration


G:\Lab-Test\terraform>terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

------------------------------------------------------------------------

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.nginx
      id:                           <computed>
      ami:                          "ami-c58c1dd3"
      associate_public_ip_address:  <computed>
      availability_zone:            <computed>
      ebs_block_device.#:           <computed>
      ephemeral_block_device.#:     <computed>
      instance_state:               <computed>
      instance_type:                "t2.micro"
      ipv6_address_count:           <computed>
      ipv6_addresses.#:             <computed>
      key_name:                     "rengsaws"
      network_interface.#:          <computed>
      network_interface_id:         <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>
      tenancy:                      <computed>
      volume_tags.%:                <computed>
      vpc_security_group_ids.#:     <computed>


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

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

AWS instance Create

G:\Lab-Test\terraform>terraform apply
aws_instance.nginx: Creating...
  ami:                          "" => "ami-ea87a78f"
  associate_public_ip_address:  "" => "<computed>"
  availability_zone:            "" => "<computed>"
  ebs_block_device.#:           "" => "<computed>"
  ephemeral_block_device.#:     "" => "<computed>"
  instance_state:               "" => "<computed>"
  instance_type:                "" => "t2.micro"
  ipv6_address_count:           "" => "<computed>"
  ipv6_addresses.#:             "" => "<computed>"
  key_name:                     "" => "rengsaws"
  network_interface.#:          "" => "<computed>"
  network_interface_id:         "" => "<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>"
  tenancy:                      "" => "<computed>"
  volume_tags.%:                "" => "<computed>"
  vpc_security_group_ids.#:     "" => "<computed>"
aws_instance.nginx: Still creating... (10s elapsed)
aws_instance.nginx: Still creating... (20s elapsed)
aws_instance.nginx: Provisioning with 'remote-exec'...
Error applying plan:

1 error(s) occurred:

* aws_instance.nginx: 1 error(s) occurred:
* Failed to read key "rengsaws.pem": no key found
Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.-

-------------
 Checking my AWS console, It is creating a new instance and its started with given configuration.


Destroy AWS instance

G:\Lab-Test\terraform>terraform destroy
aws_instance.nginx: Refreshing state... (ID: i-0e668973aee568db5)

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

Terraform will perform the following actions:

  - aws_instance.nginx


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

Do you really want to destroy?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

aws_instance.nginx: Destroying... (ID: i-0e668973aee568db5)
aws_instance.nginx: Still destroying... (ID: i-0e668973aee568db5, 10s elapsed)
aws_instance.nginx: Still destroying... (ID: i-0e668973aee568db5, 20s elapsed)
aws_instance.nginx: Still destroying... (ID: i-0e668973aee568db5, 30s elapsed)
aws_instance.nginx: Still destroying... (ID: i-0e668973aee568db5, 40s elapsed)
aws_instance.nginx: Destruction complete after 41s


Destroy complete! Resources: 1 destroyed.

Verify again the AWS console, Terraform is removing my instance in AWS cloud


2 comments:

AWS Autoscaling demo lab Configuring VPC, Public/Private subnets, Internet Gateway (IGW), NAT gateway, etc.,   Crea...