Terraform :3

DevOps Classroom Series – 25/Feb/2021

Scenario: Lets create the following architecture in AWS

  • The first version of the architecture to be created using terraform looks as shown below Preview
  • So before we start working on terraform, we should know how to manually create it. Preview Preview Preview Preview Preview
  • Now lets try to acheive the same from terraform
  • Create a new folder and in that folder create a terraform file main.tf
  • In todays example lets not hard code the access_key and secret_key
  • So lets create Environmental Variables
    • AWS_ACCESS_KEY_ID
    • AWS_SECRET_ACCESS_KEY
  • To set environmental variables in Linux
export AWS_ACCESS_KEY_ID="anaccesskey"
export AWS_SECRET_ACCESS_KEY="asecretkey"

  • To set environmental variables in windows powershell
$env:AWS_ACCESS_KEY_ID="youraccesskey"
$env:AWS_SECRET_ACCESS_KEY="your secret key"

  • Now perform init
terraform init

  • Validate the template
terraform validate .

  • Now lets create the resources by executing terraform apply . Preview Preview
  • Now lets verify the vpc manually in the aws console Preview
  • Now lets just apply terraform apply . again Preview
  • Terraform will apply only when there is need to create resources, if the resources are already created and have the same values as you mentioned, terraform will not do anything.
  • Refer Here for the changes
  • Now lets make small change in aws vpc to enable dns support Refer Here for the changes Preview Preview Preview
  • Now lets view the contents of the folder, some new files got generated during apply Preview
  • Lets understand the importance the files generated in our next session.