Terraform

Starting

Terraform reads all files in a directory to build a project. By convention they end .tf

Terrraform used plug in modules to communicate with a cloud provider, eg. AWS, the init phase loads these in a .terraform directory.

Phases of build:-

$ cd to target directory
$ terraform init
$ terraform plan
$ terraform apply
$ terraform destroy

Simple example

andrew@puppet:~/terraform/ec2-Build$ cat ec2-builder1.tf 
# test build for simple ec2-instance
# Andrew Stringer 20/09/2018

provider "aws" {
  assume_role {
    role_arn = "arn:aws:iam::1234567890:role/sandbox"

  }
  access_key = "YourKeyHere"
  secret_key = "YourSecretHere"
  region     = "eu-west-1"  (Ireland)
}


resource "aws_instance" "example" {
  ami           = "ami-0bdb1d6c15a40392c"
  instance_type = "t2.micro"
}



andrew@puppet:~/terraform/ec2-Build$

This is built and destroyed thus:-

andrew@puppet:~/terraform/ec2-Build$ terraform init

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

andrew@puppet:~/terraform/ec2-Build$ 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.example
      id:                           <computed>
      ami:                          "ami-0bdb123456789"
      arn:                          <computed>
...edited...
      ephemeral_block_device.#:     <computed>
      get_password_data:            "false"
      instance_state:               <computed>
      instance_type:                "t2.micro"
...edited...
      volume_tags.%:                <computed>
      vpc_security_group_ids.#:     <computed>


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

andrew@puppet:~/terraform/ec2-Build$ 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.example
      id:                           <computed>
...edited...
      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.example: Creating...
  ami:                          "" => "ami-0bdb123456789"
  arn:                          "" => "<computed>"
  associate_public_ip_address:  "" => "<computed>"
...edited...
aws_instance.example: Creation complete after 32s (ID: i-07e6c7dec9c95f167)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
andrew@puppet:~/terraform/ec2-Build$




andrew@puppet:~/terraform/ec2-Build$ terraform destroy
aws_instance.example: Refreshing state... (ID: i-07e6c7dec9c95f167)

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.example


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.example: Destroying... (ID: i-)
aws_instance.example: Still destroying... (ID: i-07e6c7dec9c95f167, 10s elapsed)
...edited...
aws_instance.example: Still destroying... (ID: i-07e6c7dec9c95f167, 1m0s elapsed)
aws_instance.example: Destruction complete after 1m1s

Destroy complete! Resources: 1 destroyed.
andrew@puppet:~/terraform/ec2-Build$ 

rb/terraform.txt · Last modified: 20/09/2018 12:36 by andrew