Skip to content

Commit 0bb3731

Browse files
authored
chore: align to serverless for naming and tags (#121)
* chore: add prod , will be deleted later * chore: align with serverless for naming and tags * fix: vars
1 parent 7d56f1a commit 0bb3731

File tree

16 files changed

+81
-46
lines changed

16 files changed

+81
-46
lines changed

.github/workflows/.deployer.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ jobs:
112112
api_image: ${{ steps.image-tags.outputs.api-image }}
113113
app_env: ${{inputs.app_env}}
114114
stack_prefix: ${{ inputs.stack_prefix }}
115+
repo_name: ${{ github.event.repository.name }}
115116
run: |
116117
# Run terraform
117118
terragrunt run-all ${{inputs.command}} --terragrunt-non-interactive
@@ -127,6 +128,7 @@ jobs:
127128
api_image: ${{ steps.image-tags.outputs.api-image }}
128129
app_env: ${{inputs.app_env}}
129130
stack_prefix: ${{ inputs.stack_prefix }}
131+
repo_name: ${{ github.event.repository.name }}
130132
run: |
131133
terragrunt output -json > outputs.json
132134
#print the output
@@ -144,6 +146,7 @@ jobs:
144146
api_image: ${{ steps.image-tags.outputs.api-image }}
145147
app_env: ${{inputs.app_env}}
146148
stack_prefix: ${{ inputs.stack_prefix }}
149+
repo_name: ${{ github.event.repository.name }}
147150
run: |
148151
terragrunt output -json > outputs.json
149152
#print the output

.github/workflows/.stack-prefix.yml

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,32 @@ jobs:
2121
shell: bash
2222
run: |
2323
# Get repository name
24-
REPO_NAME="${{ github.event.repository.name }}"
24+
repo_name="${{ github.event.repository.name }}"
25+
# 1. Clean the repository name
26+
# Convert to lowercase, replace non-alphanumeric/hyphen with hyphen,
27+
# reduce multiple hyphens, remove leading/trailing hyphens.
28+
cleaned_name=$(echo "$repo_name" | tr '[:upper:]' '[:lower:]' | \
29+
sed 's/[^a-z0-9-]/-/g' | sed 's/-\{2,\}/-/g' | \
30+
sed 's/^-//;s/-$//')
2531
26-
# If repo name is less than 20 characters, use it directly
27-
if [[ ${#REPO_NAME} -lt 20 ]]; then
28-
STACK_PREFIX="${REPO_NAME}"
29-
else
30-
# Split by hyphen or underscore and get first letter of each word
31-
PREFIX=$(echo "$REPO_NAME" |
32-
awk -v RS='[-_]' '{printf "%s", tolower(substr($0,1,1))}' |
33-
tr -d '\n')
32+
# Define the target length for the human-readable prefix
33+
prefix_len=5
3434
35-
# Ensure at least 4 characters without repetition
36-
while [[ ${#PREFIX} -lt 4 ]]; do
37-
# Concatenate with the next letter in the sequence (avoiding randomness)
38-
SUFFIX="${PREFIX: -1}" # Get the last character of the current PREFIX
39-
INDEX=$(( $(echo "$PREFIX" | grep -o "$SUFFIX" | wc -l) + 1 )) # Get the index of the next character
40-
NEXT_CHAR=$(echo "$PREFIX" | cut -c $INDEX) # Get the next character
41-
PREFIX="${PREFIX}${NEXT_CHAR}"
42-
done
35+
# 2. Generate the 5-character human-readable prefix
36+
prefix_part=""
37+
# Remove all hyphens from the cleaned name to get a continuous string of letters/numbers
38+
continuous_name=$(echo "$cleaned_name" | sed 's/-//g')
4339
44-
# Truncate if prefix exceeds 10 characters
45-
if [[ ${#PREFIX} -gt 10 ]]; then
46-
PREFIX="${PREFIX:0:10}"
47-
fi
40+
# Take up to 'prefix_len' (5) characters, but allow shorter prefixes
41+
prefix_part="${continuous_name:0:$prefix_len}"
4842
49-
STACK_PREFIX="${PREFIX}"
50-
fi
43+
# 3. Generate the 4-character consistent hash suffix
44+
# The MD5 hash of the original repo name ensures consistency and uniqueness
45+
hash_suffix=$(echo -n "$repo_name" | md5sum | head -c 4)
46+
47+
# 4. Combine the prefix, hyphen, and hash suffix
48+
final_name="${prefix_part}-${hash_suffix}"
5149
5250
# Set output
53-
echo "STACK_PREFIX=$STACK_PREFIX" >> $GITHUB_OUTPUT
54-
echo "Generated prefix: $STACK_PREFIX"
51+
echo "STACK_PREFIX=$final_name" >> $GITHUB_OUTPUT
52+
echo "Generated prefix: $final_name"

.github/workflows/prune-env.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ on:
1010
options:
1111
- dev
1212
- test
13+
- prod
1314
app_env:
1415
required: true
1516
type: choice
1617
description: 'The APP env separates between AWS ENV and Actual APP, since AWS dev is where PR, and TEST is deployed'
1718
options:
1819
- dev
1920
- test
21+
- prod
2022
permissions:
2123
id-token: write # This is required for requesting the JWT
2224
contents: write # This is required for actions/checkout
@@ -35,6 +37,14 @@ jobs:
3537
if: ( github.event.inputs.app_env == 'test' )
3638
uses: ./.github/workflows/.destroy_stack.yml
3739
secrets: inherit
40+
with:
41+
environment_name: ${{ github.event.inputs.environment_name }}
42+
app_env: ${{ github.event.inputs.app_env }}
43+
destroy-prod:
44+
name: Destroy Stack Prod
45+
if: ( github.event.inputs.app_env == 'prod' )
46+
uses: ./.github/workflows/.destroy_stack.yml
47+
secrets: inherit
3848
with:
3949
environment_name: ${{ github.event.inputs.environment_name }}
4050
app_env: ${{ github.event.inputs.app_env }}

infrastructure/api/api-gateway.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ resource "aws_apigatewayv2_vpc_link" "app" {
22
name = var.app_name
33
subnet_ids = data.aws_subnets.web.ids
44
security_group_ids = [data.aws_security_group.web.id]
5+
tags = local.common_tags
56
}
67

78
resource "aws_apigatewayv2_api" "app" {
89
name = var.app_name
910
protocol_type = "HTTP"
11+
tags = local.common_tags
1012
}
1113

1214
resource "aws_apigatewayv2_integration" "app" {

infrastructure/api/autoscaling.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ resource "aws_appautoscaling_target" "api_target" {
44
scalable_dimension = "ecs:service:DesiredCount"
55
min_capacity = var.min_capacity
66
max_capacity = var.max_capacity
7+
tags = local.common_tags
78
}
89

910
# Automatically scale capacity up by one

infrastructure/api/ecs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ locals {
2121

2222
resource "aws_ecs_cluster" "ecs_cluster" {
2323
name = "ecs-cluster-${var.app_name}"
24+
tags = local.common_tags
2425
}
2526

2627
resource "aws_ecs_cluster_capacity_providers" "ecs_cluster_capacity_providers" {
@@ -160,6 +161,7 @@ resource "aws_ecs_task_definition" "flyway_task" {
160161
fi
161162
EOF
162163
}
164+
tags = local.common_tags
163165
}
164166

165167
resource "aws_ecs_task_definition" "node_api_task" {
@@ -232,6 +234,7 @@ resource "aws_ecs_task_definition" "node_api_task" {
232234
lifecycle {
233235
create_before_destroy = true
234236
}
237+
tags = local.common_tags
235238
}
236239

237240

infrastructure/api/vars.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,8 @@ variable "postgres_pool_size" {
173173
description = "The size of the connection pool for the API"
174174
type = string
175175
default = "1"
176+
}
177+
variable "repo_name" {
178+
description = "Name of the repository for resource descriptions and tags"
179+
type = string
176180
}

infrastructure/api/waf.tf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ resource "aws_wafv2_web_acl" "cloudfront_acl" {
4040
metric_name = "AppWebACL"
4141
sampled_requests_enabled = true
4242
}
43+
tags = local.common_tags
4344
}
4445

4546
resource "aws_cloudfront_distribution" "api" {
4647
count = var.is_public_api ? 1 : 0
4748
provider = aws.cloudfront_waf
4849
web_acl_id = aws_wafv2_web_acl.cloudfront_acl[0].arn
49-
comment = "Distribution for ${var.app_name} api."
50+
comment = "Distribution for ${var.app_name} api, for github repository :: ${var.repo_name}"
5051

5152
origin {
5253
domain_name = "${aws_apigatewayv2_api.app.id}.execute-api.${var.aws_region}.amazonaws.com"
@@ -97,12 +98,14 @@ resource "aws_cloudfront_distribution" "api" {
9798
}
9899

99100
depends_on = [aws_s3_bucket_policy.cloudfront_log_policy]
101+
tags = local.common_tags
100102
}
101103

102104
resource "aws_s3_bucket" "cloudfront_api_logs" {
103105
count = var.is_public_api ? 1 : 0
104106
bucket = "cloudfront-api-logs-${var.app_name}"
105107
force_destroy = true
108+
tags = local.common_tags
106109
}
107110

108111
resource "aws_s3_bucket_public_access_block" "cloudfront_api_logs_block" {

infrastructure/database/aurora-v2.tf

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ resource "aws_db_subnet_group" "db_subnet_group" {
1313
description = "For Aurora cluster ${var.db_cluster_name}"
1414
name = "${var.db_cluster_name}-subnet-group"
1515
subnet_ids = [ for s in data.aws_subnet.data : s.id ]
16-
17-
tags = {
18-
managed-by = "terraform"
19-
}
16+
tags = var.common_tags
2017

2118
tags_all = {
2219
managed-by = "terraform"
@@ -32,9 +29,7 @@ data "aws_rds_engine_version" "postgresql" {
3229
resource "aws_secretsmanager_secret" "db_mastercreds_secret" {
3330
name = "${var.db_cluster_name}"
3431

35-
tags = {
36-
managed-by = "terraform"
37-
}
32+
tags = var.common_tags
3833
}
3934

4035
resource "aws_secretsmanager_secret_version" "db_mastercreds_secret_version" {
@@ -86,9 +81,7 @@ module "aurora_postgresql_v2" {
8681
two = {}
8782
}: {one = {}}
8883

89-
tags = {
90-
managed-by = "terraform"
91-
}
84+
tags = var.common_tags
9285

9386
enabled_cloudwatch_logs_exports = ["postgresql"]
9487
backup_retention_period = "${var.backup_retention_period}"

infrastructure/database/vars.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,13 @@ variable "max_capacity" {
4545
description = "Maximum capacity for Aurora Serverless v2"
4646
type = number
4747
default = 1.0
48+
}
49+
variable "repo_name" {
50+
description = "Name of the repository for resource descriptions and tags"
51+
type = string
52+
}
53+
variable "common_tags" {
54+
description = "Common tags to be applied to resources"
55+
type = map(string)
56+
default = {}
4857
}

0 commit comments

Comments
 (0)