Terraform (Series-3)

By Now, you have learnt most of the topics and components of Terraform. Next you should look at a simple example how terraform works. Lets Begin by simple Terraform code to create AWS EC2 instance.

Setting up and running Terraform Code to Create and deploy AWS EC2 instance.

  • Lets start by creating a folder or directory and inside that create a file main.tf and copy the below content.
# This is main.tf terraform file.

resource "aws_instance" "my-machine" {
  ami = "ami-0a91cd140a1fc148a"
  for_each  = {
      key1 = "t2.micro"
	  key2 = "t2.medium"
   }
    instance_type      = each.value
	key_name       = each.key
    tags =  {
	   Name  = each.value
	}
}

resource "aws_iam_user" "accounts" {
  for_each = toset( ["Account11", "Account12", "Account13", "Account14"] )
  name     = each.key
}
  • Create another file vars.tf and copy the below content.

#  This is var.tf terraform file.

variable "tag_ec2" {
  type = list(string)
  default = ["ec21a","ec21b"]
}
  • Finally, Create another file output.tf and copy the below content.
# This is  output.tf terraform file

output "aws_instance" {
   value = "${aws_instance.my-machine.*.id}"
}
output "aws_iam_user" {
   value = "${aws_iam_user.accounts.*.name}"
}
  • Now Initialize the terraform by running the terraform init command in same working directory where you have all the above terraform configuration files.
terraform init   # To initialize the terraform 
+ sudo terraform init --backend-config=key=aws/stage/automateinfra/terraform.tfstate
 [0m [1mInitializing modules... [0m

 [0m [1mInitializing the backend... [0m

 [0m [1mInitializing provider plugins... [0m
- Reusing previous version of cyrilgdn/postgresql from the dependency lock file
- Reusing previous version of hashicorp/aws from the dependency lock file
- Reusing previous version of hashicorp/random from the dependency lock file
- Installing hashicorp/random v3.0.1...
- Installed hashicorp/random v3.0.1 (signed by HashiCorp)
- Installing cyrilgdn/postgresql v1.11.1...
- Installed cyrilgdn/postgresql v1.11.1 (self-signed, key ID  [0m [1m3918DD444A3876A6 [0m [0m)
- Installing hashicorp/aws v3.27.0...
- Installed hashicorp/aws v3.27.0 (signed by HashiCorp)
  • Next run the terraform plan command. This command provides the blueprint of what all resources will be deployed before deploying actually.
terraform plan   
  • Validate the terraform configuration files by running the terraform validate command.
terraform validate   # To validate all terraform configuration files.
  • Now run the Terraform show command provides the human readable output of state file that gets generated only after terraform plan command.
terraform show   # To provide human-readable output from a state or plan file.
  • To list all resources within terraform state file run the terraform state list command.
terraform state list 
  • Finally use terraform apply command to provision the resources.
terraform apply  # To Actually apply the resources 
  • To provide graphical view of all resources in configuration files run terraform graph command.
terraform Graph  
  • To Destroy the resources that are provisioned using Terraform run Terraform destroy command.
Terraform Destroy   # It will destroy all your resources or the one which you specified 
  • To find out the Version of your Terraform run terraform -version command.
terraform -version  # It gives Terraform Version information
+ sudo terraform -version
Terraform v0.14.2
+ provider registry.terraform.io/cyrilgdn/postgresql v1.11.1
+ provider registry.terraform.io/hashicorp/aws v3.27.0
+ provider registry.terraform.io/hashicorp/random v3.0.1

Your version of Terraform is out of date! The latest version
is 0.14.6. You can update by downloading from https://www.terraform.io/downloads.html