This repository provides a detailed step-by-step guide to installing Terraform CLI and AWS CLI on various operating systems. From installation on macOS to configuring AWS credentials, it includes instructions for Windows and Linux. Additionally, it offers an introduction to basic Terraform commands and how to create, verify, and destroy an EC2 instance.
- Install Terraform CLI
- Install AWS CLI
- Install VS Code Editor
- Install HashiCorp Terraform plugin for VS Code
- Download Terraform MAC
- Install CLI
- Unzip the package
# Copy binary zip file to a folder
mkdir /Users/<YOUR-USER>/Documents/terraform-install
COPY Package to "terraform-install" folder
# Unzip
unzip <PACKAGE-NAME>
unzip terraform_0.14.3_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
# Install AWS CLI V2
curl "https://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
- Pre-requisite: Should have AWS Account.
- Generate Security Credentials 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]: xxxxx
AWS Secret Access Key [None]: xxxxx
Default region name [None]: us-east-1
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
- Download Terraform
- Install CLI
- Unzip the package
- Create new folder
terraform-bins
- Copy the
terraform.exe
to aterraform-bins
- Set PATH in windows
- Install AWS CLI
- Understand basic Terraform Commands
- terraform init
- terraform validate
- terraform plan
- terraform apply
- terraform destroy
- 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_providers {
aws = {
source = "hashicorp/aws"
#version = "~> 3.21" # Optional but recommended in production
}
}
}
# Provider Block
provider "aws" {
profile = "default" # AWS Credentials Profile configured on your local desktop terminal $HOME/.aws/credentials
region = "us-east-1"
}
# Resource Block
resource "aws_instance" "ec2demo" {
ami = "ami-04d29b6f966df1537" # Amazon Linux in us-east-1, update as per your region
instance_type = "t2.micro"
}
# Initialize Terraform
terraform init
# Terraform Validate
terraform validate
# Terraform Plan to Verify what it is going to create / update / destroy
terraform plan
# Terraform Apply to Create EC2 Instance
terraform apply
- Go to AWS Management Console -> Services -> EC2
- Verify newly created EC2 instance
# Destroy EC2 Instance
terraform destroy
# Delete Terraform files
rm -rf .terraform*
rm -rf terraform.tfstate*
- Re-iterate what we have learned in this section
- Learned about Important Terraform Commands
- terraform init
- terraform validate
- terraform plan
- terraform apply
- terraform destroy