Terraform_Configuration_Exacuation

Terraform_Configuration_Exacuation

Terraform configuration and testing.

Infrastructure as a code

Deployment on Windows system.

  • Download terraform from "https://www.terraform.io/downloads.html" and create "c:\terraform" and extract it.

  • set PATH in environmental variable and add "c:\terraform\" in PATH.

C:\>mkdir terraform
C:\>cd terraform
C:\terraform>dir
04/06/2019  10:31 PM    <DIR>          .
04/06/2019  10:31 PM    <DIR>          ..
03/13/2019  12:07 AM        86,698,496 terraform.exe

Create project directory and initilize terraform plugin. To do that first create on sample tf file.

C:\Users\feenix>mkdir terraform
C:\Users\feenix>cd terraform

Create deployment configuration file with "*.tf" extansion
In this eaxmple i created "create_ec2.tf" with below infomation

provider "aws" {
 access_key = "XXXXXXXXXXXXXX"
 secret_key = "XXXXXXXXXXXXXXXXXX"
 region = "us-east-2"
}

resource "aws_instance" "Feenix_test" {
 ami = "ami-0b500ef59d8335eee"
 instance_type = "t2.micro"
  tags {
    name = "feenix_web"
    }
 }

Update access_key and secret_key.

First we need to initilize all plugin which is required.
run "terraform init" in side project deirectory(C:\Users\feenix\terraform).

 C:\Users\feenix\terraform>terraform init

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "aws" (2.5.0)...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.aws: version = "~> 2.5"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Check all update done by "init"

C:\Users\feenix\terraform>dir
 Volume in drive C is OSDISK
 Volume Serial Number is E096-A98D

 Directory of C:\Users\feenix\terraform

04/06/2019  11:25 PM    <DIR>          .
04/06/2019  11:25 PM    <DIR>          ..
04/06/2019  10:53 PM    <DIR>          .terraform
04/06/2019  11:07 PM               285 create_ec2.tf
04/06/2019  11:11 PM               318 terraform.tfstate
04/06/2019  11:11 PM             3,902 terraform.tfstate.backup
               3 File(s)          4,505 bytes
               3 Dir(s)  55,487,930,368 bytes free

Now run "terraform plan" to check all input tf file and required plugin are ok. Its a type of dry run.

C:\Users\feenix\terraform>terraform plan
.
.
Plan: 1 to add, 0 to change, 0 to destroy.
.
.

In the output we can see one action to add.

Now if everything is ok then apply the terraform script.

C:\Users\feenix\terraform>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.Feenix_test
      id:                           <computed>
      ami:                          "ami-0b500ef59d8335eee"
      arn:                          <computed>
      associate_public_ip_address:  <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.Feenix_test: Still creating... (40s elapsed)
aws_instance.Feenix_test: Creation complete after 44s (ID: i-0432c53da5e1f070a)

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

In the output its return "ID: i-0432c53da5e1f070a" which is EC2 instance ID. You can check on AWS consol.

We can show all configuration value with below command.

C:\Users\feenix\terraform>terraform show
aws_instance.Feenix_test:
  id = i-0432c53da5e1f070a
  ami = ami-0b500ef59d8335eee
  arn = arn:aws:ec2:us-east-2:827132459049:instance/i-0432c53da5e1f070a
  associate_public_ip_address = true
  availability_zone = us-east-2b
  cpu_core_count = 1

Delete or role back all changes. 

C:\Users\feenix\terraform>terraform destroy   

      

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *