Skip to content

Commit a673f82

Browse files
committed
refactor: extract all hard coded stuff and calculated stuff in locals
1 parent ca6e47e commit a673f82

File tree

3 files changed

+77
-47
lines changed

3 files changed

+77
-47
lines changed

locals.tf

+40-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,44 @@
11
locals {
2-
google_oidc_endpoint = "https://accounts.google.com"
3-
container_port = 4141
4-
alb_ip_target_type = "ip"
5-
ecs_container_definations_name = "atlantis"
6-
alb_system_name = "atlantis"
7-
domain_parts = split(".", var.atlantis_url)
8-
base_domain = join(".", slice(local.domain_parts, length(local.domain_parts) - 2, length(local.domain_parts)))
9-
sub_domain = join(".", slice(local.domain_parts, 0, length(local.domain_parts) - 2))
10-
ecs_task_definition_family = "atlantis"
11-
task_definition_network_mode = "awsvpc"
12-
load_balancer_internal = "false"
13-
ecs_service_name = "atlantis"
14-
container_memory_reservation = 10
15-
authenticate_oidc_issuer = "https://accounts.google.com"
16-
authenticate_oidc_user_info_endpoint = "https://openidconnect.googleapis.com/v1/userinfo"
17-
authenticate_oidc_token_endpoint = "https://oauth2.googleapis.com/token"
18-
authenticate_oidc_authorization_endpoint = "https://accounts.google.com/o/oauth2/v2/auth"
2+
google_oidc_endpoint = "https://accounts.google.com"
3+
container_port = 4141
4+
alb_ip_target_type = "ip"
5+
ecs_container_definations_name = "atlantis"
6+
alb_system_name = "atlantis"
7+
domain_parts = split(".", var.atlantis_url)
8+
base_domain = join(".", slice(local.domain_parts, length(local.domain_parts) - 2, length(local.domain_parts)))
9+
sub_domain = join(".", slice(local.domain_parts, 0, length(local.domain_parts) - 2))
10+
ecs_task_definition_family = "atlantis"
11+
task_definition_network_mode = "awsvpc"
12+
load_balancer_internal = "false"
13+
ecs_service_name = "atlantis"
14+
container_memory_reservation = 10
15+
authenticate_oidc_issuer = "https://accounts.google.com"
16+
authenticate_oidc_user_info_endpoint = "https://openidconnect.googleapis.com/v1/userinfo"
17+
authenticate_oidc_token_endpoint = "https://oauth2.googleapis.com/token"
18+
authenticate_oidc_authorization_endpoint = "https://accounts.google.com/o/oauth2/v2/auth"
19+
ecs_task_assume_policy_actions = ["sts:AssumeRole"]
20+
ecs_task_assume_policy_principal_type = "Service"
21+
ecs_task_assume_policy_principal_identifiers = ["ecs-tasks.amazonaws.com"]
22+
acm_certificate_statuses = ["ISSUED"]
23+
default_desired_count = 1
24+
target_group_name = "atlantis-target-group"
25+
target_group_protocol = "HTTP"
26+
listener_protocol = "HTTPS"
27+
listener_port = 443
28+
default_action_type = "fixed-response"
29+
fixed_response_content_type = "application/json"
30+
fixed_response_message_body = "Unauthorised"
31+
fixed_response_status_code = 404
32+
listener_name = "https-listener"
33+
listener_priority = 10
34+
authenticate_oidc_type = "authenticate-oidc"
35+
authenticate_oidc_scope = "openid email"
36+
authenticate_oidc_on_unauthenticated_request = "authenticate"
37+
forward_action_type = "forward"
38+
http_listener_priority = 1
39+
path_pattern_values = ["/events"]
40+
http_request_method_values = ["POST"]
41+
create_capacity_provider = false
1942

2043
env_variables = [
2144
{

main.tf

+29-30
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,28 @@ data "aws_vpc" "selected" {
88
}
99

1010
data "aws_ecs_cluster" "default" {
11-
cluster_name = "default"
11+
cluster_name = var.ecs_cluster_name
1212
}
1313

1414
data "aws_iam_policy_document" "ecs_task_assume_policy" {
1515
statement {
16-
actions = ["sts:AssumeRole"]
16+
actions = local.ecs_task_assume_policy_actions
1717
principals {
18-
type = "Service"
19-
identifiers = ["ecs-tasks.amazonaws.com"]
18+
type = local.ecs_task_assume_policy_principal_type
19+
identifiers = local.ecs_task_assume_policy_principal_identifiers
2020
}
2121
}
2222
}
2323

2424
data "aws_acm_certificate" "base_domain_certificate" {
2525
domain = local.base_domain
26-
statuses = ["ISSUED"]
26+
statuses = local.acm_certificate_statuses
2727
}
2828

2929
data "aws_route53_zone" "zone" {
3030
name = local.base_domain
3131
}
3232

33-
3433
module "ecs_deployment" {
3534
source = "infraspecdev/ecs-deployment/aws"
3635
version = "3.0.1"
@@ -63,11 +62,11 @@ module "ecs_deployment" {
6362
}
6463
service = {
6564
name = local.ecs_service_name
66-
desired_count = var.ecs_service_desired_count != null ? var.ecs_service_desired_count : 1
65+
desired_count = var.ecs_service_desired_count != null ? var.ecs_service_desired_count : local.default_desired_count
6766
load_balancer = [{
6867
container_name = local.ecs_container_definations_name
6968
container_port = local.container_port
70-
target_group = "atlantis-target-group"
69+
target_group = local.target_group_name
7170
}]
7271

7372
network_configuration = {
@@ -86,25 +85,25 @@ module "ecs_deployment" {
8685
atlantis-target-group = {
8786
name = format("%s-%s-ip", local.alb_system_name, terraform.workspace)
8887
port = local.container_port
89-
protocol = "HTTP"
88+
protocol = local.target_group_protocol
9089
target_type = local.alb_ip_target_type
9190
}
9291
}
9392

9493
listeners = {
9594
https-listener = {
96-
protocol = "HTTPS"
97-
port = 443
95+
protocol = local.listener_protocol
96+
port = local.listener_port
9897
certificate_arn = data.aws_acm_certificate.base_domain_certificate.arn
9998

10099
default_action = [
101100
{
102-
type = "fixed-response"
103-
target_group = "atlantis-target-group"
101+
type = local.default_action_type
102+
target_group = local.target_group_name
104103
fixed_response = {
105-
content_type = "application/json"
106-
message_body = "Unauthorised"
107-
status_code = 404
104+
content_type = local.fixed_response_content_type
105+
message_body = local.fixed_response_message_body
106+
status_code = local.fixed_response_status_code
108107
}
109108
}
110109
]
@@ -113,8 +112,8 @@ module "ecs_deployment" {
113112

114113
listener_rules = {
115114
https-listener-rules = {
116-
listener = "https-listener"
117-
priority = 10
115+
listener = local.listener_name
116+
priority = local.listener_priority
118117

119118
condition = [
120119
{
@@ -126,39 +125,39 @@ module "ecs_deployment" {
126125

127126
action = [
128127
{
129-
type = "authenticate-oidc"
128+
type = local.authenticate_oidc_type
130129

131130
authenticate_oidc = {
132131
authorization_endpoint = local.authenticate_oidc_authorization_endpoint
133132
token_endpoint = local.authenticate_oidc_token_endpoint
134133
user_info_endpoint = local.authenticate_oidc_user_info_endpoint
135134
issuer = local.authenticate_oidc_issuer
136135
session_cookie_name = format("TOKEN-OIDC-%s", data.aws_ssm_parameter.environment["ATLANTIS_GOOGLE_CLIENT_ID"].value)
137-
scope = "openid email"
138-
on_unauthenticated_request = "authenticate"
136+
scope = local.authenticate_oidc_scope
137+
on_unauthenticated_request = local.authenticate_oidc_on_unauthenticated_request
139138
client_id = data.aws_ssm_parameter.environment["ATLANTIS_GOOGLE_CLIENT_ID"].value
140139
client_secret = data.aws_ssm_parameter.environment["ATLANTIS_GOOGLE_CLIENT_SECRET"].value
141140
}
142141
},
143142
{
144-
target_group = "atlantis-target-group"
145-
type = "forward"
143+
target_group = local.target_group_name
144+
type = local.forward_action_type
146145
}
147146
]
148147
},
149148
http-listener-rules = {
150-
listener = "https-listener"
151-
priority = 1
149+
listener = local.listener_name
150+
priority = local.http_listener_priority
152151

153152
condition = [
154153
{
155154
path_pattern = {
156-
values = ["/events"]
155+
values = local.path_pattern_values
157156
}
158157
},
159158
{
160159
http_request_method = {
161-
values = ["POST"]
160+
values = local.http_request_method_values
162161
}
163162
},
164163
{
@@ -170,13 +169,13 @@ module "ecs_deployment" {
170169

171170
action = [
172171
{
173-
target_group = "atlantis-target-group"
174-
type = "forward"
172+
target_group = local.target_group_name
173+
type = local.forward_action_type
175174
}
176175
]
177176
}
178177
}
179178
}
180179

181-
create_capacity_provider = false
180+
create_capacity_provider = local.create_capacity_provider
182181
}

variables.tf

+8
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,17 @@ variable "atlantis_gh_user" {
4949
variable "thumbprint_list" {
5050
description = "The thumbprint of the OIDC provider"
5151
type = list(string)
52+
default = ["e252aa6e92432f32cbc1b182056627c239652678"]
5253
}
5354

5455
variable "atlantis_docker_image" {
5556
description = "The Docker image to use for the Atlantis server"
5657
type = string
58+
default = "ghcr.io/runatlantis/atlantis:v0.23.1"
59+
}
60+
61+
variable "ecs_cluster_name" {
62+
description = "The name of the ECS cluster"
63+
type = string
64+
default = "default"
5765
}

0 commit comments

Comments
 (0)