Multi-Environment Infrastructure using Terraform: Step-by-step guide to deploy in AWS

Table of Contents :

  1. Overview

  2. Architecture

  3. Plan of Execution

  4. Results

🖼️Overview

Hi Guys, my name is Vasudeva Vempati and Iam a Cloud Learner, in this blog you can create a mutli stage environment infrastructure using Terraform which consists of S3 buckets, Dynamo DB Tables and EC2 Instances. This infrastructure is provisioned by Terraform which is connected to AWS using AWS CLI from VScode editor. This is a beginner friendly project to deploy an infrasture on AWS who are learning Terraform (or going to learn Terraform) which has simple AWS and Terraform concepts. This is a simple version of multi enviroment set-up which is used in industry using Terraform and managed using tools like Ansible.

🏡Architecture

The Architecture has 3 Stages namely Development, Staging and Production where each enviroment has a S3 Bucket, Dynamo DB Table and 1:1:3 (t2.micro, t2.medium, t2.large) Servers respective to the enviroment. They all are in the same VPC with S3 bucaket and Dynamo DB table in the private subnet and servers in the public subnet.

Plan of Execution

🖥️Local Setup

  • VS Code

    We are going to use VS Code Editor for this project. You can download it from below link. Install Hashicorp Terraform extension with verifies blue tick on your local computer from the official provider and set-up the process.

    VS Code : https://code.visualstudio.com/download

Terraform Installation

Install Terraform from the link below here and finish the setting things up.

Link : https://developer.hashicorp.com/terraform/install

AWS CLI

Install AWS CLI from the official AWS provider with latest version for your system

AWS CLI : https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

🚨 IAM secret key

I hope you have installed all the software and tools, now it's time to open the AWS console. please head over to IAM service on the AWS Console.

  • Create a New USER

    • please click on the user tab in the left corner. now click on the add user button on the top right corner and give the name to the user. now you need to attach the Policy for the user. actually, we should follow the Principle of least privilege but we need to use many services to here you can give AdministratorAccess. but it is not at all advisable to give AdministratorAccess or you can follow the above list of services part and attach the policy accordingly. now click on the next button. and click on the Create User button.

🔐Create Access Key and Secret Access Key

After creating your user with required permissions you have to connect to AWS CLI using certain credentials where Access key and Secret Access key comes in hand which acts as your lock and key pair to enter into your account. You can create an Access key and download it for the future use when logging back or create a new key.

💻Configure AWS-CLI

  • Now open the terminal on your system and type aws configure which will prompt you with credentials that you have created in the before step. Enter your your Acess key ID and secret key id. please enter what we have just created. furthermore**,** it will ask output format JSON default region us-east-1 . you can enter the region where you want to deploy your app. Follow the below image to get more ideas.

✍️Terraform commands

Initialize Terraform working directory

  • Downloads required provider plugins (like AWS, Azure, GCP).

  • Sets up the backend if you’re using remote state.

  • Initializes modules if you have any.

terraform init

Preview of what Terraform is going to create, delete

  • Compares your code (*.tf files) with the current state.

  • Shows what changes will be made (adds, deletes, updates).

  • Does not make any changes—it's just a preview.

terraform plan

Provisioning your Infrastructure

  • Executes the changes required to reach the desired state.

  • Prompts for confirmation (you can skip it with -auto-approve).

  • Updates the state file.

terraform apply

Destroy/remove the resources created by Terraform

  • Destroy all the resources configured and tracked using statefile

  • Useful for removing all resources during cleanup

  • -auto-approve to destroy without interactive prompt

terraform destroy

\ Note: Dont’ forget to destroy all the resources created using terraform destroy so it doesn’t incur cost after practising ! **

🖥️Results

Creation of S3 Buckets with different environment stages

DynamoDB Tables are created and are active in status

Instances running in different environments successfully.

Thanks for reading 🙏

📚Resources

🧑‍💻https://youtu.be/NoKeG8iRnQw?si=CgEiqMEJp6GXUeL9

🧑‍💻https://github.com/LondheShubham153/terraform-for-devops/tree/main/aws_module_project

🎓www.linkedin.com/in/vempativasudeva

🧑‍💻https://github.com/Vasudeva630

0
Subscribe to my newsletter

Read articles from Vasudeva Vempati directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vasudeva Vempati
Vasudeva Vempati

Cloud Learner and Enthusiast. I like to breakdown things and learn how they work in real time.