Skip to content

Instantly share code, notes, and snippets.

@Sanjogsharma
Forked from ravsau/terraform-lab.md
Last active February 10, 2019 18:04
Show Gist options
  • Select an option

  • Save Sanjogsharma/27389f6de4733d1e95d3e12346a20265 to your computer and use it in GitHub Desktop.

Select an option

Save Sanjogsharma/27389f6de4733d1e95d3e12346a20265 to your computer and use it in GitHub Desktop.
Terraform lab

In this tutorial, I will use Terraform to deploy an EC2 Instance. Terraform is an infrastructure as code software tool created by Hashicorp. We will refer to Hashicorp's AWS documentation throughout this tutorial.

Find the code and steps for this Lab at my github page linked here.

What is Terraform?

Terraform can build, modify, and version infrastructure safely and efficiently. Terraform can manage existing and popular service providers like AWS, Azure, Google Cloud. It can also handle custom in-house solutions.

Terraform is similar to Cloudformation, but they have some significant differenes.

Steps to provision an EC2 instance

  1. Download the terraform binary file
  • If you are a Mac user with homebrew installed:
    • type the following on your terminal brew install terraform
    • Skip to step 5
  1. Extract the zip file.
  2. Identify Terraform's binary executable file
  3. Ensure that the terraform binary file is available on the PATH.
On Mac and Linux:

Type the following on shell or terminal in folder where terraform binary is extracted

echo $"export PATH=\$PATH:$(pwd)" >> ~/.bash_profile
source ~/.bash_profile
On Windows:

Follow this tutorial to add Terraform to PATH

  1. Create a new directory, (it can be named anything) and inside the directory, type the following command:
mkdir terraform-july && cd terraform-july
  1. Paste the following code to a file called ec2.tf
  • Terraform filenames end in .tf, the best practice is to have descriptive name floowed by ".tf".

Minimal Viable Configuration

provider "aws" {
  access_key = "ACCESS_KEY_HERE"
  secret_key = "SECRET_KEY_HERE"
  region     = "us-east-1"
}

resource "aws_instance" "example" {
  ami           = "ami-2757f631"
  instance_type = "t2.micro"
}

Note:

Replace the access_key and secret_access with your AWS IAM user credentials with enough permissions attached. You can go to IAM console on AWS to do this. First, go to the IAM management console iam

Then Click on the user's name and navigate to the security credentials tab. Click create access keys iam

Either download the csv file or, click show keys. Now you have both the access_key and secret_key required for the terraform code above. iam

If you've setup the AWS CLI and have credentials stored, you may skip the credential portion. Hashicorp recommends: "If you simply leave out AWS credentials, Terraform will automatically search for saved API credentials (for example, in ~/.aws/credentials) or IAM instance profile credentials. This option is much cleaner for situations where tf files are checked into source control."

  1. Initialize the working directory for terraform:
terraform init

"The terraform init command is used to initialize a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control. It is safe to run this command multiple times."

  1. Provision the ec2 with this command:
terraform apply
Verificaion and cleanup:

Login to the AWS management console and navigate to the EC2 management console. Verify if an instance got provisioned. Once that's done, from your terminal, command prompt or shell, destory the resources:

terraform destroy

That's it! You just installed Terraform and used it to provison an EC2 instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment