DEV Community

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

Posted on

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

Setting up Terraform, AWS CLI, and Visual Studio Code (VS Code) is essential for developers working on cloud infrastructure automation and Infrastructure-as-Code (IaC) workflows. This guide walks you through the steps to set up these tools, share tips for smoother installation, and address common challenges.


Prerequisites

Before starting, ensure you have:

  • A computer running Windows, macOS, or Linux.
  • Administrator privileges to install software.
  • An AWS account (if you’re planning to use AWS).

1. Install Terraform

Terraform is a tool used for building, changing, and versioning infrastructure safely and efficiently.

Steps to Install Terraform

  1. Download Terraform:

  2. Install Terraform:

    • Windows:
      • Unzip the downloaded file.
      • Move the terraform.exe file to a directory included in your system’s PATH (e.g., C:\Windows\System32).
    • macOS/Linux:
      • Unzip the file and move the terraform binary to /usr/local/bin:
       sudo mv terraform /usr/local/bin/
    
  3. Verify Installation:

    • Open your terminal or command prompt.
    • Run the command:
     terraform --version
    
  • You should see the installed version of Terraform.

Tips:

  • Use a version manager like tfenv (for macOS/Linux) to manage multiple Terraform versions.

2. Install AWS CLI

The AWS Command Line Interface (CLI) allows you to interact with AWS services via the terminal.

Steps to Install AWS CLI

  1. Download AWS CLI:

  2. Install AWS CLI:

    • Windows:
      • Run the installer and follow the prompts.
    • macOS/Linux:
      • Use curl to download and install:
       curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
       unzip awscliv2.zip
       sudo ./aws/install
    
  3. Verify Installation:

    • Run the following command in your terminal:
     aws --version
    
  • You should see the AWS CLI version installed.
  1. Configure AWS CLI:

    • Run:
     aws configure
    
  • Provide your:
    • AWS Access Key ID
    • AWS Secret Access Key
    • Default region (e.g., us-east-1)
    • Output format (e.g., json, table, or text)

Tips:

  • Store AWS credentials securely by using AWS profiles (~/.aws/credentials).
  • Use the AWS CLI autocomplete feature to speed up command typing:
  complete -C '/usr/local/bin/aws_completer' aws
Enter fullscreen mode Exit fullscreen mode

3. Install and Configure VS Code

Visual Studio Code (VS Code) is a lightweight but powerful source code editor with built-in support for development tools like Terraform.

Steps to Install VS Code

  1. Download VS Code:

  2. Install VS Code:

    • Windows:
      • Run the installer and follow the prompts.
    • macOS/Linux:
      • Download the package and follow the installation instructions for your OS.
  3. Verify Installation:

    • Open VS Code to ensure it launches correctly.
  4. Install Extensions for Terraform and AWS:

    • Open VS Code.
    • Go to the Extensions tab (Ctrl+Shift+X or Cmd+Shift+X).
    • Search for and install:
      • HashiCorp Terraform: Provides syntax highlighting and autocompletion for Terraform files.
      • AWS Toolkit: Allows integration with AWS services.
    • Optional extensions:
      • Prettier or Beautify: For code formatting.
      • GitLens: To improve Git workflow.
  5. Configure VS Code Settings:

    • Configure your VS Code settings for Terraform by adding the following to your settings file:
     {
       "[terraform]": {
         "editor.defaultFormatter": "hashicorp.terraform",
         "editor.formatOnSave": true
       }
     }
    

Tips:

  • Use the built-in terminal in VS Code for running Terraform and AWS CLI commands.
  • Enable autosave to avoid losing work.

Common Challenges and How to Resolve Them

1. Terraform Command Not Found

  • Issue: The terminal says terraform: command not found.
  • Solution: Ensure the terraform binary is in your system’s PATH. Restart your terminal after adding it.

2. AWS CLI Configuration Issues

  • Issue: Unable to authenticate with AWS CLI.
  • Solution: Double-check your AWS Access Key ID and Secret Access Key. Ensure they are associated with an IAM user that has proper permissions.

3. VS Code Extensions Not Working

  • Issue: Terraform syntax highlighting or AWS Toolkit features are not functioning.
  • Solution: Check if the extensions are installed and enabled. Restart VS Code after installing extensions.

Conclusion

Setting up Terraform, AWS CLI, and VS Code is straightforward when following these steps. By ensuring proper installation and configuration, you’ll have a powerful development environment ready for building and managing cloud infrastructure efficiently. With practice, you’ll streamline your workflow and overcome any initial setup challenges.

Happy coding!

Top comments (0)