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.
- Open the Ubuntu app.
- Run the following commands:
sudo apt update
Step 3. Install Terraform
Now that Ubuntu is set up, we can proceed to install Terraform.
- Download the Terraform package by running:
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
- Add the HashiCorp Linux repository:
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
- Update and install Terraform:
sudo apt update && sudo apt install terraform
- 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).
- Download and install AWS CLI v2 by following the instructions here.
- Once installed, configure AWS CLI:
aws configure
- 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.
- Create a directory for your project:
mkdir terraform-aws-demo && cd terraform-aws-demo
- Create a new file called
main.tf
using your favorite text editor, for example:
nano main.tf
- 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" }
- 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:
Step 7. Plan and Apply to Create Resources
Execute the plan and apply commands to provision the defined infrastructure.
- To see the execution plan:
terraform plan
- To create the resources:
terraform apply
- Confirm the action by typing
yes
when prompted.
Step 8. Verify the Creation
Check your AWS console to verify that the S3 bucket has been created successfully.
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.