Wednesday, December 4, 2024

Step-by-Step Guide to Setting Up Terraform, AWS CLI, and VS Code

Programming LanguageStep-by-Step Guide to Setting Up Terraform, AWS CLI, and VS Code


Today, I’ll be sharing a detailed guide to setting up the tools you’ll need to work effectively with Terraform. Whether you’re a beginner or looking to refresh your setup, this guide will walk you through installing Terraform, configuring the AWS CLI, and setting up VS Code for smooth Infrastructure as Code (IaC) workflows.


Step 1: Install Terraform

Terraform is the backbone of your IaC journey, and setting it up is straightforward:

On Windows

  1. Download the Terraform binary from Terraform’s official website.
  2. Extract the binary to a directory (e.g., C:\Terraform).
  3. Add the directory to your system’s PATH:
    • Open System Properties > Environment Variables.
    • Edit the PATH variable and add C:\Terraform.

On macOS/Linux

  1. Install using a package manager:
   brew install terraform  # For macOS
   sudo apt-get install terraform  # For Linux (with apt-get installed)
Enter fullscreen mode

Exit fullscreen mode

  1. Verify installation:
   terraform --version
Enter fullscreen mode

Exit fullscreen mode


Step 2: Set Up the AWS CLI

The AWS CLI allows Terraform to communicate with AWS for provisioning resources.

Installation

  1. Download the AWS CLI from AWS CLI Installation Guide.
  2. Follow the installation steps for your OS.

follow installation steps

Verify AWS CLI
verify AWS CLI

Configuration

  1. Run the following command to configure the CLI:
   aws configure
Enter fullscreen mode

Exit fullscreen mode

  1. Provide your Access Key ID, Secret Access Key, Region, and output format (e.g., json).

  2. Test the configuration:

   aws s3 ls
Enter fullscreen mode

Exit fullscreen mode

If this works, your AWS CLI is ready to go!


Step 3: Install and Configure VS Code

VS Code is a lightweight and powerful editor perfect for Terraform projects.

Installation

  1. Download VS Code from VS Code’s website.
  2. Install Terraform extensions:
    • Open VS Code.
    • Go to Extensions (Ctrl+Shift+X or Cmd+Shift+X).
    • Search for and install “HashiCorp Terraform”.

Optional Extensions for Productivity

  • AWS Toolkit: For managing AWS services directly from VS Code.
  • Prettier: For consistent formatting of your Terraform code.

Step 4: Create Your First Terraform Project

Terraform worklow

To verify your setup, create a simple Terraform project:

Here’s a basic Terraform program that creates an S3 bucket in AWS—a great starting point for beginners! It demonstrates how to use a provider and define a simple resource.


Basic Terraform Program for Creating an S3 Bucket

Step 1: Create a New Project Directory

mkdir terraform_job
cd terraform_job
Enter fullscreen mode

Exit fullscreen mode


Step 2: Write the Terraform Configuration File

Create a file named main.tf and add the following code:

# Specify the provider
provider "aws" {
  region = "us-east-1"  # Change to your desired region
}

# Define the S3 bucket resource
resource "aws_s3_bucket" "example_bucket" {
  bucket = "my-unique-bucket-name-7890"  # Replace with a globally unique name
  acl    = "private"

  # Optional: Add versioning
  versioning {
    enabled = true
  }

  tags = {
    Name        = "MyFirstBucket"
    Environment = "Dev"
  }
}
Enter fullscreen mode

Exit fullscreen mode


Step 3: Initialize Terraform

Run the following command to initialize Terraform. This downloads the necessary provider plugins:

terraform init
Enter fullscreen mode

Exit fullscreen mode

Image description

Image description


Step 4: Preview the Changes

To see what Terraform plans to create without actually creating anything, run:

terraform plan
Enter fullscreen mode

Exit fullscreen mode

Image description

Image description


Step 5: Apply the Changes

If everything looks good, apply the configuration to create the S3 bucket:

terraform apply
Enter fullscreen mode

Exit fullscreen mode

Type yes when prompted.


Step 6: Verify the Bucket in AWS

Log in to your AWS Management Console and navigate to S3. You should see your newly created bucket.

Image description


Step 7: Clean Up

To avoid unnecessary charges, delete the bucket by destroying the infrastructure:

terraform destroy
Enter fullscreen mode

Exit fullscreen mode

Type yes when prompted.


Key Concepts in This Program

  1. Provider: Specifies the cloud platform (AWS in this case) and configuration like the region.
  2. Resource: Represents the actual infrastructure component (e.g., an S3 bucket).
  3. Tags: Adds metadata to the resource for better organization and identification.
  4. Versioning: An optional feature to keep track of object versions.

Check out our other content

Check out other tags:

Most Popular Articles