DevOps Classroomnotes 15/Oct/2022

Terraform Continued

  • Terraform uses HCL (Hashicorp Configuration Language)

Problem 1: Fixed names for the resources

  • In the templates which we have created so far, we have directly specified the configuration as part of resource, so the template will not be flexible as it will try to create resources with same configuration all the time.
  • We need flexbility where the user should have option to pass/change the values while creating infrastructure.
  • Lets look at template where we have create a vpc in aws
  • Consider the following provider
provider "aws" {
    region      = "ap-south-1"
}

  • This will always choose ap-south-1 (mumbai) as the region, but if the user of this template wants a different region he/she has to change the template
  • What we want to provide is the flexibility for the user to pass any valid region for aws.
  • This can be acheived by using variables in terraform Refer Here
  • Variables can have following arguments Refer Here
  • Variable syntax
variable "<name-of-variable>" {
    type = "<type of variable>"
    default = "<default-value>"
    ...
}
  • simple types are string, number and bool
  • Lets create variables with type string for region and cidr range. Then apply the template
    Preview
    Preview
  • To pass the variables from command line Refer Here
  • Now we have executed terraform apply -var="target_region=us-west-2" -var="vpc_range=192.168.0.0/16" -auto-approve
    Preview
  • destroy the resources
  • Now lets try to use defult value
    Preview
  • Now destroy the resources
  • Passing all the variable values from commandline is not sensible, so lets assign values to all the variables in .tfvars file and apply terraform apply -var-file ".\dev.tfvars" -auto-approve
  • Refer Here for the changeset
  • Lets do the similar stuff in azure Refer Here
    Preview

Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

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

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube