How to Configure Terraform on Windows 10 WSL Ubuntu for AWS Provisioning

Provisioning infrastructure on AWS with Terraform is efficient and allows for Infrastructure as Code (IaC) practices. In this tutorial, we will walk through the process of setting up Terraform on Windows 10 using the Windows Subsystem for Linux (WSL) with Ubuntu.

Step-by-Step Guide

Step 1. Enable WSL and Install Ubuntu

To enable WSL and install Ubuntu, follow the instructions in this guide: Install Ubuntu 20.04 or 22.04 in WSL 2 on Windows 10.

Step 2. Update Ubuntu Packages

Ensure all the default packages are up to date.

  1. Open the Ubuntu app.
  2. Run the following commands:
sudo apt update

Step 3. Install Terraform

Now that Ubuntu is set up, we can proceed to install Terraform.

  1. Download the Terraform package by running:
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
  1. Add the HashiCorp Linux repository:
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
  1. Update and install Terraform:
sudo apt update && sudo apt install terraform
  1. Verify the installation:
terraform -version

Optional: Upgrade Terraform to a Specific Version

For upgrading Terraform to a specific version, check out our guide here.

Step 4. Configure AWS CLI

Terraform interacts with AWS via the AWS Command Line Interface (CLI).

  1. Download and install AWS CLI v2 by following the instructions here.
  2. Once installed, configure AWS CLI:
aws configure
  1. Enter your AWS Access Key ID, Secret Access Key, region, and output format when prompted.

Step 5. Create a Terraform Configuration File

This is where you define your infrastructure as code.

  1. Create a directory for your project:
mkdir terraform-aws-demo && cd terraform-aws-demo
  1. Create a new file called main.tf using your favorite text editor, for example:
nano main.tf
  1. Add the following content to main.tf, which defines an AWS S3 bucket:
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

provider "aws" {
  region = "us-west-2"
}

resource "aws_s3_bucket" "mybucket" {
  bucket = "sb-bucket-name-identifier"
}

resource "aws_s3_bucket_acl" "mybucket_acl" {
  bucket = aws_s3_bucket.mybucket.id
  acl    = "private"
}
  1. Save and exit the editor.

Step 6. Initialize Terraform

Initialize your Terraform project, which will download necessary plugins.

Run the following command in the same directory as your main.tf file:

terraform init

Note: If you encounter an error like this during terraform init, just increase the timeout by running the command: export TF_REGISTRY_CLIENT_TIMEOUT=300.

│ Error: Failed to query available provider packages
│ 
│ Could not retrieve the list of available versions for provider hashicorp/aws: could not query provider registry for
│ registry.terraform.io/hashicorp/aws: the request failed after 2 attempts, please try again later: Get
│ "https://registry.terraform.io/v1/providers/hashicorp/aws/versions": net/http: request canceled while waiting for connection (Client.Timeout
│ exceeded while awaiting headers)

Here’s what the successful output of terraform init should look like:

How to Configure Terraform on Windows 10 WSL with Ubuntu for AWS Provisioning

Step 7. Plan and Apply to Create Resources

Execute the plan and apply commands to provision the defined infrastructure.

  1. To see the execution plan:
terraform plan
How to Configure Terraform on Windows 10 WSL with Ubuntu for AWS Provisioning
  1. To create the resources:
terraform apply
  1. Confirm the action by typing yes when prompted.
How to Configure Terraform on Windows 10 WSL with Ubuntu for AWS Provisioning

Step 8. Verify the Creation

Check your AWS console to verify that the S3 bucket has been created successfully.

How to Configure Terraform on Windows 10 WSL with Ubuntu for AWS Provisioning

Conclusion

Congratulations! You have now configured Terraform on Windows 10 WSL with Ubuntu and successfully provisioned an AWS resource. Remember to commit your changes to version control and continue exploring more complex Terraform configurations and modules to manage a broader infrastructure.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.