DEV Community

Cover image for Step-by-Step Guide to Setting Up Terraform, AWS CLI, and VS Code
Patrick Odhiambo
Patrick Odhiambo

Posted on

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

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

Setting up Terraform, AWS CLI, and VS Code is essential for managing cloud infrastructure efficiently. This guide will walk you through each step of the process, including tips and solutions to common challenges.


1. Install Terraform

terrafoem

Step 1: Download Terraform

  • Visit the official Terraform website.
  • Download the version appropriate for your operating system (Windows, macOS, Linux).

Step 2: Install Terraform

  • Windows: Extract the downloaded zip file and place the terraform.exe file in a directory included in your system’s PATH.
  • macOS/Linux: Use Homebrew for macOS or package managers like apt-get for Linux.

    brew install terraform
    

    or

    sudo apt-get install terraform
    

Step 3: Verify Installation

Open your terminal or command prompt and type:

terraform -v
Enter fullscreen mode Exit fullscreen mode

You should see the installed Terraform version.

Tip: Ensure Terraform’s path is correctly set by adding it to your environment variables if necessary.


2. Set Up AWS CLI

cli

Step 1: Install AWS CLI

Step 2: Configure AWS CLI

After installation, configure your AWS CLI with your credentials:

aws configure
Enter fullscreen mode Exit fullscreen mode

You’ll be prompted to enter:

  • AWS Access Key ID
  • AWS Secret Access Key
  • Default region name
  • Default output format (e.g., json)

Step 3: Verify Installation

Check the configuration by running:

aws sts get-caller-identity
Enter fullscreen mode Exit fullscreen mode

This should return your AWS account details, confirming the setup.

Challenge & Solution: If you encounter permission errors, ensure that your IAM user has the necessary permissions for the AWS CLI commands.


3. Set Up VS Code

vscode

Step 1: Download and Install VS Code

Step 2: Install Extensions for Terraform and AWS

  • Open VS Code.
  • Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or pressing Ctrl+Shift+X.
  • Search for and install the following extensions:
    • HashiCorp Terraform: Provides syntax highlighting, auto-completion, and other features for Terraform files.
    • AWS Toolkit: Adds AWS integration, including support for AWS Lambda, S3, and other services.

Step 3: Configure VS Code for Terraform Development

  • Create a new folder for your Terraform project and open it in VS Code.
  • Create a .tf file (e.g., main.tf), and VS Code will automatically recognize it as a Terraform configuration file.

Tip: Use the built-in terminal in VS Code to run Terraform and AWS CLI commands directly from your workspace.


Common Challenges I Encountered and their Solutions

  • Issue with AWS CLI Path: If the aws command is not recognized, ensure the AWS CLI is in your system’s PATH. On Windows, you might need to add it manually through System Properties > Environment Variables.

  • Terraform Initialization Problems: If Terraform doesn’t initialize correctly, check that your provider block (e.g., provider "aws") is correctly configured with the right region and credentials.

  • VS Code Extensions Not Working: Ensure you’re using the latest version of VS Code and that the extensions are up-to-date. Restarting VS Code often resolves extension-related issues.


Parting Shot

Setting up Terraform, AWS CLI, and VS Code is a crucial first step for anyone looking to manage cloud infrastructure efficiently. By following this guide, you’ll have a fully functional environment ready for infrastructure as code development. Remember, troubleshooting is part of the learning process, so don’t hesitate to explore documentation and community forums if you encounter any issues.

Top comments (1)

Collapse
 
rquadling profile image
Richard Quadling • Edited

Purely on the grounds of some personal advertising, if you are needing to use AWS CLI within Terraform, take a look at registry.terraform.io/modules/digi...

I wrote it predominantly to access AWS elements that were not available within the Terraform provider.

And as an example, the ability to get the AWS WAFv2 Managed Rule Group Rules was NOT available within the AWS Provider until Jan 28th 2026.

I automate some dashboards and documentation based upon the rule groups and rules the WAF has, and so needed to get the managed rules information. Wasn't possible within the provider, so the module comes to use!

# Get all the managed rule groups.
module "managed_rule_groups_overview" {
  source          = "digitickets/cli/aws"
  version         = "7.1.1"
  aws_cli_commands = [
    "wafv2", "list-available-managed-rule-groups",
    "--scope REGIONAL"
  ]
}

# Get more information for each member of the rule_groups.
# This is used to add the available metrics to the CloudWatch Dashboard as well as the automated documentation.
module "managed_rule_groups_detailed" {
  source = "digitickets/cli/aws"
  for_each = {
    for rule_group in module.managed_rule_groups_overview.result.ManagedRuleGroups : rule_group.Name => rule_group
  }
  version         = "7.1.1"
  aws_cli_commands = [
    "wafv2", "describe-managed-rule-group",
    "--scope REGIONAL",
    "--vendor-name", each.value.VendorName,
    "--name", each.value.Name
  ]
}
Enter fullscreen mode Exit fullscreen mode

The second AWS CLI module call is now replaceable with native Terraform HCL:

data "aws_wafv2_managed_rule_group" {
  for_each = {
    for rule_group in module.managed_rule_groups_overview.result.ManagedRuleGroups : rule_group.Name => rule_group
  }
  name        = each.key
  scope       = "REGIONAL"
  vendor_name = each.value.VendorName
}
Enter fullscreen mode Exit fullscreen mode

(more or less, actually in the process of migrating so to the updated version of the AWS Terraform Provider).

Still waiting on the first one.

And as with all things, other options may exist!