Terraform :4

DevOps Classroom Series – 01/Mar/2021

Terraform contd..

  • Till now we had create a vpc and now lets continue to create subnets Preview
  • Lets see how to create a subnet manually Preview Preview Preview
  • Authenticate terraform using shared credentials file
    • Navigate to ~/.aws/ and edit the credential file and make following entries
    [default] aws_access_key_id = youraccesskey aws_secret_access_key = yoursecretkey
  • Now lets use terraform resource in terraform aws provider to create subnet Refer Here for the documentation
  • Lets add one subnet Refer Here for the changeset and execute the command to create terraform plan file Preview Preview Preview
  • Now we have two kinds of files
    • State file: Whenever terraform creates resources it will store the information of the resources created in the .state file in the same directory where .tf files are present
    • Plan file: Whenver we would apply terraform .tf to create infra, terraform will create the plan after verifying with the provider to understand what has to be created
  • Now lets create web2 subnet Refer Here Preview
  • Now our template does the things which are fixed as the region, cidr ranges and az’s are hardcoded, So we need to parametrize these values, so that user has option to change region, cidr ranges and az’s
  • Terraform has a concept called as variable which help in parametrizing. There are three kinds of variables
    • Input Variables: They serve as parameter
    • Output Values: They are return values
    • Local Values: They are for using variables with in template which cannot be set by user
  • Input Variables Refer Here for the official documentation
  • Refer Here for the changeset containing the variable for a region
  • Refer Here for the changeset containing changes to add cidr block as variable
  • In Terraform, we give directory as input and terraform will read all the .tf files in that directory to create infra, so lets create multiple tf files
    • provider.tf: to have provider information
    • variables.tf: to have input variables defined
    • main.tf: to have resources to be created
  • Lets try to create plan Preview
  • Now destroy whatever you have created
terraform destroy .

  • Lets try to understand our next steps
    • How to use the looping constructs
    • How to use terraform functions to calcuate cidrs of subnets
    • How to use terraform functions to dynamically allocate AZ’s
    • How to pass different values to variables
  • Note:
    • To set the access key and secret key in the shared credentials file install aws cli and configure the access key and secret key Refer Here