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)
-
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.
-
Restart Your System:
- After installation, restart your computer.
-
Open WSL:
- Once your system restarts, open the WSL terminal by typing
wsl
in the Windows search bar.
- Once your system restarts, open the WSL terminal by typing
-
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
-
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 -
-
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"
-
Install Terraform:
- Now, update your package list again and install Terraform:
sudo apt update && sudo apt install terraform
-
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
-
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"
-
Unzip the Installer:
- Unzip the downloaded file:
sudo apt install unzip unzip awscliv2.zip
-
Install the AWS CLI:
- Run the install script:
sudo ./aws/install
-
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
-
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
)
-
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
-
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
-
Create a Terraform Configuration File:
- Create a new file named
main.tf
:
nano main.tf
- Create a new file named
-
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" }
-
Initialize Terraform:
- Initialize your Terraform workspace, which downloads the necessary plugins:
terraform init
-
Validate the Configuration:
- Ensure your configuration files are correct:
terraform validate
-
Plan the Infrastructure Deployment:
- Generate an execution plan, showing what actions Terraform will take:
terraform plan
-
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.
-
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
-
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)
great writeup