IamEddie / advanced-terraform

Advanced Terraform repository

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Advanced Terraform

This is the repository for the LinkedIn Learning course Advanced Terraform. The full course is available from LinkedIn Learning.

Advanced Terraform Terraform simplifies and accelerates the configuration of cloud-based environments. DevOps engineers looking to use Terraform in the real world can start by learning how to work with the Terraform CLI and the HashiCorp Configuration Language (HCL). In this course, David Swersky covers these concepts and more, helping you go beyond the basics with this powerful infrastructure as code solution. Using practical use cases, David shows how to manage and automate your infrastructure with Terraform. He steps through how to analyze an existing application running in a client's data center, and design a Terraform configuration that supports the application in AWS. He also goes over advanced concepts, including how to set the values of variables in a Terraform configuration. Plus, he steps through how to develop an application infrastructure with Terraform, create an infrastructure CI/CD pipeline using GitHub and Terraform Cloud, and more.

Instructions

This repository has a folder for each of the videos in the course. The naming convention is [CHAPTER]_[NAME], e.g.: 01_05_base

Installing

  1. To use these exercise files, you must have the following installed:

    • [list of requirements for course]
    • create AWS IAM user with programatic access and keep safe of the keys
    • install AWS CLi
    • aws configure...
    • aws ec2 create-key-pair --key-name tf_key --query 'KeyMaterial' --profile demo --output text >tf_key.pem
    • aws sts get-caller-identity --profile demo
  2. Fork this repository to your own account

  3. Clone your fork to your local machine using the terminal (Mac), CMD (Windows), or a GUI tool like SourceTree.

  4. List of resources(AWS Infrastructure) to create for use case of a web application with ec2 instances and a MySQL DB

  • vpc
  • Elastic Load Balancer
  • ec2 instances
  • subnets(2 subnets)
  • Internet Gatway for internet facing application
  • security groups
  • MySQL DB
  1. Copy the created tf_key.pem into the 01_05_base folder
  2. Make a copy of the terraform.tfvars.backup file and paste it into the 01_05_base folder
  3. Rename the terraform.tfvars.backup to terraform.tfvars

.gitignore contains important files that we don't want t be commited to the public repository

  1. cd into 01_05_base folder
  2. run $ terraform init
  3. run $ terraform plan -out=s1.tfplan
  4. run $ terraform graph and cpy to paste on http://webgraphviz.com/ to see a graph 12: $ terraform show s1.tfplan #to see a plan generated by someone in the console
  5. $ terraform show -json s1.tfplan #shows macine readable json
  6. $ terraform apply s1.tfplan #to create the resources
  7. $ chmod 400 tf_key.pem
  8. $ ssh -i "tf_key.pem" ec2-user@ec2-34-244-223-54.eu-west-1.compute.amazonaws.com
  9. By default show command displays the state
  • $ terraform show
  1. Review the created instance #useful for scripting
  • $ aws ec2 describe-instances --query "Reservations[].Instances[].InstanceId" --output table --profile demo
  1. Clean/Destroy the created resources
  • $ terraform destroy

Commands

$ terraform plan -var deploy_environment=PROD $ terraform console #starts a repl(read, evaluate, print, look) => 5 != 5 ? "foo" : "bar" #if condition is true, it prints "foo" but if false, it prints "bar"(turnery operator)

  • aws_instance=node_instances.*.public_dns #the * asteristik is calld a sprat, to regenerate count
  • "${data.aws_iam_user.terraform.arn}" #string intrapolation to allow the injection the resources of the terraform user $ terraform plan -out=backend.tfplan

03_05-06_multi_environment commands

$ terraform init ../../manifests $ terraform plan -out=s1.tfplan ../..manifests View the resources

  1. $ export AWS_PAGER=""
  2. $ aws ec2 describe-instances
    --filters Name=tag-key,Values=Name
    --query 'Reservations[].Instances[].{Instance:InstanceId,AZ:Placement.AvailabilityZone,Name:Tags[?Key=='Name']|[0].Value,Environment:Tags[?Key=='environment']|[0].Value}'
    --output table

Terraform Cloud

  • You must have an account on app.terraform.io
  1. Navigate to the folder
  2. $ terraform login

Instructor

David Swersky

DevOps and Enterprise Architect with 20+ years of IT experience

Check out some of my other courses on LinkedIn Learning.

About

Advanced Terraform repository

License:Other


Languages

Language:HCL 100.0%