Kicking Off My Terraform Project Series: How To Create three different environments in Terraform.

FaizanFaizan
3 min read

I’m super excited to start a new series where we’ll dive into real-life projects using Terraform. Throughout this series, I’ll be guiding you step-by-step, showing you not only how to use Terraform but also giving you practical, hands-on experience. Whether you're just getting started or brushing up your skills, this series is for you!

Today, let’s keep it simple and build a Terraform project that will set up three different environments: dev, stage, and prod. We’ll use Terraform workspaces to manage these environments. Don't worry if you're new to this — I’ll walk you through it!

Ready? Let’s Go! 🚀

Step 1: Install Terraform

Before we begin, make sure you have Terraform installed on your computer. If not, you can grab it from the Terraform website.

Step 2: Set Up Your Project Folder

First, let’s create a new folder for this project and move into it. Run these commands in your terminal:

mkdir terraform-multi-env
cd terraform-multi-env

This is where all our Terraform magic will happen!

Step 3: Create the Main Configuration File

Now, let’s create a file called main.tf. This file will have the instructions to set up an AWS S3 bucket in each of our environments.

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-bucket-${terraform.workspace}-${count.index}"
}

What is ${terraform.workspace}?

In Terraform, ${terraform.workspace} helps you identify which workspace you're currently using. Think of workspaces as different stages for your infrastructure (like development, staging, and production).

Why Use It?

  • Unique Resource Names: It allows you to create resources with names that include the workspace name. For example, an S3 bucket could be named my-bucket-dev for the development workspace and my-bucket-prod for production.

  • Easier Management: You can use the same configuration for multiple environments without changing your code, keeping everything organized and separate.

Step 4: Initialize Terraform

Time to get things started! Run the command below to initialize Terraform:

terraform init

This sets everything up so Terraform knows what to do next.

Step 5: Create Workspaces for Each Environment

We’re going to create separate workspaces for each environment. This allows us to manage dev, stage, and prod without mixing things up. Run these commands one by one:

terraform workspace new dev
terraform workspace new stage
terraform workspace new prod

Boom! We now have three different environments ready to go!

Step 6: Deploy the Buckets in Each Environment

Now it’s time to actually create the S3 buckets. We’ll switch to each workspace and apply the configuration. Start with dev:

terraform workspace select dev
terraform apply -auto-approve

Then do the same for stage and prod:

terraform workspace select stage
terraform apply -auto-approve

With just a few commands, you’ve deployed S3 buckets in three different environments!

Step 7: Check Your Buckets in AWS

Head over to your AWS account and check if the buckets were created. You should see something like this:

And That’s It! 🎉

You’ve just set up multiple environments with Terraform! Not only did you learn how to work with Terraform, but you also got hands-on experience with real-world tools like AWS.

Stay tuned for the next post, where we’ll dive even deeper into more cool Terraform projects. Until then, keep exploring and experimenting! 💻🌟

0
Subscribe to my newsletter

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

Written by

Faizan
Faizan

👋 Hi there! I'm Faizan, a Certified AWS Certified Solution Architect with over 3 years of hands-on experience in IT Service Desk, AWS Cloud, and Infrastructure Support. I thrive in customer-oriented environments, where I tackle challenges head-on and deliver effective solutions. Throughout my career, I have demonstrated a strong commitment to problem-solving, always striving to exceed expectations and provide exceptional service. As a proactive and focused professional, I possess a natural curiosity and enthusiasm for technology, which fuels my passion for continuous learning and growth. Working collaboratively within diverse teams, I am a reliable team player who values open communication and fosters a positive work environment. I pride myself on my ability to adapt quickly and work independently, ensuring smooth operations and efficient workflows. My hunger for knowledge has led me to explore the exciting realms of DevOps and further expand my AWS expertise. I embrace new challenges and seize opportunities to develop my skills, seeking to stay at the forefront of emerging trends and best practices. Beyond the professional sphere, I am an avid explorer of different cultures, languages, and perspectives. I find joy in engaging with people from diverse backgrounds, discovering new places, and immersing myself in the beauty of our world. I am eager to connect with like-minded professionals, industry experts, and potential collaborators. Let's forge new connections, share insights, and explore opportunities together. Feel free to reach out, and let's start a conversation! #AWS #CloudComputing #DevOps #ITSupport #CustomerService #Networking