DEV Community

Chibuzo Nwobia
Chibuzo Nwobia

Posted on

Step-by-Step Guide to Setting Up Terraform, AWS CLI, and Your AWS Environment.

Setting up Terraform, AWS CLI, and configuring your AWS environment on Windows Subsystem for Linux (WSL) involves several steps. Below is a detailed step-by-step guide to help you get started.

Step 1: Set Up Windows Subsystem for Linux (WSL)

  1. Install WSL:

    • Open PowerShell as Administrator and run:
     wsl --install
    
  • This command installs WSL and the latest Ubuntu distribution by default. You can choose a different distribution if you prefer.
  1. Restart Your System:

    • After installation, restart your computer.
  2. Open WSL:

    • Once your system restarts, open the WSL terminal by typing wsl in the Windows search bar.
  3. Update and Upgrade Your Distribution:

    • In the WSL terminal, update your package lists and upgrade installed packages:
     sudo apt update && sudo apt upgrade -y
    

Step 2: Install Terraform on WSL

  1. Install the HashiCorp GPG Key:

    • Run the following command to add the HashiCorp GPG key:
     curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
    
  2. Add the HashiCorp Linux Repository:

    • Add the official Terraform repository to your system:
     sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
    
  3. Install Terraform:

    • Now, update your package list again and install Terraform:
     sudo apt update && sudo apt install terraform
    
  4. Verify the Installation:

    • Check that Terraform is installed correctly:
     terraform -v
    
  • This should return the installed version of Terraform.

Step 3: Install AWS CLI on WSL

  1. Download the AWS CLI Installer:

    • Download the latest version of the AWS CLI using the following command:
     curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
    
  2. Unzip the Installer:

    • Unzip the downloaded file:
     sudo apt install unzip
     unzip awscliv2.zip
    
  3. Install the AWS CLI:

    • Run the install script:
     sudo ./aws/install
    
  4. Verify the Installation:

    • Confirm the AWS CLI is installed:
     aws --version
    
  • This should return the installed version of AWS CLI.

Step 4: Configure Your AWS Environment

  1. Configure AWS CLI:

    • Set up your AWS credentials by running:
     aws configure
    
  • You'll be prompted to enter your:
    • AWS Access Key ID
    • AWS Secret Access Key
    • Default region name (e.g., us-east-1)
    • Default output format (e.g., json)
  1. Test the Configuration:

    • To verify your configuration, you can run a simple AWS command, like listing your S3 buckets:
     aws s3 ls
    

Step 5: Set Up Your AWS Environment Using Terraform

  1. Create a Directory for Your Terraform Project:

    • Navigate to your working directory and create a new directory for your Terraform files:
     mkdir 30-day-terraform
     cd 30-day-terraform
    
  2. Create a Terraform Configuration File:

    • Create a new file named main.tf:
     nano main.tf
    
  • Add your Terraform configuration. For example, to create an S3 bucket:

     provider "aws" {
       region = "us-east-1"
     }
    
     resource "aws_s3_bucket" "my_bucket" {
       bucket = "demo_bucket"
       acl    = "private"
     }
    
  1. Initialize Terraform:

    • Initialize your Terraform workspace, which downloads the necessary plugins:
     terraform init
    
  2. Validate the Configuration:

    • Ensure your configuration files are correct:
     terraform validate
    
  3. Plan the Infrastructure Deployment:

    • Generate an execution plan, showing what actions Terraform will take:
     terraform plan
    
  4. Apply the Configuration:

    • Deploy the infrastructure by applying the configuration:
     terraform apply --auto-approve
    
  • using the --auto-approve would prevent Terraform from asking for confirmation before proceeding.
  1. Verify the Deployment:
    • Once the apply process is complete, you can verify the resources have been created using the AWS CLI or the AWS Management Console.

Step 6: Clean Up Resources

  1. Destroy the Infrastructure:

    • When you're done, you can clean up the AWS resources created by Terraform by running:
     terraform destroy --auto-approve
    
  • using the --auto-approve would prevent Terraform from asking for confirmation before proceeding.

Conclusion

By following these steps, you've successfully set up Terraform, AWS CLI, and configured your AWS environment using WSL. You can now manage your AWS infrastructure using Terraform, enabling a consistent and automated approach to infrastructure provisioning.

Top comments (1)

Collapse
 
destech profile image
CHI CHE

great writeup