1+ locals {
2+
3+ # Concatenate the load balancer domain to spring boot args
4+ spring_boot_args_with_host = " ${ var . spring_boot_args } --server.authorizedForwardedHost=${ aws_lb . registry-api-lb . dns_name } ,${ var . cloudfront_dns } "
5+ }
6+
17resource "aws_lb" "registry-api-lb" {
2- name = " registry-api-lb-new "
8+ name = " registry-api-lb"
39 internal = false
410 load_balancer_type = " application"
511 security_groups = var. aws_fg_security_groups
@@ -9,26 +15,17 @@ resource "aws_lb" "registry-api-lb" {
915
1016 access_logs {
1117 bucket = var. aws_s3_bucket_logs_id
12- prefix = " registry-api-lb"
18+ prefix = " registry/registry -api-lb"
1319 enabled = true
1420 }
1521
16- tags = {
17- Alfa = var.node_name_abbr
18- Bravo = var.venue
19- Charlie = " registry"
20- }
22+ tags = var. common_tags
2123}
2224
23- resource "aws_ssm_parameter" "load_balancer_domain" {
24- name = " /pds/registry/load-balancer-domain"
25- type = " String"
26- overwrite = true
27- value = aws_lb. registry-api-lb . dns_name
28- }
25+
2926
3027resource "aws_lb_target_group" "pds-registry-api-target-group" {
31- name = " pds-${ var . venue } - registry-tgt "
28+ name = " pds-registry-tg "
3229 port = 80
3330 protocol = " HTTP"
3431 target_type = " ip"
@@ -44,6 +41,8 @@ resource "aws_lb_target_group" "pds-registry-api-target-group" {
4441 matcher = " 200"
4542 interval = 300
4643 }
44+
45+ tags = var. common_tags
4746}
4847
4948resource "aws_lb_listener" "registry-api-ld-listener" {
@@ -55,6 +54,7 @@ resource "aws_lb_listener" "registry-api-ld-listener" {
5554 type = " forward"
5655 target_group_arn = aws_lb_target_group. pds-registry-api-target-group . arn
5756 }
57+ tags = var. common_tags
5858}
5959
6060resource "aws_lb_listener_rule" "pds-registry-forward-rule" {
@@ -75,44 +75,64 @@ resource "aws_lb_listener_rule" "pds-registry-forward-rule" {
7575 }
7676}
7777
78- # Define the cluster
79- resource "aws_ecs_cluster" "pds-registry-api-ecs" {
80- name = " pds-${ var . venue } -registry-api-ecs"
8178
82- tags = {
83- Alfa = var.node_name_abbr
84- Bravo = var.venue
85- Charlie = " registry"
86- }
79+ # Credentials for ECR pull through cache from GHCR
80+ resource "aws_secretsmanager_secret" "github_ecr_credentials" {
81+ count = var. create_github_secret_credentials
82+
83+ name = " ecr-pullthroughcache/github-credentials"
84+ tags = var. common_tags
8785}
8886
89- # Do we need individual dev/test/prod repositories?
90- # I don't think we do, but then we need to use prod account instead of the dev account, would that work ?
91- data "aws_ecr_repository" "pds-registry-api-service" {
92- name = " pds-registry-api-service"
87+ resource "aws_secretsmanager_secret_version" "github_ecr_credentials" {
88+ count = var. create_github_secret_credentials
89+
90+ secret_id = aws_secretsmanager_secret. github_ecr_credentials [count . index ]. id
91+ secret_string = jsonencode ({
92+ username = var.github_username
93+ accessToken = var.github_token
94+ })
95+ }
96+
97+ # Look up the secret when it is not created by this script
98+ data "aws_secretsmanager_secret" "github_ecr_credentials" {
99+ count = 1 - var. create_github_secret_credentials
100+ name = " ecr-pullthroughcache/github-credentials"
101+ }
102+
103+ locals {
104+ github_ecr_credentials_arn = var. create_github_secret_credentials == 1 ? aws_secretsmanager_secret. github_ecr_credentials [0 ]. arn : data. aws_secretsmanager_secret . github_ecr_credentials [0 ]. arn
105+ }
106+
107+ # Add a Pull Through Cache rule for GHCR
108+ resource "aws_ecr_pull_through_cache_rule" "ghcr" {
109+ ecr_repository_prefix = " ghcr"
110+ upstream_registry_url = " ghcr.io"
111+ credential_arn = local. github_ecr_credentials_arn
112+ }
113+
114+ resource "aws_ecr_repository" "ghcr_registry_api" {
115+ name = " ghcr/nasa-pds/registry-api"
116+ tags = var. common_tags
93117}
94118
95119# Log groups hold logs from our app.
96120resource "aws_cloudwatch_log_group" "pds-registry-log-group" {
97- name = " /ecs/pds-${ var . venue } - registry-api-svc -task"
121+ name = " /ecs/pds-registry-api-task"
98122
99- tags = {
100- Alfa = var.node_name_abbr
101- Bravo = var.venue
102- Charlie = " registry"
103- }
123+ tags = var. common_tags
104124}
105125
106126
107127# The task definition for app.
108128resource "aws_ecs_task_definition" "pds-registry-ecs-task" {
109- family = " pds-${ var . venue } - registry-api-svc -task"
129+ family = " pds-registry-api-task"
110130
111131 container_definitions = << EOF
112132 [
113133 {
114- "name": "pds- ${ var . venue } -reg -container",
115- "image": "${ var . aws_fg_image } ",
134+ "name": "registry-api -container",
135+ "image": "${ var . registry_api_docker_image } ",
116136 "portMappings": [
117137 {
118138 "containerPort": 80
@@ -138,7 +158,7 @@ resource "aws_ecs_task_definition" "pds-registry-ecs-task" {
138158 },
139159 "environment": [
140160 {"name": "SERVER_PORT", "value": "80"},
141- {"name": "SPRING_BOOT_APP_ARGS", "value": "${ var . spring_boot_args } "}
161+ {"name": "SPRING_BOOT_APP_ARGS", "value": "${ local . spring_boot_args_with_host } "}
142162 ]
143163 }
144164 ]
@@ -149,25 +169,27 @@ EOF
149169 task_role_arn = var. ecs_task_role
150170
151171 # These are the minimum values for Fargate containers.
152- cpu = 256
153- memory = 512
172+ cpu = var . aws_fg_cpu_units
173+ memory = var . aws_fg_ram_units
154174 requires_compatibilities = [" FARGATE" ]
155175
156176 # This is required for Fargate containers
157177 network_mode = " awsvpc"
158178
159- tags = {
160- Alfa = var.node_name_abbr
161- Bravo = var.venue
162- Charlie = " registry"
163- }
179+ tags = var. common_tags
164180}
165181
182+ # Define the cluster
183+ resource "aws_ecs_cluster" "pds-registry-api-ecs" {
184+ name = " pds-registry-api-cluster"
185+
186+ tags = var. common_tags
187+ }
166188
167189
168190# The main service.
169191resource "aws_ecs_service" "pds-registry-reg-service" {
170- name = " pds-${ var . venue } - registry-api-service"
192+ name = " pds-registry-api-service"
171193 task_definition = aws_ecs_task_definition. pds-registry-ecs-task . arn
172194 cluster = aws_ecs_cluster. pds-registry-api-ecs . id
173195 launch_type = " FARGATE"
@@ -176,7 +198,7 @@ resource "aws_ecs_service" "pds-registry-reg-service" {
176198
177199 load_balancer {
178200 target_group_arn = aws_lb_target_group. pds-registry-api-target-group . arn
179- container_name = " pds- ${ var . venue } -reg -container"
201+ container_name = " registry-api -container"
180202 container_port = " 80"
181203 }
182204
@@ -186,9 +208,8 @@ resource "aws_ecs_service" "pds-registry-reg-service" {
186208 subnets = var. aws_fg_subnets
187209 }
188210
189- tags = {
190- Alfa = var.node_name_abbr
191- Bravo = var.venue
192- Charlie = " registry"
193- }
211+ tags = var. common_tags
212+
213+ depends_on = [aws_ecr_repository . ghcr_registry_api , aws_ecr_pull_through_cache_rule . ghcr ]
194214}
215+
0 commit comments