This repository provides a scaffold for managing multiple environments and/or accounts by leveraging a combination of native Terraform workspaces and modules. The primary focus is to adhere to DRY (Don't Repeat Yourself) principles, eliminating redundancy and duplication of files and code between environments within the same account. By leveraging this approach, the repository provides a scalable, maintainable infrastructure-as-code solution without relying on external tools like Terragrunt.
- Terraform
- AWS CLI configured with appropriate credentials in
~/.aws/credentials
- Git
- Bash - Native support for macOS and Linux. For Windows users, you can use Git Bash.
-
Update AWS Credentials: Ensure your local
~/.aws/credentials
file is updated with the correct AWS profile name. -
Clone the Repository: Make a copy of this repository to your local environment.
-
Choose Backend State: Decide whether to use a local or remote AWS S3 + DynamoDB backend state. Navigate to the corresponding folder (local or remote).
-
Update Configuration Files:
- For remote backend, update both
variables.tf
and the**.tfbackend
files. - For local backend, only
variables.tf
needs to be updated.
- For remote backend, update both
-
Run Terraform: Navigate to the appropriate Terraform folder based on your backend choice, then execute
./start.sh production
or./start.sh stage
depending on the environment you want to test.
- Make use of conditionals to define names and specifics per environment
local.environment == "production" ? "www.test-website.com" : "stage.test-website.com"
- If you have a resource that should be created on one environment only, you may use count:
count = local.environment == "production" ? 1 : 0
. If0
, it doesn't create it. Just remember that if you implement the count parameter, when calling that resources you need to account for itaws_resource.resource_name[0].arn
. - This can also be used with loops:
for_each = local.environment == "production" ? [] : [1]
Contributions are welcome! Please open an issue or submit a pull request for any changes.
This project is licensed under the MIT License. See the LICENSE file for details.