Skip to content

Commit

Permalink
feat(terraform): add CDN_URL to environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaLicense committed May 7, 2024
1 parent bc2400e commit b1b3ab1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
16 changes: 12 additions & 4 deletions infra/terraform/environments/dev/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ module "service" {
cpu = 1024
memory = 4096

image = "${data.aws_ecr_repository.this["api"].repository_url}:${var.api_image_tag}"
version = var.api_image_tag
repository = "${data.aws_ecr_repository.this["api"].repository_url}"

task_iam_role_statements = [
{
Expand Down Expand Up @@ -147,7 +148,8 @@ module "service" {
data.aws_security_group.this["API"].id
]

lb_listener_arn = data.aws_lb_listener.this["API"].arn
lb_listener_arn = data.aws_lb_listener.this["API"].arn
listener_rule_host_header = "api.*"

vpc_id = data.aws_vpc.this.id
}
Expand All @@ -156,7 +158,10 @@ module "service" {
cpu = 1024
memory = 4096

image = "${data.aws_ecr_repository.this["internal"].repository_url}:${var.internal_image_tag}"
version = var.internal_image_tag
repository = "${data.aws_ecr_repository.this["internal"].repository_url}"

add_cdn_url_to_env = true

task_iam_role_statements = [
{
Expand Down Expand Up @@ -194,7 +199,10 @@ module "service" {
cpu = 1024
memory = 4096

image = "${data.aws_ecr_repository.this["selfserve"].repository_url}:${var.selfserve_image_tag}"
version = var.selfserve_image_tag
repository = "${data.aws_ecr_repository.this["selfserve"].repository_url}"

add_cdn_url_to_env = true

task_iam_role_statements = [
{
Expand Down
2 changes: 1 addition & 1 deletion infra/terraform/modules/service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
| <a name="input_assets_version"></a> [assets\_version](#input\_assets\_version) | The version of the assets | `string` | n/a | yes |
| <a name="input_domain_name"></a> [domain\_name](#input\_domain\_name) | The domain name for the environment | `string` | n/a | yes |
| <a name="input_environment"></a> [environment](#input\_environment) | The environment to deploy to | `string` | n/a | yes |
| <a name="input_services"></a> [services](#input\_services) | The services to deploy | <pre>map(object({<br> image = string<br> cpu = number<br> memory = number<br> lb_listener_arn = string<br> security_group_ids = list(string)<br> subnet_ids = list(string)<br> vpc_id = string<br> task_iam_role_statements = list(object({<br> effect = string<br> actions = list(string)<br> resources = list(string)<br> }))<br> }))</pre> | `{}` | no |
| <a name="input_services"></a> [services](#input\_services) | The services to deploy | <pre>map(object({<br> version = string<br> repository = string<br> cpu = number<br> memory = number<br> task_iam_role_statements = list(object({<br> effect = string<br> actions = list(string)<br> resources = list(string)<br> }))<br> add_cdn_url_to_env = optional(bool, false)<br> lb_listener_arn = string<br> listener_rule_priority = optional(number, 10)<br> listener_rule_host_header = optional(string, "*")<br> security_group_ids = list(string)<br> subnet_ids = list(string)<br> vpc_id = string<br> }))</pre> | `{}` | no |

## Outputs

Expand Down
41 changes: 22 additions & 19 deletions infra/terraform/modules/service/ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ resource "aws_lb_listener_rule" "this" {
for_each = var.services

listener_arn = each.value.lb_listener_arn
priority = 10
priority = each.value.listener_rule_priority

action {
type = "forward"
target_group_arn = aws_lb_target_group.this[each.key].arn
}

condition {
query_string {
key = "infra"
value = "ecs"
host_header {
values = [each.value.listener_rule_host_header]
}
}
}
Expand Down Expand Up @@ -84,7 +83,7 @@ module "ecs_service" {
cpu = try(var.services[each.key].task_cpu_limit, var.services[each.key].cpu / 2)
memory = try(var.services[each.key].task_memory_limit, var.services[each.key].memory / 4)
essential = true
image = var.services[each.key].image
image = "${var.services[each.key].repository}:${var.services[each.key].version}"
port_mappings = [
{
name = "http"
Expand All @@ -97,20 +96,24 @@ module "ecs_service" {
# Have to explicitly set the user to null to avoid the default user being set to root.
user = null

environment = [
{
name = "ENVIRONMENT_NAME"
value = var.environment
},
{
name = "APP_VERSION"
value = var.services[each.key].image
},
{
name = "CDN_URL"
value = module.cloudfront.cloudfront_distribution_domain_name
}
]
environment = concat(
[
{
name = "ENVIRONMENT_NAME"
value = var.environment
},
{
name = "APP_VERSION"
value = var.services[each.key].version
},
],
each.value.add_cdn_url_to_env ? [
{
name = "CDN_URL"
value = module.cloudfront.cloudfront_distribution_domain_name
}
] : []
)

readonly_root_filesystem = false

Expand Down
18 changes: 11 additions & 7 deletions infra/terraform/modules/service/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ variable "assets_version" {

variable "services" {
type = map(object({
image = string
cpu = number
memory = number
lb_listener_arn = string
security_group_ids = list(string)
subnet_ids = list(string)
vpc_id = string
version = string
repository = string
cpu = number
memory = number
task_iam_role_statements = list(object({
effect = string
actions = list(string)
resources = list(string)
}))
add_cdn_url_to_env = optional(bool, false)
lb_listener_arn = string
listener_rule_priority = optional(number, 10)
listener_rule_host_header = optional(string, "*")
security_group_ids = list(string)
subnet_ids = list(string)
vpc_id = string
}))
description = "The services to deploy"
default = {}
Expand Down

0 comments on commit b1b3ab1

Please sign in to comment.