DevOps Classroomnotes 20/May/2022

Terraform Contd…

  • The two major problems with the terraform configuration code which we have written are
    • secrets are directly written (access key and secret key)
    • for creating subnets we have similar resource definition used multiple times
  • Then the next set of problems are, reason for writing terraform templates are to make the infrastructure as code and reusable. But our template always
    • creates the infra in mumbai region
    • the cidr ranges are fixed
    • In my account
  • Terraform aws provider configuration:
    • Refer Here
    • Lets try to install aws cli Refer Here
    • Now execute aws configure and enter access key secret access key and region ap-south-1
  • Refer Here for the terraform script used to create the vpc.
  • To solve the problem of using similar resource definition multiple times for subnet, first we need to make the terraform script reusable.
  • In terraform to enable the parametrization of the argument values, we can use input variables Refer Here

Input Variables

  • Syntax:
variable "<var-name>" {
    type            = "<type of the variable>"
    default         = "<default value>"
    description     = "description of the variable"
    validation      = "A block to validate the values"
    sensitive       = "Sensitive information"
    nullable        = "specifies if the variable can have null value"
}
  • Refer Here for the variable defintions added
  • We need to use the variables the syntax is var.<variable-name> Refer Here for the changes
  • Now lets try to validate the terraform script
    Preview
  • To pass the variables from command line Refer Here
    Preview
  • If we have lot of variables to pass then variable defintions file is the option Refer Here.
  • Refer Here
    Preview
  • Lets add more variables and Refer Here for the changes
  • Now execute terraform apply -var-file "default.tfvars"

Leave a Reply

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

About continuous learner

devops & cloud enthusiastic learner