Skip to content

Introduction to basic Terraform commands and how to create, verify, and destroy an EC2 instance.

Notifications You must be signed in to change notification settings

gerardodavidlopezcastillo/TF_AWSCLI-CommandsSyntax_Public

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

TF_AWSCLI-CommandsSyntax_Public

Description Details:

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.

Terraform & AWS CLI Installation

Step-01: Introduction

  • Install Terraform CLI
  • Install AWS CLI
  • Install VS Code Editor
  • Install HashiCorp Terraform plugin for VS Code

Step-02: MACOS: Terraform Install

# 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

Step-03: MACOS: IDE for Terraform - VS Code Editor

Step-04: MACOS: Install AWS CLI

# 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

Step-05: MACOS: Configure AWS Credentials

  • 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 

Step-06: WindowsOS: Terraform & AWS CLI Install

Step-07: LinuxOS: Terraform & AWS CLI Install

Terraform Command Basics

Step-01: Introduction

  • Understand basic Terraform Commands
    • terraform init
    • terraform validate
    • terraform plan
    • terraform apply
    • terraform destroy

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_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"
}

Step-03: Terraform Core Commands

# 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 

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

# Destroy EC2 Instance
terraform destroy

# Delete Terraform files 
rm -rf .terraform*
rm -rf terraform.tfstate*

Step-08: Conclusion

  • Re-iterate what we have learned in this section
  • Learned about Important Terraform Commands
    • terraform init
    • terraform validate
    • terraform plan
    • terraform apply
    • terraform destroy

About

Introduction to basic Terraform commands and how to create, verify, and destroy an EC2 instance.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages