Skip to content

san99tiago/terraform-playground

Repository files navigation

🔨 TERRAFORM-PLAYGROUND 🔨

Learning Terraform with AWS examples.

This repository covers a simple-learning approach for Terraform (90%) and Packer (10%) with AWS.

We will cover important Infrastructure as Code (IaC) concepts that support the best DevOps practices.


General Overview of this Repository 🏊

This repository is my own learning-playground for Terraform-based projects. It has different examples and the overall idea is to have fun with Infrastructure as Code projects towards AWS.

The examples are simple, but they are a good way to learn terraform from "scratch".

Dependencies 🚦

My advice is to primary understand the way Terraform works with basic tutorials, and then try to develop amazing project ideas based on these examples.

Software dependencies (based on project)

  • Visual Studio Code
    Visual Studio Code is my main code editor for high-level programming. This is not absolutely necessary, but from my experience, it gives us a great performance and we can link it with Git and GitHub easily.

  • Terraform
    Terraform is a way to codify, automate, provision and version cloud APIs into declarative configuration files. The main purpose of Terraform is to create IaC for provisioning amazing infrastructure to different clouds.

  • Packer
    Packer allows us to create identical machine images for multiple platforms from a single source configuration. We will use it to create AWS AMIs to launch custom EC2 images.

Important Terraform Remarks 🎧

Some important notes to keep in mind for the usage of Terraform are:

Install Terraform Bin/Exe

To install Terraform, please go to Download Terraform and install the specific version for your OS. Remember to add the specific path to the binary, so that Terraform CLI can be used correctly.

Terraform Files

Terraform files usually have the .tf extension and there is also the .tf.json convention for a JSON-based variant of the language. See extra details on Terraform Files and Directories.

Terraform State

One of the most important files are the terraform.tfstate and terraform.tfstate.backup, which determine the state and they can be stored locally or remotely. These files contain the state of the infrastructure and configurations related to the IaC. They also maps real world resources to the configuration and keep track of extra relevant information. You can learn more about the state in Terraform State

Terraform Dependency Lock

Another important file is the .terraform.lock.hcl, which is the Dependency Lock File and it tracks the provider dependencies.

Terraform Commands

  • Initialize Terraform directory and downloads providers:

    terraform init
  • Initialize Terraform directory and do not download providers:

    terraform init --get-plugins=false
  • Download and update root modules:

    terraform get
  • Create an execution plan:

    terraform plan
  • Create an execution plan only for a targeted resource:

    terraform plan -target="random_cool_resource.random_cool_resource_name"
  • Create a destroy plan:

    terraform plan -destroy
  • Execute (apply) changes in the real environment:

    terraform apply
  • Execute (apply) changes in the real environment with specific variable file:

    terraform apply --var-file dev.tfvars.json
  • Execute (apply) changes only for a targeted resource:

    terraform apply -target="random_cool_resource.random_cool_resource_name"
  • Destroy all resources:

    terraform destroy
  • Destroy only a targeted resource:

    terraform destroy -target="random_cool_resource.random_cool_resource_name"
  • Refresh the state of Terraform state file with real environment:

    terraform refresh
  • Terraform workspace commands:

    terraform workspace new
    terraform workspace select
    terraform workspace list
    terraform workspace show
    terraform workspace delete
  • Format Terraform files into HCL canonical structure:

    terraform fmt
  • Validate Terraform files:

    terraform validate
  • Create graph of resources and its dependencies:

    terraform graph
    # Copy the output to "http://webgraphviz.com"
  • Enumerates all outputs from the root module:

    terraform output
  • Enumerates an specific output from the root module:

    terraform output <specific_output>
  • Terraform state commands:

    terraform state list
    terraform state list aws_instance.my_cool_ec2
    terraform state show aws_instance.my_cool_ec2
    terraform state pull
    terraform state push
    terraform state mv
    terraform state rm

Important Packer Remarks 🎧

Some important notes to keep in mind for the usage of Packer are:

Install Packer Bin/Exe

To install Packer, please go to Download Packer and install the specific version for your OS. Remember to add the specific path to the binary, so that Packer CLI can be used correctly.

How the AMI is created

When the AMI process is being executed, it usually creates an EC2 instance called Packer Builder. Keep this in mind if you want to do extra debugging related to the AMI creation on AWS.

Packer Commands

  • Validate a JSON file that creates an AMI:

    packer validate cool-ubuntu-aws-ami.json
  • Build the AMI with Packer and a JSON file:

    packer build cool-ubuntu-aws-ami.json

Usage 💫

All projects are really well commented and most of them have specifications and remarks for their purpose and I/O.
I will be uploading most of the files, and try to keep it as clean as possible.

Special thanks 🎁

  • Thanks to all contributors of the great OpenSource projects that I am using.

Author 🎹

Santiago Garcia Arango

Senior DevOps Engineer passionate about advanced cloud-based solutions and deployments in AWS. I am convinced that today's greatest challenges must be solved by people that love what they do.