DevOps Classroom Series – 08/Oct/2020

Terraform Configuration Language

  • Every terraform configuration file will have .tf extension.
  • When we run terraform commands it will check for all the .tf files in the directory to create infrastructure.
  • The Terraform has a custom configuration language (DSL) which terraform call as Terraform Configuration Language
  • Refer Here for the official documentation of Terraform Configuration Language.
  • In Terraform our aim to create infrastructure. Each element of the infra is considered to be resource.
  • Terraform considers resources as Configuration blocks. To create resources we need provider which is also other Configuration block. The Block syntax is
<BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
  # Block body
  <IDENTIFIER> = <EXPRESSION> # Argument
}
  • Blocks when executed by terraform will return some out put which will be referred as Attributes
  • Refer Here for Resources Block and Refer Here for Provider Blocks official documentation
  • Provider Configuration Block:
    • Providers are plugins and they need to be downloaded (bcoz they are not part of installation) & there can be multiple versions of providers.
    • Downloading providers is a one time job per terraform configuration, where we execute init to download necessary providers into .terraform folder
  • Resources Configuration:
    • The Resource Types are obtained depending on provider, so in the configuration block we mention resource type and then the name of the resource (for referring in terraform template)

Today lets create a AWS VPC with 4 subnets

  • Lets create an aws VPC with address range or cidr (192.168.0.0/16)
  • Then lets add the following subnets to the vpc
    • web 192.168.0.0/24
    • app 192.168.1.0/24
    • db 192.168.2.0/24
    • mgmt 192.168.3.0/24
  • Manual creation steps: Navigate to vpc and create a vpc from vpc(s) section Preview Preview Preview Preview
  • Try to create other subnets by following the same approach
  • Lets Set the Environmental variables to hold access key and secret
    • Linux: Use export <ENV-NAME> = VALUE
    • Windows: Use $env:<ENV-NAME>= VALUE
  • Execute the commands after making the changes in the terraform configuration as mentioned Over Here
terraform init
terraform validate .
terraform apply .

Preview Preview

  • Now lets try to add subnet. While creating subnet as mentioned in the above images, we need to pass vpc id.
  • To Get VPC Lets look at attributes section Refer Here
  • To use Attributes refer Accessing Resource Attributes section in the resource block docs Refer Here
<RESOURCE TYPE>.<NAME>.<ATTRIBUTE>
  • If we apply this to get vpc id
aws_vpc.myvpc.id
  • Refer Here for aws subnet reference
  • So the changes will appear as Refer Here
  • Now lets apply the template after validating
terraform validate .
terraform apply .

Preview Preview

  • Now lets add other 3 subnets Refer Here for changes.
  • Now apply and verify the resources created Preview Preview

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin