📖 Terramate Docs | 🚀 Getting Started | 💻 Playground | 🙌 Join Us
This template repository provides a pre-configured Terramate project to get started with Terramate and Terraform on AWS using best practices. It also comes with pre-configured GitOps workflows that run natively in GitHub Actions so that you can automate your Terraform in Pull Requests without requiring any additional tooling or infrastructure using the Terramate orchestration and change detection.
- GitOps for Terraform with GitHub Actions: Pre-configured GitHub Action GitOps workflows using merge-and-apply strategy with drift detection and reconciliation.
- Recommended Project Structure: Best practice project structure with environment-based organization (stg, prd).
- Change Preview in Pull Requests: Preview and approval of plans in Pull Requests to review and approve changes before deploying.
- DRY Terraform Stacks: Generate Terraform provider and backend configuration in stacks.
- OpenID Connect (OIDC): Allows GitHub Actions workflows to access AWS resources without storing long-lived GitHub secrets.
- Terraform S3 Remote State Backend: Terraform Remote State Storage and State Locking with AWS S3 and DynamoDB.
- Terramate Cloud Integration: Pushes data to Terramate Cloud for observability, asset management, drift management, and Slack notifications.
- Multi-Environment Support: Built-in support for staging and production environments with separate AWS accounts.
- Drift Detection and Reconciliation: Automated drift detection and reconciliation workflows to maintain infrastructure consistency.
This template creates a complete AWS infrastructure with the following components:
-
VPC (Virtual Private Cloud)
- A dedicated network space for your infrastructure
- Public and private subnets across multiple availability zones
- NAT Gateway for outbound internet access from private subnets
- Internet Gateway for inbound internet access to public subnets
-
EKS (Elastic Kubernetes Service)
- A managed Kubernetes cluster in auto-mode
- Node groups for running your containerized applications
- Auto-scaling capabilities based on workload demand
- Integration with AWS IAM for authentication and authorization
-
Sample Applications
- Two sample web applications deployed in the EKS cluster
- Applications are accessible via Load Balancer services
- Demonstrates best practices for Kubernetes deployments
The infrastructure is designed to be:
- Highly available across multiple availability zones
- Secure with proper network isolation
- Scalable to meet growing demands
- Easy to maintain with clear separation of concerns
Click the Use this template button to create your own repository in a GitHub account or organization you manage, and let's get started.
Ensure you have the following prerequisites set up by running the commands below:
-
Install asdf: Follow the official guide.
-
Install required
asdf
plugins for Terramate, Terraform, and OpenTofu:asdf plugin add terramate && \ asdf plugin add terraform && \ asdf plugin add opentofu && \ asdf plugin add pre-commit && \ asdf install
-
(Optional) If you need to create a Terraform State Bucket and Workload Identity Provider, you need to configure your AWS credentials using one of the supported authentication mechanisms. (We recommend you use aws-vault for secure authentication.)
-
(Optional) Install pre-commit hooks
We recommend installing the pre-commit hooks in this repository to enable a seamless development flow. The hooks guarantee that your Terramate and Terraform code is always up-to-date and well-formatted when committing changes to the repository.
pre-commit install
The project is organized into the following main directories:
stacks/terraform/
: Contains all Terraform stacksenvs/
: Environment-specific configurationsstg/
: Staging environmentconfig.tm.hcl
: Environment-specific configuration for all sub-stacksvpc/
: Network infrastructure stackmain.tf
: VPC, subnets, and networking components
eks/
: Kubernetes cluster stackmain.tf
: EKS cluster and node groupsapps/
: Application deployments (with namespace definition)app1/
: First application stackapp2/
: Second application stack
prd/
: Production environment (similar structure to stg)
workflows.tm.hcl
: Reusable Terramate scripts for common operations
The stacks are designed to be deployed in a specific order using Terramate's stack ordering capabilities. This allows you to:
- Deploy infrastructure components (VPC then EKS cluster) first
- Create namespaces
- Deploy applications in the correct order
- All while maintaining a single state file per stack
This approach enables you to:
- Deploy multiple related resources in a single PR
- Maintain clear dependencies between resources
- Keep your infrastructure code organized and modular
- Reduce the number of PRs needed for related changes
After testing changes in the staging environment, you can promote them to production using the promote
Terramate script. This script is designed to help you safely propagate changes from staging to production.
To use the promote script:
-
Navigate to the staging environment directory:
cd stacks/terraform/envs/stg
-
Run the promote script:
terramate script run promote
Note: The promote script is only available when executed from within the staging environment directory. If you try to run it from any other location, it will print a warning message and exit.
The promote script will:
- Compare the changes between staging and production and sync any differences (including deleting files)
- Run
terramate generate
to make sure any config differences are applied correctly
This repository comes with a pre-configured Terraform S3 State Bucket, DynamoDB Lock Table and Workload Identity Provider to enable keyless authentication from GitHub Actions to AWS.
- Navigate to the
config.tm.hcl
file in your project's root directory. - Replace the default Terraform State Bucket name with a name of your choice.
globals "terraform" "backend" {
bucket = "any-name-you-want"
region = "us-east-1"
}
- Update the GitHub repository name for the workload identity provider with your repository (
<githubuserororganization/repository-name>
).
globals "aws" "oidc" {
github_repositories = [
"your-github-username-or-organization/repository-name",
]
}
Generate Terraform files using Terramate:
terramate generate
The terramate generate
command generates files/code in stacks and helps to keep your stacks DRY. In the _bootstrap/terraform-state-bucket
directory, the config.tm.hcl
file includes a generate_hcl
block which specifies the HCL code that will be generated by the generate command.
Because we run the command without specifying the context, the default stack
context is used and generates code relative to the stack where the config file is defined(terraform-state-bucket
stack in this case). The generated code can be located in the _main.tf
file within the same directory.
To deploy the generated resources to AWS, use the following commands:
terramate run -C _bootstrap terraform init
terramate run -C _bootstrap terraform apply
Now that we have the Terraform State Bucket deployed, we want to move the state of the newly deployed bucket and workload identity provider into the bucket.
-
Remove
tags = ["no-backend"]
fromstack.tm.hcl
files of the_bootstrap/oidc-aws-github
and/bootstrap/terraform-state-bucket
directories. -
Generate Terraform configuration files:
terramate generate
This will create a _backend.tf
file in both stack directories.
- Initialize Terraform for state migration:
terramate run -C _bootstrap terraform init
This command will move the state of deployed stacks to the S3 bucket.
- Visit Terramate Cloud
- Sign up for a new account or log in
- Create a new organization
- Follow the setup wizard to configure your organization
- In Terramate Cloud, navigate to your organization settings
- Go to the "Integrations" section
- Add a new Slack integration
The project includes several pre-configured Terramate scripts in stacks/terraform/workflows.tm.hcl
(see Terramate Scripts Documentation):
init
: Initialize Terraformpreview
: Create a preview of changes and sync to Terramate Clouddeploy
: Run a full deployment cycle and sync results to Terramate Clouddrift detect
: Check for infrastructure drift and sync results to Terramate Clouddrift reconcile
: Optionally reconcile detected driftterraform render
: Render Terraform plan output
The repository includes three main GitHub Actions workflows. All workflows support multiple environments (stg, prd) using a workflow matrix.
-
Preview Workflow (
preview.yml
):- Runs on pull requests
- Performs Terraform validation and planning
- Syncs preview results to Terramate Cloud
-
Deploy Workflow (
deploy.yml
):- Runs on merges to the main branch
- Applies approved changes
- Syncs deployment results to Terramate Cloud
-
Drift Detection Workflow (
drift-detection.yml
):- Runs on a schedule
- Detects infrastructure drift
- Creates pull requests for drift reconciliation
- Sends notifications via Slack
- Policies with OPA and/or Sentinel
- Implement checkov, trivy, terrascan
- Implement infracost