Skip to main content
In this guide, we will walk you through creating a new Terraform module that deploys multiple environments of an infrastructure template. FlexIT Consulting’s blueprint for a prototype payroll software is used as an example. This payroll software will be deployed in several countries on the AWS Cloud using an identical architecture for each deployment. The architecture comprises the following components:
  • A single EC2 instance, using a custom AMI, that hosts the application server.
  • A DynamoDB NoSQL database to store employee and payroll data.
  • An S3 bucket to store documents such as pay stubs and tax forms.
Users access the application hosted on the EC2 instance, and this simplified setup utilizes the default VPC. It does not include advanced configurations such as VPC endpoints or specific IAM roles. The high-level architecture diagram below depicts this setup:
The image depicts a simplified AWS architecture with an EC2 instance, S3 bucket, and DynamoDB table within a default VPC, focusing on payroll data management.
The primary goal is to create a reusable Terraform module that deploys a consistent stack of resources across different regions.

Module Directory and File Structure

The reusable module will be stored in the modules directory. In this example, we will use the path: /root/terraform-projects/modules/payroll-app This directory will contain resource definitions for the EC2 instance, S3 bucket, and DynamoDB table. To maintain consistency across environments, some configuration values such as the EC2 instance type, DynamoDB table name, and primary key are hard-coded, while others like the AMI are configurable by the user.

Creating the Module Directory

Inside this module directory, create the following files:
  • app_server.tf
  • s3_bucket.tf
  • dynamodb_table.tf
  • variables.tf

Module Resource Definitions

EC2 Instance Configuration (app_server.tf)

S3 Bucket Configuration (s3_bucket.tf)

DynamoDB Table Configuration (dynamodb_table.tf)

Module Variables (variables.tf)

In these configurations, the AMI and the application region are defined as variables. Meanwhile, the instance type, DynamoDB table name, and primary key are fixed. The S3 bucket name is prefixed with the region to ensure global uniqueness.

Deploying the Terraform Module

Once the module is defined, we can deploy an instance of it. Below are examples of deploying the module in two regions: US East 1 and the UK (London region).

US East 1 Deployment

Start by creating a directory for the US payroll application:
Inside this directory, create a main.tf file with the following content:
If you need to override the default bucket name, you can specify the bucket variable in this module configuration.
After setting up the directory, initialize Terraform by running:
Terraform will download the module from the specified source and the AWS provider required by the module. The initialization output will resemble:
Apply the configuration to create the resources:
The EC2 instance, S3 bucket, and DynamoDB table are created based on the configuration defined in the module and the values provided in the root module.

UK Deployment

To deploy the same stack in the UK region, create a directory for the UK payroll application:
Inside this directory, create a main.tf file with the following content:
In this configuration, the app_region is set to eu-west-2 (London region) and the AMI ID is appropriate for that region. When you run terraform apply, Terraform will create the resources with names prefixed by eu-west-2 to reflect the region-specific deployment. The module output ensures clear resource addressing. For instance, you can reference the DynamoDB table as:
And the EC2 instance as:

Benefits of Using Terraform Modules

Using reusable modules in Terraform provides several advantages: By adopting a module-based approach, you ensure that key configuration aspects such as the EC2 instance type and DynamoDB settings remain constant, while still being able to adjust region-specific variables like the AMI ID and bucket naming.
For further learning on Terraform modules, refer to the Terraform Documentation.

That’s it for this guide on creating and deploying a Terraform module for multiple environments. Happy deploying!

Watch Video