Skip to content

Commit c1ee055

Browse files
authored
ci(terraform): Add matrix for multi environment deployment (#35)
1 parent f523f29 commit c1ee055

25 files changed

+700
-375
lines changed

.envrc

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

.github/README.md

Lines changed: 93 additions & 55 deletions
Large diffs are not rendered by default.

.github/workflows/terraform-ci.yaml

Lines changed: 100 additions & 207 deletions
Large diffs are not rendered by default.

.github/workflows/terraform-docs.yaml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
name: Terraform Docs
2+
run-name: ${{ github.event.workflow_run.display_title }}
23

34
on:
4-
pull_request:
5-
types: [closed]
6-
branches: [main]
7-
paths:
8-
- "**/*.tf"
9-
- "**/*.tfvars"
10-
- "**/*.tftpl"
5+
workflow_run:
6+
workflows: [Terraform CI]
7+
types: [completed]
118

129
# Disable permissions for all available scopes
1310
permissions: {}
1411

1512
jobs:
1613
terraform-docs:
17-
if: ${{ github.event.pull_request.merged == true }}
14+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
1815
name: Terraform Docs
1916
uses: 3ware/workflows/.github/workflows/terraform-docs.yaml@7880d6b986d1d689f5d219e901b863f1378fea9c # v4.4.0
2017
secrets: inherit
2118
with:
22-
tf-directory: terraform/development
19+
tf-directory: terraform

.github/workflows/wait-for-checks.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
name: Checks
2+
run-name: ${{ github.event_name == 'merge_group' && github.event.merge_group.head_commit.message || ''}}
23

34
on:
45
pull_request:
56
branches: [main]
6-
types: [opened, edited, synchronize]
7+
merge_group:
8+
types: [checks_requested]
79

810
# Disable permissions for all available scopes
911
permissions: {}

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ override.tf.json
2727

2828
# Ignore CLI configuration files
2929
.terraformrc
30-
terraform.rc
30+
terraform.rc
31+
32+
# Ignore direnv files
33+
.envrc

.trunk/trunk.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
33
version: 0.1
44
cli:
5-
version: 1.22.6
5+
version: 1.22.7
66
# Trunk provides extensibility via plugins. (https://docs.trunk.io/plugins)
77
plugins:
88
sources:
99
- id: trunk
10-
ref: v1.6.3
10+
ref: v1.6.4
1111
uri: https://github.com/trunk-io/plugins
1212
# Many linters and tools depend on runtimes - configure them here. (https://docs.trunk.io/runtimes)
1313
runtimes:
@@ -17,19 +17,19 @@ runtimes:
1717
# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
1818
lint:
1919
disabled:
20+
- checkov
21+
- trivy
2022
- regal
2123
enabled:
22-
23-
24+
2425
2526
2627
commands: [fmt, validate]
2728
28-
2929
- git-diff-check
3030
3131
32-
- trufflehog@3.82.8
32+
- trufflehog@3.83.2
3333
3434
definitions:
3535
- name: tflint
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
development_aws_account_id: ENC[AES256_GCM,data:DJFVyfC1L2sU3Rg3,iv:/tY8GG2lda8IP2ITG72Xh4sMs+Tt4VNAP1Qb1LdTZoM=,tag:GwieQ56POhsSxjexHYx9fg==,type:int]
2+
sops:
3+
kms: []
4+
gcp_kms: []
5+
azure_kv: []
6+
hc_vault: []
7+
age:
8+
- recipient: age1wpy4kcrhan5ffwwv9dke50v9e302lhravg2njkze9qu33xgnr42q9p2d22
9+
enc: |
10+
-----BEGIN AGE ENCRYPTED FILE-----
11+
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBoSml5M1p6MHIzaitVaGVW
12+
OTBZNVBQRmVXU2FENXFnb2UvdFNFYTJhSG1ZCk5FSEUvZFZiVUJFdzVXdC9hclNj
13+
T0NicFFSZ212QkdlRTFuQ3lSZUpVMWsKLS0tIFVta2pYaC9VMXlnbU1KNW1Zcnk4
14+
aXRkOUhWakRBUEtxSWdFVkw2R3ZscFEKm9zke6+CQFYyFohhm2XLMqW3ffkPs10d
15+
Lk5rBlAmGTsneyVHNdBrF/zjD6nKOqs7MZudWX+rZFgeSBnSjxo8qA==
16+
-----END AGE ENCRYPTED FILE-----
17+
lastmodified: "2024-11-01T11:23:38Z"
18+
mac: ENC[AES256_GCM,data:UItGJx1VhU+OtH0B7wcrBdiLjS3sZQwvZ+9pRLA7alkUU56AkdhSGHIIOrstdO8Nnb3Yjt4mPpwyKHUx1r3eIczQUGqUxa3h8kaF7fzyhf3RkJdLsnYRxvjvk7iHRKr/Ey0wtFFa5cGo7bSj2Ar0RTARq1pglLGJ6kRDjMzWaJo=,iv:HHzgmVOWktFiW5YaFEFyMy2wlhPc/v5v1+ccLpRZsB0=,tag:ipbezFjdtACwvEosS+p0Lg==,type:str]
19+
pgp: []
20+
unencrypted_suffix: _unencrypted
21+
version: 3.9.1

terraform/development/.terraform.lock.hcl

Lines changed: 16 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform/development/outputs.tf

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
output "aws_account_id" {
2-
description = "AWS account number resources are deployed into"
3-
value = data.aws_caller_identity.current.account_id
4-
sensitive = true
5-
}
6-
7-
output "default_tags" {
8-
description = "A map of default tags applied to resources."
9-
value = data.aws_default_tags.this.tags
10-
}
11-
121
output "grafana_ip" {
132
description = "The connection details of the grafana server."
143
value = "http://${aws_instance.grafana_server.public_ip}:3000"

terraform/development/providers.tf

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,14 @@
1-
terraform {
2-
# Must be above 1.9.0 to allow cross-object referencing for input variable validations
3-
required_version = ">=1.9.0, <=2.0.0"
4-
required_providers {
5-
aws = {
6-
source = "hashicorp/aws"
7-
version = "~>5.69.0"
8-
}
9-
# http = {
10-
# source = "hashicorp/http"
11-
# version = "~>3.4.5"
12-
# }
13-
}
14-
}
15-
16-
locals {
17-
valid_account_no = {
18-
development = "713881824542"
19-
production = "535002868697"
20-
}
21-
}
22-
23-
data "aws_caller_identity" "current" {
24-
lifecycle {
25-
postcondition {
26-
condition = contains(values(local.valid_account_no), self.id)
27-
error_message = format(
28-
"Invalid AWS account ID specified. Received: '%s', Require: '%s'.\n%s",
29-
self.id,
30-
join(", ", values(local.valid_account_no)),
31-
"Configure AWS credentials to assume the correct role."
32-
)
33-
}
34-
}
35-
}
36-
37-
locals {
38-
# Defines a list of permitted environment tag values. Used by the postcondition in the aws_default_tags data source
39-
# to validate the environment tag extrapolated from the workspace name in data.tf
40-
valid_environment = ["development", "production"]
41-
}
42-
43-
data "aws_default_tags" "this" {
44-
lifecycle {
45-
postcondition {
46-
condition = anytrue([
47-
for tag in values(self.tags) : contains(local.valid_environment, tag)
48-
])
49-
error_message = format(
50-
"Invalid environment tag specified. Received: '%s', Require: '%s'.\n%s",
51-
self.tags["3ware:environment"],
52-
join(", ", local.valid_environment),
53-
"Rename workspace with a valid environment suffix."
54-
)
55-
}
56-
}
1+
data "sops_file" "aws_account_id" {
2+
source_file = "${path.module}/.sops-files/sensitive.enc.yaml"
573
}
584

595
provider "aws" {
60-
region = var.region
6+
region = var.region
7+
allowed_account_ids = [data.sops_file.aws_account_id.data["${var.environment}_aws_account_id"]]
618
default_tags {
629
tags = {
6310
"3ware:project-id" = var.project_id
64-
"3ware:environment" = local.environment
11+
"3ware:environment" = var.environment
6512
"3ware:managed-by-terraform" = true
6613
"3ware:workspace" = terraform.workspace
6714
}

terraform/development/terraform.tfvars

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
environment = "development"
12
instance_type = "t2.micro"
23
project_id = "gitops-2024"
34
region = "us-east-1"

terraform/development/variables.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
locals {
2+
valid_environment = ["development"]
3+
}
4+
5+
variable "environment" {
6+
description = "(Required) Terraform deployment environment"
7+
type = string
8+
9+
validation {
10+
condition = contains(local.valid_environment, var.environment)
11+
error_message = format(
12+
"Invalid environment provided. Received: '%s', Require: '%v'.\n%s",
13+
var.environment,
14+
join(", ", local.valid_environment),
15+
"Change the environment variable value to one that is permitted."
16+
)
17+
}
18+
}
19+
20+
121
locals {
222
valid_instance_types = ["t2.micro"]
323
}

terraform/development/versions.tf

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
terraform {
2-
required_version = ">= 1.8.2"
3-
2+
# Must be above 1.9.0 to allow cross-object referencing for input variable validations
3+
required_version = ">=1.9.0, < 2.0.0"
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = "~>5.69.0"
8+
}
9+
# http = {
10+
# source = "hashicorp/http"
11+
# version = "~>3.4.5"
12+
# }
13+
sops = {
14+
source = "carlpett/sops"
15+
version = "~> 1.1.1"
16+
}
17+
}
418
cloud {
519
organization = "3ware"
620
hostname = "app.terraform.io"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
production_aws_account_id: ENC[AES256_GCM,data:OGQZoe74L66XGHe5,iv:FI81M4+97WLF5KzLjA3H7AkaFC4uDx+ooS0vXGv4scM=,tag:K0yrBflkL/cObMnb+HWVIw==,type:int]
2+
sops:
3+
kms: []
4+
gcp_kms: []
5+
azure_kv: []
6+
hc_vault: []
7+
age:
8+
- recipient: age1wpy4kcrhan5ffwwv9dke50v9e302lhravg2njkze9qu33xgnr42q9p2d22
9+
enc: |
10+
-----BEGIN AGE ENCRYPTED FILE-----
11+
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSAvbDlpYlJsOHE1SVd4MWZj
12+
eXNuZ1dyVTVWbGZqZXpUTWdRWnc5TnFOd21zCkd2TkQrUWhwaWhJaThjZmVBZGYw
13+
ck1WRkhtK0ZNYmFmaXNMQXQweVFPZFkKLS0tIGFCZytBUy9SbnNkbUFIVCtKZWJH
14+
Q3dVbjg3NXZPME9sdUtEYzVlcGhPbG8KyuJvku8qDbnmOm2zG94RthEQM8ML2U3n
15+
YFfHPYaKVQydgbb6lziQywZja2oJICXM1zRbGvadQNpN4VH6D7OFfw==
16+
-----END AGE ENCRYPTED FILE-----
17+
lastmodified: "2024-11-01T14:56:19Z"
18+
mac: ENC[AES256_GCM,data:OVSNjOmC9onsy5pQPO7nIQOsDXkY3CiJ611x+Etun5XMqVpPFaVqv6xsQeNXNth4bc0uqui8zH6hGJ8TZ6Y5idfzej3fqOJ0Qz1VoLKgYNSnUsQJ/LtIKTrVaJv6zMqIrkcTwC+4Xva+Rrb538XavQ/J6PP8JOez2ako5E3BYpc=,iv:SuPbeZ1MBySAKnMY3gryyOzX3cZ0ajblmfYMBqA+zy4=,tag:chYjPV86oIqUGm+b3XHpuQ==,type:str]
19+
pgp: []
20+
unencrypted_suffix: _unencrypted
21+
version: 3.9.1

terraform/production/.terraform.lock.hcl

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)