
Automating AWS Infrastructure with Terraform
Aug 2
3 min read
2
59
1
What’s Covered in This Blog Post?
Step 1: Install Visual Studio Code
Step 2: Install AWS CLI
Step 3: Install Terraform
Step 4: Install VS Code Extensions
Step 5: Set Up AWS IAM
Step 6: Configure Terraform to Create AWS VPC and Subnet
Step 7: Verify VPC and Subnet in AWS Console
Step 8: Destroy Resources
Please Note: I've pasted the screenshots from my lab for your reference.
Step 1: Install Visual Studio Code
First, download and install Visual Studio Code for your operating system. It’s a lightweight, powerful editor that’s widely used for writing and managing scripts. While VS Code offers a rich development experience with extensions and syntax support, you can also use PowerShell or Command Prompt (CMD) for executing Terraform commands if you prefer a simpler interface.
Step 2: Install AWS CLI
Next, install the AWS Command Line Interface (CLI). This tool allows you to interact with AWS services directly from your terminal. Below is the link to download it:
Step 3: Install Terraform
Now, download Terraform from the official HashiCorp website. Follow the installation instructions specific to your operating system. The site provides installers and detailed setup guides for Windows, macOS, and Linux platforms.
After installation, verify it by running the following command:
```bash
terraform -v
```
Step 4: Install VS Code Extensions
To enhance your development experience, install the following extensions in VS Code:
AWS Toolkit
Terraform by HashiCorp
These extensions provide syntax highlighting, auto-completion, and integration with AWS services.

Step 5: Set Up AWS IAM
Ensure you have an IAM user with programmatic access and necessary permissions to provision and manage resources. Follow the instructions and screenshots below:
Go to AWS Management Console
Navigate to IAM and create a user.
Set permissions and create the user.
Under Security Credentials, create an Access Key.
Note: Make sure to save the generated Access Key and Secret Key for later use.






Step 6: Configure Terraform to Create AWS VPC and Subnet
Create a Terraform configuration file (e.g., `main.tf`) with the complete syntax you need. Here’s how to set it up:
Run the command to configure AWS with your IAM Access Key and Secret Key:
```bash
aws configure
```
Prepare your Terraform code for provisioning.
Initialize Terraform with:
```bash
terraform init
```
Apply the configuration with:
```bash
terraform apply
```
Confirm the creation by typing "yes" when prompted.

Below is the code I used to provision AWS VPC and Subnet:




Step 7: Verify VPC and Subnet in AWS Console
After applying the Terraform configuration, verify that your VPC and Subnet have been created successfully in the AWS Management Console.


Step 8: Destroy Resources
When you're done testing, you can clean up your resources. To delete the previously provisioned resources, run the following command:
```bash
terraform destroy
```
Confirm the deletion by typing "yes" when prompted.

Once the destruction is complete, verify from the AWS Management Console to ensure all resources have been removed.
Conclusion
With Terraform, you can automate AWS infrastructure efficiently and consistently. This hands-on guide helps you get started with IaC and lays the foundation for more advanced automation workflows. I hope this helps you on your journey! Good luck!
By following these steps, you’ll be well on your way to mastering Infrastructure as Code with Terraform. Enjoy the process, and don't hesitate to reach out if you have any questions!







Thank you for this article. nicely explained.