Getting started with Infrastructure as code (IaS) with Terraform
Hi, I have been a full-stack(Frontend heavy) developer for about 5 years now. And I made a decision to delve into the world of cloud computing and devOps. Hence the reason I started learning about AWS and terraform in recent weeks. I will be sharing my learnings here as I progress on this path. In this article we'd go through how you can get started with cloud development, as well as some hands on AWS and Infrastructure management tool known as terraform. You might be wondering, what the heck is terraform and what not, don't fret, we'd dive in a bit.
First of all l what the heck is terraform and how what does it do ?
Simply put terraform is a software, you can also call it a tool that enables developers manage infrastructure but with code. Hence the reason it is called Infrastructure as code (IaS) management software. We'd go through how you can set it up on your machine in a bit.
Terraform & AWS CLI Installation
A) Prerequisites
Install Terraform CLI
Install AWS CLI
Install VS Code Editor
Install AWS CLI
Install HashiCorp Terraform plugin for VS Code - recommended
B) MACOS - Terraform Install
Download Terraform MAC
Install CLI
Unzip the package
Copy binary zip file to a folder
mkdir /Users//Documents/terraform-install COPY Package to "terraform-install" folder
Unzip
unzip unzip terraform_1.0.10_darwin_amd64.zip
Copy terraform binary to /usr/local/bin
echo $PATH mv terraform /usr/local/bin
Verify Version
terraform version
To Uninstall Terraform (NOT REQUIRED)
rm -rf /usr/local/bin/terraform
C) MACOS - Install VSCode Editor and terraform plugin
D) MACOS - Install AWS CLI
Install AWS CLI V2
curl "awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /
which aws
aws --version
Uninstall AWS CLI V2 (NOT REQUIRED)
which aws
ls -l /usr/local/bin/aws
sudo rm /usr/local/bin/aws
sudo rm /usr/local/bin/aws_completer
sudo rm -rf /usr/local/aws-cli
E) MACOS - Configure AWS Credentials
Pre-requisite: Should have AWS Account.
Role: -If your terraform server is in the cloud, then create a role and attach the role to your server.
Generate Security Credential s using AWS Management Console
Go to Services -> IAM -> Users -> "Your-Admin-User" -> Security Credentials -> Create Access Key Configure AWS credentials using SSH Terminal on your local desktop
Configure AWS Credentials in command line
$ aws configure
AWS Access Key ID [None]: AKIASUF7DEFKSIAWMZ7K
AWS Secret Access Key [None]: WL9G9Tl8lGm7w9t7B3NEDny1+w3N/K5F3HWtdFH/
Default region name [None]: us-west-2
Default output format [None]: json
Verify if we are able list S3 buckets
aws s3 ls
Verify the AWS Credentials Profile
cat $HOME/.aws/credentials
F) Windows OS - Terraform & AWS CLI Install
Unzip the package
Create new folder binaries
Copy the terraform.exe to a binaries
Set PATH in windows How to set the windows path: Windows 8/10 In Search, search for and then select:
System (Control Panel) Click the Advanced system settings link. Click Environment Variables. In the section System Variables find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.Install AWS CLI
Terraform install on windows using a packet manager
- Install terraform on windows using the windows package manager(Use power shell and install as administrator). $ choco install terraform
G) Linux OS - Terraform & AWS CLI Install
Install Terraform on Ubuntu:
$sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl $curl -fsSL apt.releases.hashicorp.com/gpg | sudo apt-key add - $sudo apt-add-repository "deb [arch=amd64] apt.releases.hashicorp.com $(lsb_release -cs) main" $sudo apt-get update && sudo apt-get install terraform
Install Terraform on RHEL:
Install aws cli sudo yum update -y sudo yum install curl unzip wget -y
curl "awscli.amazonaws.com/awscli-exe-linux-x86_6.." -o "awscliv2.zip" unzip awscliv2.zip sudo ./aws/install
Install Terraform
a) Download binary
sudo yum update -y sudo yum install wget unzip -y sudo wget releases.hashicorp.com/terraform/1.4.4/terr.. sudo unzip terraform_1.1.4_linux_amd64.zip -d /usr/local/bin terraform -v
b) Install from hashicorp repo
sudo yum install -y yum-utils sudo yum-config-manager --add-repo rpm.releases.hashicorp.com/RHEL/hashicorp.r.. sudo yum -y install terraform
Terraform Command Basics
Step-01: Terraform configuration files
Terraform uses declarative syntax to describe your Infrastructure as Code (IaC) infrastructure and then persist it in configuration files that can be shared, reviewed, edited, versioned, preserved, and reused. Terraform configuration files can use either of two formats: Terraform domain-specific language (HashiCorp Configuration Language format [HCL]), which is the recommended approach, or JSON format if the files need to be machine-readable. Configuration files that use the HCL format end with the .tf file extension; Those using JSON format end with the .tf.json file extension. The Terraform format is human-readable, while the JSON format is machine readable
Step-02: Review terraform manifest for EC2 Instance
Pre-Conditions-1: Ensure you have default-vpc in that respective region Pre-Conditions-2: Ensure AMI you are provisioning exists in that region if not update AMI ID Pre-Conditions-3: Verify your AWS Credentials in $HOME/.aws/credentials
# Terraform Settings Block
terraform {
required_version = "~> 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0" # Optional but recommended in production
}
}
}
# Provider Block
provider "aws" {
profile = "default"
region = "us-west-2"
}
# Resource Block
resource "aws_instance" "ec2demo" {
ami = "ami-0e5b6b6a9f3db6db8"
instance_type = "t2.micro"
}
Step-03: Terraform Core Commands
Initialize Terraform
terraform init
This command is used to initialize the terraform project and downloads all the required dependencies from remote sources. Just like you do git init when working with git
Terraform Validate
terraform validate
This command verifies if there configuration is valid and there are are no errors in the configuration files
Terraform Plan
terraform plan
This command verifies resources that are to be created, updated or destroyed
terraform apply
terraform apply
Terraform apply is used to create the AWS EC2 instance
Step-04: Verify the EC2 Instance in AWS Management Console
Go to AWS Management Console -> Services -> EC2
Verify newly created EC2 instance
Step-05: Destroy Infrastructure
terraform destroy
This command is used to terminate the EC2 instance initially created in AWS