Skip to content

Commit

Permalink
Move prefix from env-config to service root (#832)
Browse files Browse the repository at this point in the history
- Move prefix local var from env-config module to service layer's root
module
- Move storage resources to separate storage.tf file
  • Loading branch information
lorenyu authored Jan 14, 2025
1 parent 5c11598 commit ab1fda4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
3 changes: 2 additions & 1 deletion bin/update-pr-environment
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pr_number="$3"
image_tag="$4"

workspace="p-${pr_number}"
prefix="${workspace}-"

echo "::group::Initialize Terraform with backend for environment: ${environment}"
terraform -chdir="infra/${app_name}/service" init -backend-config="${environment}.s3.tfbackend"
Expand All @@ -31,7 +32,7 @@ terraform -chdir="infra/${app_name}/service" apply -input=false -auto-approve -v
echo "::endgroup::"

cluster_name="$(terraform -chdir="infra/$app_name/service" output -raw service_cluster_name)"
service_name="$(terraform -chdir="infra/$app_name/service" output -raw service_name)"
service_name="${prefix}$(terraform -chdir="infra/$app_name/service" output -raw service_name)"
echo "Wait for service ${service_name} to become stable"
aws ecs wait services-stable --cluster "${cluster_name}" --services "${service_name}"

Expand Down
9 changes: 1 addition & 8 deletions infra/{{app_name}}/app-config/env-config/main.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
locals {
# The prefix is used to create uniquely named resources per terraform workspace, which
# are needed in CI/CD for preview environments and tests.
#
# To isolate changes during infrastructure development by using manually created
# terraform workspaces, see: /docs/infra/develop-and-test-infrastructure-in-isolation-using-workspaces.md
prefix = terraform.workspace == "default" ? "" : "${terraform.workspace}-"

bucket_name = "${local.prefix}${var.project_name}-${var.app_name}-${var.environment}"
bucket_name = "${var.project_name}-${var.app_name}-${var.environment}"

network_config = module.project_config.network_configs[var.network_name]
}
Expand Down
2 changes: 1 addition & 1 deletion infra/{{app_name}}/app-config/env-config/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ output "network_name" {

output "service_config" {
value = {
service_name = "${local.prefix}${var.app_name}-${var.environment}"
service_name = "${var.app_name}-${var.environment}"
domain_name = var.domain_name
enable_https = var.enable_https
region = var.default_region
Expand Down
15 changes: 5 additions & 10 deletions infra/{{app_name}}/service/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ locals {
environment_config = module.app_config.environment_configs[var.environment_name]
service_config = local.environment_config.service_config
database_config = local.environment_config.database_config
storage_config = local.environment_config.storage_config
incident_management_service_integration_config = local.environment_config.incident_management_service_integration
identity_provider_config = local.environment_config.identity_provider_config
notifications_config = local.environment_config.notifications_config

network_config = module.project_config.network_configs[local.environment_config.network_name]

service_name = "${local.prefix}${local.service_config.service_name}"
}

terraform {
Expand Down Expand Up @@ -128,7 +129,7 @@ data "aws_route53_zone" "zone" {

module "service" {
source = "../../modules/service"
service_name = local.service_config.service_name
service_name = local.service_name

image_repository_arn = local.build_repository_config.repository_arn
image_repository_url = local.build_repository_config.repository_url
Expand Down Expand Up @@ -168,7 +169,7 @@ module "service" {

extra_environment_variables = merge(
{
BUCKET_NAME = local.storage_config.bucket_name
BUCKET_NAME = local.bucket_name
},
local.identity_provider_environment_variables,
local.notifications_environment_variables,
Expand Down Expand Up @@ -204,13 +205,7 @@ module "monitoring" {
#email_alerts_subscription_list = ["[email protected]", "[email protected]"]

# Module takes service and ALB names to link all alerts with corresponding targets
service_name = local.service_config.service_name
service_name = local.service_name
load_balancer_arn_suffix = module.service.load_balancer_arn_suffix
incident_management_service_integration_url = module.app_config.has_incident_management_service && !local.is_temporary ? data.aws_ssm_parameter.incident_management_service_integration_url[0].value : null
}

module "storage" {
source = "../../modules/storage"
name = local.storage_config.bucket_name
is_temporary = local.is_temporary
}
10 changes: 10 additions & 0 deletions infra/{{app_name}}/service/storage.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
locals {
storage_config = local.environment_config.storage_config
bucket_name = "${local.prefix}${local.storage_config.bucket_name}"
}

module "storage" {
source = "../../modules/storage"
name = local.bucket_name
is_temporary = local.is_temporary
}

0 comments on commit ab1fda4

Please sign in to comment.