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

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

·

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! 💻🌟