Skip to content

Commit a6c8452

Browse files
committed
master
master
1 parent 2c4bcaa commit a6c8452

File tree

12 files changed

+96
-110
lines changed

12 files changed

+96
-110
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.terraform
2+
*tfstate*

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Multiple Environments in Terraform
2+
3+
This is an example repository of Terraform code showing how to deploy and revert infrastructure changes for multiple environments with Terraform Enterprise & GitHub.
4+
5+
## Setup
6+
7+
To setup a multi-environment deployment workflow, create a new Terraform Enterprise environment for each.
8+
9+
1. Fork the [hashicorp/multiple-envs](https://github.com/hashicorp/multiple-envs) repo
10+
1. [Link](https://atlas.hashicorp.com/settings/connections) your Atlas account to GitHub
11+
1. Use the [Import tool](https://atlas.hashicorp.com/configurations/import) to create new environments for `demo-dev`, `demo-staging`, and `demo-prod`
12+
- **Name the environment**: `demo-dev`, `demo-staging`, or `demo-prod` depending on the environment you're creating
13+
- **GitHub repository**: `multiple-envs`
14+
1. Go to **Variables** in the left navigation of each environment
15+
- Add the below **Environment Variables**
16+
- `AWS_ACCESS_KEY_ID`: `YOUR_AWS_ACCESS_KEY_ID`
17+
- `AWS_SECRET_ACCESS_KEY`: `YOUR_AWS_SECRET_ACCESS_KEY`
18+
- `AWS_DEFAULT_REGION`: `us-east-1`
19+
- Add the below **Terraform Variables** to override the defaults to be environment specific
20+
- `name`: `demo-dev`, `demo-staging`, or `demo-prod` depending on the environment you're creating
21+
- The placeholder variables allow you to remove environment specific or secret/sensitive variables from version control
22+
1. Click **Queue plan** and **Confirm & apply** for each environment to deploy the infrastructure
23+
24+
## Deployment Workflow
25+
26+
Below is an example deployment workflow you can use to version changes through each environment in a collaborative way.
27+
28+
1. Create and push a feature branch with a proposed change, for example, adding a new tag to the security group in [network.tf](modules/network/network.tf#L26)
29+
1. Create a pull request for your feature branch against `master`
30+
- You should see 3 Terraform plan checks that have changes
31+
1. Deploy your feature branch to `demo-dev`
32+
- Go to **Integrations** in the left navigation of the `demo-dev` environment and enter your feature branch name into **GitHub branch**, then click **Update GitHub settings**
33+
- This will automatically trigger a plan that you can **Confirm & Apply** in **Runs** to deploy
34+
1. Deploy your feature branch to `demo-staging`
35+
- Go to **Integrations** in the left navigation of the `demo-staging` environment and enter your feature branch name into **GitHub branch**, then click **Update GitHub settings**
36+
- This will automatically trigger a plan that you can **Confirm & Apply** in **Runs** to deploy
37+
1. Deploy your feature branch to `demo-prod` now that you've tested the change in `demo-dev` and `demo-staging`
38+
- Merge your pull request
39+
- This will trigger a Terraform plan in **Runs** of the `demo-prod` environment that you can **Confirm & Apply** to deploy
40+
1. Remove your feature branch from **GitHub branch** in **Integrations** of `demo-dev` and `demo-staging` and click **Update GitHub settings**, there should be no changes
41+
42+
## Revert Workflow
43+
44+
In the event that one of your commits should need to be reverted, you can follow the below steps to achieve this.
45+
46+
1. Go to **Runs** in the left navigation of the `demo-prod` environment and click on the run that you would like to revert
47+
1. On the top right, click the Git SHA link that triggered this run
48+
1. In GitHub, navigate to the PR that was associated with this commit and click **Revert**
49+
1. Create a pull request to revert the change, this should have Terraform plan change checks across all 3 environments
50+
1. Upon success of the Terraform plan checks, merge the pull request
51+
1. This will trigger a Terraform plan in all 3 environments that you can **Confirm & Apply** to revert the change

main.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
variable "name" { default = "REPLACE_ME" }
2+
variable "cidr" { default = "10.139.0.0/16" }
3+
4+
module "network" {
5+
source = "modules/network"
6+
7+
name = "${var.name}"
8+
cidr = "${var.cidr}"
9+
}
10+
11+
output "env" { value = "${var.name}" }
12+
output "vpc_cidr" { value = "${module.network.vpc_cidr}" }

modules/network/network.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
variable "name" { default = "network" }
2+
variable "cidr" { }
3+
4+
resource "aws_vpc" "vpc" {
5+
cidr_block = "${var.cidr}"
6+
enable_dns_support = true
7+
enable_dns_hostnames = true
8+
9+
tags {
10+
Name = "${var.name}"
11+
}
12+
}
13+
14+
resource "aws_security_group" "allow" {
15+
name = "allow-${var.name}"
16+
vpc_id = "${aws_vpc.vpc.id}"
17+
18+
ingress {
19+
from_port = 22
20+
to_port = 22
21+
protocol = "tcp"
22+
cidr_blocks = ["0.0.0.0/0"]
23+
}
24+
25+
tags {
26+
Name = "${var.name}"
27+
}
28+
}
29+
30+
output "vpc_id" { value = "${aws_vpc.vpc.id}" }
31+
output "vpc_cidr" { value = "${aws_vpc.vpc.cidr_block}" }

modules/vpc/vpc.tf

Lines changed: 0 additions & 13 deletions
This file was deleted.

prod/.terraform/terraform.tfstate

Lines changed: 0 additions & 19 deletions
This file was deleted.

prod/prod.tf

Lines changed: 0 additions & 24 deletions
This file was deleted.

prod/prod.tfvars

Lines changed: 0 additions & 2 deletions
This file was deleted.

qa/qa.tf

Lines changed: 0 additions & 24 deletions
This file was deleted.

qa/qa.tfvars

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)