Skip to content

Commit 39acf80

Browse files
author
rohit-ng
committed
refactor: segregated local variables
1 parent 76df659 commit 39acf80

File tree

5 files changed

+70
-67
lines changed

5 files changed

+70
-67
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ This command will display the values of the stored parameters, ensuring they hav
8787
|------|--------|---------|
8888
| <a name="module_ecs_exec_role"></a> [ecs\_exec\_role](#module\_ecs\_exec\_role) | ./modules/iam | n/a |
8989
| <a name="module_ecs_kong"></a> [ecs\_kong](#module\_ecs\_kong) | infraspecdev/ecs-deployment/aws | ~> 2.0.0 |
90-
| <a name="module_ecs_node_security_group"></a> [ecs\_node\_security\_group](#module\_ecs\_node\_security\_group) | terraform-aws-modules/security-group/aws | ~> 5.1.2 |
9190
| <a name="module_ecs_task_security_group"></a> [ecs\_task\_security\_group](#module\_ecs\_task\_security\_group) | terraform-aws-modules/security-group/aws | ~> 5.1.2 |
9291
| <a name="module_github_runner"></a> [github\_runner](#module\_github\_runner) | ./modules/github-runner | n/a |
9392
| <a name="module_internal_alb_kong"></a> [internal\_alb\_kong](#module\_internal\_alb\_kong) | infraspecdev/ecs-deployment/aws//modules/alb | ~> 2.0.0 |
@@ -103,7 +102,6 @@ This command will display the values of the stored parameters, ensuring they hav
103102
| Name | Type |
104103
|------|------|
105104
| [aws_autoscaling_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/autoscaling_group) | data source |
106-
| [aws_ssm_parameter.ecs_node_ami](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssm_parameter) | data source |
107105
| [aws_ssm_parameter.github](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssm_parameter) | data source |
108106
| [aws_ssm_parameter.rds](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssm_parameter) | data source |
109107
| [aws_vpc.vpc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |

data.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ data "aws_vpc" "vpc" {
33
}
44

55
data "aws_ssm_parameter" "rds" {
6-
for_each = toset(local.rds_parameters)
6+
for_each = toset(local.ssm_parameters.rds)
77
name = "/rds/${each.value}"
88
with_decryption = true
99
}
1010

1111
data "aws_ssm_parameter" "github" {
12-
for_each = toset(local.github_parameters)
12+
for_each = toset(local.ssm_parameters.github)
1313
name = "/github-action/${each.value}"
1414
with_decryption = true
1515
}

locals.tf

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
locals {
2-
name = "kong-postgres"
3-
db_identifier = "${local.name}-01"
4-
rds_engine = "postgres"
5-
storage_encrypted = true
6-
storage_type = "gp3"
72

8-
postgres = {
3+
ssm_parameters = {
4+
rds = [
5+
"POSTGRES_USERNAME",
6+
"POSTGRES_PASSWORD",
7+
"POSTGRES_DB_NAME"
8+
]
9+
github = [
10+
"GITHUB_ORG",
11+
"GITHUB_REPO",
12+
"GITHUB_TOKEN"
13+
]
14+
}
15+
16+
rds = {
17+
name = "kong-postgres"
18+
db_identifier = "kong-postgres-01"
19+
engine = "postgres"
20+
storage_encrypted = true
21+
storage_type = "gp3"
922
engine_version = 16.3
1023
engine_family = "postgres16"
1124
major_engine_version = 16
1225
port = 5432
26+
sg_name = "kong-postgres"
27+
postgres_username = data.aws_ssm_parameter.rds["POSTGRES_USERNAME"].value
28+
postgres_password = data.aws_ssm_parameter.rds["POSTGRES_PASSWORD"].value
29+
postgres_db_name = data.aws_ssm_parameter.rds["POSTGRES_DB_NAME"].value
1330
}
1431

1532
ecs = {
@@ -29,54 +46,42 @@ locals {
2946
alb_sg_name = "kong"
3047
ecs_task_sg_name = "kong"
3148
commands = ["/bin/sh", "-c", "kong migrations bootstrap && ulimit -n 4096 && kong start"]
32-
portMappings = [
33-
{ containerPort = 80, hostPort = 80 },
34-
{ containerPort = 8000, hostPort = 8000 },
35-
{ containerPort = 8443, hostPort = 8443 },
36-
{ containerPort = 8001, hostPort = 8001 },
37-
{ containerPort = 8002, hostPort = 8002 }
38-
]
39-
admin_port = 8001
40-
proxy_port = 8000
49+
4150
public_target_group = "kong_public"
4251
internal_target_group = "kong_internal"
4352
public_domains = [for subdomain in var.kong_public_sub_domain_names : "${subdomain}.${var.base_domain}"]
4453
admin_domains = [for subdomain in var.kong_admin_sub_domain_names : "${subdomain}.${var.base_domain}"]
45-
}
4654

47-
rds_parameters = [
48-
"POSTGRES_USERNAME",
49-
"POSTGRES_PASSWORD",
50-
"POSTGRES_DB_NAME"
51-
]
5255

53-
github_parameters = [
54-
"GITHUB_ORG",
55-
"GITHUB_REPO",
56-
"GITHUB_TOKEN"
57-
]
56+
admin_port = 8001
57+
proxy_port = 8000
58+
portMappings = [
59+
{ containerPort = 80, hostPort = 80 },
60+
{ containerPort = 8000, hostPort = 8000 },
61+
{ containerPort = 8001, hostPort = 8001 },
62+
]
5863

59-
postgres_username = data.aws_ssm_parameter.rds["POSTGRES_USERNAME"].value
60-
postgres_password = data.aws_ssm_parameter.rds["POSTGRES_PASSWORD"].value
61-
postgres_db_name = data.aws_ssm_parameter.rds["POSTGRES_DB_NAME"].value
62-
github_org = data.aws_ssm_parameter.github["GITHUB_ORG"].value
63-
github_repo = data.aws_ssm_parameter.github["GITHUB_REPO"].value
64-
github_token = data.aws_ssm_parameter.github["GITHUB_TOKEN"].value
64+
environment = {
65+
"KONG_ADMIN_LISTEN" = "0.0.0.0:8001"
66+
"KONG_PROXY_LISTEN" = "0.0.0.0:8000"
67+
"KONG_DATABASE" = local.rds.engine
68+
"KONG_PG_HOST" = module.kong_rds.db_instance_address
69+
"KONG_PG_USER" = local.rds.postgres_username
70+
"KONG_PG_PASSWORD" = local.rds.postgres_password
71+
"KONG_PG_DATABASE" = local.rds.postgres_db_name
72+
"KONG_PROXY_ACCESS_LOG" = "/dev/stdout"
73+
"KONG_ADMIN_ACCESS_LOG" = "/dev/stdout"
74+
"KONG_PROXY_ERROR_LOG" = "/dev/stderr"
75+
"KONG_ADMIN_ERROR_LOG" = "/dev/stderr"
76+
"KONG_LOG_LEVEL" = "debug"
77+
"KONG_PG_SSL" = "on"
78+
}
79+
}
6580

66-
kong_parameters = {
67-
"KONG_ADMIN_LISTEN" = "0.0.0.0:8001"
68-
"KONG_PROXY_LISTEN" = "0.0.0.0:8000"
69-
"KONG_DATABASE" = local.rds_engine
70-
"KONG_PG_HOST" = module.kong_rds.db_instance_address
71-
"KONG_PG_USER" = local.postgres_username
72-
"KONG_PG_PASSWORD" = local.postgres_password
73-
"KONG_PG_DATABASE" = local.postgres_db_name
74-
"KONG_PROXY_ACCESS_LOG" = "/dev/stdout"
75-
"KONG_ADMIN_ACCESS_LOG" = "/dev/stdout"
76-
"KONG_PROXY_ERROR_LOG" = "/dev/stderr"
77-
"KONG_ADMIN_ERROR_LOG" = "/dev/stderr"
78-
"KONG_LOG_LEVEL" = "debug"
79-
"KONG_PG_SSL" = "on"
81+
github = {
82+
org = data.aws_ssm_parameter.github["GITHUB_ORG"].value
83+
repo = data.aws_ssm_parameter.github["GITHUB_REPO"].value
84+
token = data.aws_ssm_parameter.github["GITHUB_TOKEN"].value
8085
}
8186

8287
default_tags = {

main.tf

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module "postgres_security_group" {
66
source = "terraform-aws-modules/security-group/aws"
77
version = "~> 5.1.2"
88

9-
name = local.name
9+
name = local.rds.sg_name
1010
description = "Allow all traffic within vpc"
1111
vpc_id = var.vpc_id
1212

@@ -35,24 +35,24 @@ module "kong_rds" {
3535
source = "terraform-aws-modules/rds/aws"
3636
version = "~> 6.7.0"
3737

38-
identifier = local.db_identifier
39-
engine = local.rds_engine
40-
engine_version = local.postgres.engine_version
41-
family = local.postgres.engine_family
42-
major_engine_version = local.postgres.major_engine_version
38+
identifier = local.rds.db_identifier
39+
engine = local.rds.engine
40+
engine_version = local.rds.engine_version
41+
family = local.rds.engine_family
42+
major_engine_version = local.rds.major_engine_version
4343
instance_class = var.rds_instance_class
4444

45-
storage_encrypted = local.storage_encrypted
46-
storage_type = local.storage_type
45+
storage_encrypted = local.rds.storage_encrypted
46+
storage_type = local.rds.storage_type
4747
allocated_storage = var.db_allocated_storage
4848
max_allocated_storage = var.db_max_allocated_storage
4949
multi_az = var.multi_az
5050

5151
manage_master_user_password = var.manage_master_user_password
52-
db_name = local.postgres_db_name
53-
username = local.postgres_username
54-
port = local.postgres.port
55-
password = local.postgres_password
52+
db_name = local.rds.postgres_db_name
53+
username = local.rds.postgres_username
54+
port = local.rds.port
55+
password = local.rds.postgres_password
5656

5757
backup_retention_period = var.backup_retention_period
5858
backup_window = var.backup_window
@@ -213,7 +213,7 @@ module "ecs_kong" {
213213
portMappings = local.kong.portMappings
214214

215215
environment = [
216-
for key, value in local.kong_parameters : {
216+
for key, value in local.kong.environment : {
217217
name = key
218218
value = value
219219
}
@@ -382,7 +382,7 @@ module "github_runner" {
382382
source = "./modules/github-runner"
383383
vpc_id = var.vpc_id
384384
private_subnet_id = var.private_subnet_ids[0]
385-
github_org = local.github_org
386-
github_repo = local.github_repo
387-
github_token = local.github_token
385+
github_org = local.github.org
386+
github_repo = local.github.repo
387+
github_token = local.github.token
388388
}

modules/route-53-record/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ No requirements.
66

77
| Name | Version |
88
|------|---------|
9-
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.59.0 |
9+
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
1010

1111
## Modules
1212

0 commit comments

Comments
 (0)