[Hands-On] How to Set Up Terraform with VSCode and Connect to AWS

6 min readSep 4, 2023


In modern tech panorama, infrastructure as code (IaC) has become an essential tool for engineers running in cloud environments. If you’re new to cloud computing, let me introduce you to the core concepts of IaC and Terraform. In this complete manual, I will stroll you through the technique of setting up Terraform for your local device, especially in the VS Code development environment, and show you a way to connect it to your AWS account.

This hands-on tutorial is designed to be a step-by-step manual, making it an easy way to get started with Terraform and create a basic infrastructure. Keep in mind that while this manual will provide you with a solid basis, building complex architectures with Terraform would require a deeper knowledge of the tool.

Let’s dive in and begin our journey into the world of Terraform and cloud infrastructure automation.

What’s Infrastructure as Code (IaC)?

Infrastructure as code (IaC) is the practice of managing and provisioning infrastructure via code. This could assist in improving the reliability, scalability, and security of your infrastructure. IaC tools assist you in defining your infrastructure in code, which could then be used to automate the provisioning and control of your infrastructure.

Pros of IaC:

  1. Faster deployments: IaC assists you in deploying your infrastructure faster and without problems by automating the provisioning method. This could save you time, and it could additionally assist in reducing errors.
  2. Improved reliability: IaC can assist in improving the reliability of your infrastructure by ensuring that your infrastructure is always provisioned in the same way, this could assist in preventing errors and outages.
  3. Advanced scalability: IaC can help you to scale your infrastructure greater without problems. That is due to the fact you may outline your infrastructure in code, which can then be used to automate the provisioning of the latest sources. For example, you can deploy one, two, or 100 servers with simple codes.

Cons of IaC:

  1. Difficult to maintain the code: IaC code can be complicated and difficult to preserve. that is as it frequently needs to be updated to reflect adjustments to your infrastructure or your requirements. In case your IaC code is not well-written or well-documented, it could be tough to make changes without introducing errors.
  2. Difficult to restart at the same point that failed: If your IaC code fails to set up your infrastructure, it could be difficult to restart the procedure from the same point. That is due to the fact that IaC code frequently creates dependencies among sources. If one useful resource fails to install, it may prevent different resources from deploying.
  3. Time-consuming to understand the other’s code: If you are not the writer of the IaC code, it could be time-consuming to understand the way it works. This is in particular true if the code is not well-documented.

What is Terraform, and which platform can it be connected to?

Terraform is an open-source infrastructure as code (IaC) tool that lets you safely and predictably create, change, and improve infrastructure. It is able to be used to control infrastructure on a huge variety of structures, such as Amazon Web Services (AWS), Microsoft Azure, Google Cloud Platform (GCP), vSphere, OpenStack, etc.

Terraform uses declarative language to define infrastructure resources. which means you describe the desired state of your infrastructure, and Terraform will figure out how to get there. This makes it smooth to manage complex infrastructure and to ensure that your infrastructure is constantly within the desired state.

Let’s proceed with the setup and then perform a simple test. Please ensure you have installed VScode, and also have an AWS IAM user account before the below procedure.

Step I: Obtain Terraform

1. Commence by acquiring Terraform from the official website at https://developer.hashicorp.com/terraform/downloads. Be sure to select the appropriate version tailored to your operating system.


2. Establish a dedicated directory named ‘Terraform’ within your C drive and move the Terraform executable file (.exe) into this designated folder.

3. To seamlessly integrate Terraform into your environment, you must also configure an environmental variable. Follow these steps to achieve this:

  • Access your System Properties and navigate to the Environmental Variables section.
  • In the Environmental Variables window, locate the ‘Path’ variable, and select ‘Edit.’
  • Proceed by adding a new environment variable with the path ‘C:\Terraform.’
  • Confirm your changes by clicking ‘OK,’ and subsequently exit the Environmental Variables and System Properties dialogs.

Ascertain the successful installation of Terraform by executing the following command in either the Command Prompt, PowerShell, or Visual Studio Code:

Step II: Installing Extensions in Visual Studio Code

1. Launch Visual Studio Code, and navigate to the Extensions Marketplace.

2. Conduct a search for the ‘Terraform’ extension and proceed to install it. Ensure that you enable this extension once it is installed.

3. Continue searching for and installing the ‘HarshCorp Terraform’ extension. Once installed, make sure that you enable this extension to enhance your Terraform development environment.

Effectively incorporating these extensions will empower your Visual Studio Code environment with valuable tools for Terraform development.

Step III: Establishing an AWS Connection

  1. Initiate this AWS connectivity process by crafting a new file bearing the extension .tf.
  2. Within this newly created file, configure the Terraform provider for AWS, a pivotal step in facilitating your interaction with Amazon Web Services.
  3. To authenticate with AWS, meticulously input your ACCESS KEY and SECRET KEY credentials, safeguarding the integrity of your security access.
  4. Specify the AWS region that aligns with your project requirements. This region selection is pivotal for optimizing performance and resource allocation.

By meticulously following these steps, you will seamlessly connect your Terraform environment with Amazon Web Services, laying the foundation for robust cloud infrastructure management.

Step IV: Provision an EC2 Instance using Terraform in Visual Studio Code

1. Craft your Terraform code as exemplified below, precisely defining the desired configuration for your EC2 instance with ami and instance type.

2. Begin this pivotal phase by executing the command ‘terraform init’. This initiates the Terraform environment, setting the stage for infrastructure provisioning.

3. Employ the command ‘terraform plan’ to gain insight into the forthcoming infrastructure alterations. This step allows you to review and validate the planned changes before proceeding.

4. Execute the command ‘terraform apply’, and when prompted, confirm with ‘yes’. This action triggers the provisioning process, translating your Terraform code into tangible resources.

5. Monitor the results within your Visual Studio Code environment and simultaneously inspect the AWS Console for a comprehensive view of the provisioned EC2 instance.

By meticulously following these steps, you will effectively test the creation of an EC2 instance using Terraform, fostering a deeper understanding of infrastructure as code in action.

While this guide provides a strong foundation, remember that mastering Terraform and IaC is an ongoing journey, empowering you to confidently build and manage complex cloud architectures. Happy coding!




Partner Engineer at Google | Solutions Architect | Cloud Engineer | AWS & GCP Certified