Skip to content

Commit bcfdd13

Browse files
authored
feat: tf modules (#54)
1 parent 358c42f commit bcfdd13

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+981
-2788
lines changed

.github/workflows/.deploy_stack.yml

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ on:
2525
description: 'The APP env separates between AWS ENV and Actual APP, since AWS dev is where PR, and TEST is deployed'
2626
outputs:
2727
API_GW_URL:
28-
value: ${{ jobs.deploy-api.outputs.API_GW_URL }}
28+
value: ${{ jobs.deploy.outputs.API_GW_URL }}
2929
S3_BUCKET_ARN:
30-
value: ${{ jobs.deploy-cloudfront.outputs.S3_BUCKET_ARN }}
30+
value: ${{ jobs.deploy.outputs.S3_BUCKET_ARN }}
31+
S3_BUCKET_NAME:
32+
value: ${{ jobs.deploy.outputs.S3_BUCKET_NAME }}
3133
CF_DOMAIN:
32-
value: ${{ jobs.deploy-cloudfront.outputs.CF_DOMAIN }}
34+
value: ${{ jobs.deploy.outputs.CF_DOMAIN }}
3335
CF_DISTRIBUTION_ID:
34-
value: ${{ jobs.deploy-cloudfront.outputs.CF_DISTRIBUTION_ID }}
36+
value: ${{ jobs.deploy.outputs.CF_DISTRIBUTION_ID }}
3537
env:
3638
AWS_REGION: ca-central-1
3739
permissions:
@@ -90,46 +92,23 @@ jobs:
9092
name: Stack Prefix
9193
needs: ecr
9294
uses: ./.github/workflows/.stack-prefix.yml
93-
deploy-db:
94-
name: Deploys Database
95+
deploy:
96+
name: Deploys
9597
needs: [stack-prefix]
9698
uses: ./.github/workflows/.deployer.yml
9799
with:
98100
environment_name: ${{ inputs.environment_name }}
99101
command: ${{ inputs.command }}
100-
working_directory: database
101102
app_env: ${{ inputs.app_env }}
102103
stack_prefix: ${{ needs.stack-prefix.outputs.stack_prefix }}
103-
secrets: inherit
104-
deploy-api:
105-
name: Deploys API
106-
needs: [deploy-db, stack-prefix]
107-
uses: ./.github/workflows/.deployer.yml
108-
with:
109-
environment_name: ${{ inputs.environment_name }}
110-
command: ${{ inputs.command }}
111104
tag: ${{ inputs.tag }}
112-
app_env: ${{ inputs.app_env }}
113-
working_directory: api
114-
stack_prefix: ${{ needs.stack-prefix.outputs.stack_prefix }}
115-
secrets: inherit
116-
deploy-cloudfront:
117-
name: Deploys Cloudfront
118-
needs: [stack-prefix]
119-
uses: ./.github/workflows/.deployer.yml
120-
with:
121-
environment_name: ${{ inputs.environment_name }}
122-
command: ${{ inputs.command }}
123-
tag: ${{ inputs.tag }}
124-
app_env: ${{ inputs.app_env }}
125-
working_directory: frontend
126-
stack_prefix: ${{ needs.stack-prefix.outputs.stack_prefix }}
127105
secrets: inherit
106+
128107
build-ui:
129108
name: Build And upload UI to s3 ${{ inputs.environment_name }}
130109
environment: ${{ inputs.environment_name }}
131110
if: (inputs.command == 'apply')
132-
needs: [deploy-api, deploy-cloudfront]
111+
needs: [deploy]
133112
runs-on: ubuntu-24.04
134113
steps:
135114
- name: Checkout
@@ -149,10 +128,17 @@ jobs:
149128
- name: Build And Update UI (CF)
150129
working-directory: frontend
151130
env:
152-
VITE_API_BASE_URL: ${{ needs.deploy-api.outputs.API_GW_URL }}/api
153-
S3_BUCKET_ARN: ${{ needs.deploy-cloudfront.outputs.S3_BUCKET_ARN }}
154-
CF_DISTRIBUTION_ID: ${{ needs.deploy-cloudfront.outputs.CF_DISTRIBUTION_ID }}
131+
VITE_API_BASE_URL: ${{ needs.deploy.outputs.API_GW_URL }}/api
132+
S3_BUCKET_ARN: ${{ needs.deploy.outputs.S3_BUCKET_ARN }}
133+
CF_DISTRIBUTION_ID: ${{ needs.deploy.outputs.CF_DISTRIBUTION_ID }}
155134
run: |
135+
BUCKET=$(echo "$S3_BUCKET_ARN" | cut -d: -f6)
156136
npm run deploy
157-
aws s3 sync --delete ./dist s3://$(echo "$S3_BUCKET_ARN" | cut -d: -f6)
137+
aws s3 cp ./dist/index.html s3://$BUCKET/index.html \
138+
--cache-control "no-cache, must-revalidate" \
139+
--content-type "text/html"
140+
aws s3 sync ./dist s3://$BUCKET \
141+
--exclude "index.html" \
142+
--cache-control "public, max-age=31536000, immutable" \
143+
--delete
158144
aws cloudfront create-invalidation --distribution-id $CF_DISTRIBUTION_ID --paths "/*"

.github/workflows/.deployer.yml

Lines changed: 39 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ on:
1414
required: true
1515
default: 'apply'
1616
type: string
17-
working_directory:
18-
description: 'The working directory to run the command in'
19-
required: true
20-
default: 'database'
21-
type: string
2217
tag:
2318
description: 'The tag of the containers to deploy'
2419
default: 'latest'
@@ -37,38 +32,41 @@ on:
3732
value: ${{ jobs.infra.outputs.API_GW_URL }}
3833
S3_BUCKET_ARN:
3934
value: ${{ jobs.infra.outputs.S3_BUCKET_ARN }}
35+
S3_BUCKET_NAME:
36+
value: ${{ jobs.infra.outputs.S3_BUCKET_NAME }}
4037
CF_DOMAIN:
4138
value: ${{ jobs.infra.outputs.CF_DOMAIN }}
4239
CF_DISTRIBUTION_ID:
4340
value: ${{ jobs.infra.outputs.CF_DISTRIBUTION_ID }}
4441

4542
env:
46-
TG_VERSION: 0.63.6
47-
TF_VERSION: 1.12.2
48-
TG_SRC_PATH: terraform/${{ inputs.working_directory }}
49-
AWS_REGION: ca-central-1
43+
TG_VERSION: 0.63.6
44+
TF_VERSION: 1.12.2
45+
TG_SRC_PATH: terragrunt
46+
AWS_REGION: ca-central-1
5047
permissions:
5148
id-token: write # This is required for requesting the JWT
5249
contents: write # This is required for actions/checkout
5350
jobs:
5451
infra:
5552
environment: ${{ inputs.environment_name }}
56-
name: Terraform ${{inputs.command}} ${{inputs.working_directory}} ${{inputs.app_env}}
53+
name: Terragrunt ${{inputs.command}} ${{inputs.app_env}}
5754
runs-on: ubuntu-24.04
5855
outputs:
59-
API_GW_URL: ${{ steps.tg-outputs.outputs.API_GW_URL }}
60-
S3_BUCKET_ARN: ${{ steps.tg-outputs-frontend.outputs.S3_BUCKET_ARN }}
61-
CF_DOMAIN: ${{ steps.tg-outputs-frontend.outputs.CF_DOMAIN }}
62-
CF_DISTRIBUTION_ID: ${{ steps.tg-outputs-frontend.outputs.CF_DISTRIBUTION_ID }}
56+
API_GW_URL: ${{ steps.tg.outputs.API_GW_URL }}
57+
S3_BUCKET_ARN: ${{ steps.tg.outputs.S3_BUCKET_ARN }}
58+
S3_BUCKET_NAME: ${{ steps.tg.outputs.S3_BUCKET_NAME }}
59+
CF_DOMAIN: ${{ steps.tg.outputs.CF_DOMAIN }}
60+
CF_DISTRIBUTION_ID: ${{ steps.tg.outputs.CF_DISTRIBUTION_ID }}
6361
steps:
6462
- name: Checkout
6563
uses: actions/checkout@v4
6664
- name: Configure AWS Credentials
6765
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4
6866
with:
69-
role-to-assume: ${{ secrets.AWS_DEPLOY_ROLE_ARN }}
70-
role-session-name: ${{ inputs.environment_name }}-deployment
71-
aws-region: ${{ env.AWS_REGION }}
67+
role-to-assume: ${{ secrets.AWS_DEPLOY_ROLE_ARN }}
68+
role-session-name: ${{ inputs.environment_name }}-deployment
69+
aws-region: ${{ env.AWS_REGION }}
7270
- uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3
7371
with:
7472
terraform_version: ${{ env.TF_VERSION }}
@@ -82,57 +80,35 @@ jobs:
8280
shell: bash
8381
run: |
8482
API_IMAGE="${{ steps.ecr-check.outputs.ecr-registry }}/${{ github.repository }}:backend-${{ inputs.tag }}"
83+
FLYWAY_IMAGE="${{ steps.ecr-check.outputs.ecr-registry }}/${{ github.repository }}:migrations-${{ inputs.tag }}"
8584
echo "api-image=$API_IMAGE" >> $GITHUB_OUTPUT
85+
echo "flyway-image=$FLYWAY_IMAGE" >> $GITHUB_OUTPUT
8686
8787
- name: Setup Terragrunt
8888
uses: autero1/action-terragrunt@aefb0a43c4f5503a91fefb307745c4d51c26ed0e # v3
8989
with:
90-
terragrunt-version: ${{ env.TG_VERSION }}
90+
terragrunt-version: ${{ env.TG_VERSION }}
9191
- name: Terragrunt ${{inputs.command}}
92-
working-directory: terraform/${{ inputs.working_directory }}/${{ inputs.environment_name }}
93-
env:
94-
target_env: ${{ inputs.environment_name }}
95-
aws_license_plate: ${{ secrets.AWS_LICENSE_PLATE }}
96-
api_image: ${{ steps.image-tags.outputs.api-image }}
97-
app_env: ${{inputs.app_env}}
98-
stack_prefix: ${{ inputs.stack_prefix }}
99-
repo_name: ${{ github.event.repository.name }}
100-
run: |
101-
# Run terraform
102-
terragrunt run-all ${{inputs.command}} --terragrunt-non-interactive
103-
- name: Terragrunt API Outputs
104-
if: (inputs.working_directory == 'api' && inputs.command == 'apply')
105-
working-directory: terraform/${{ inputs.working_directory }}/${{ inputs.environment_name }}
106-
id: tg-outputs
92+
id: tg
93+
working-directory: terragrunt/${{ inputs.environment_name }}
10794
env:
108-
target_env: ${{ inputs.environment_name }}
109-
aws_license_plate: ${{ secrets.AWS_LICENSE_PLATE }}
110-
api_image: ${{ steps.image-tags.outputs.api-image }}
111-
app_env: ${{inputs.app_env}}
112-
stack_prefix: ${{ inputs.stack_prefix }}
113-
repo_name: ${{ github.event.repository.name }}
95+
target_env: ${{ inputs.environment_name }}
96+
aws_license_plate: ${{ secrets.AWS_LICENSE_PLATE }}
97+
api_image: ${{ steps.image-tags.outputs.api-image }}
98+
app_env: ${{inputs.app_env}}
99+
stack_prefix: ${{ inputs.stack_prefix }}
100+
repo_name: ${{ github.event.repository.name }}
101+
terragrunt_command: ${{ inputs.command }}
114102
run: |
115-
terragrunt output -json > outputs.json
116-
#print the output
117-
cat outputs.json
118-
119-
echo "API_GW_URL=$(jq -r .apigw_url.value outputs.json)" >> $GITHUB_OUTPUT
120-
- name: Terragrunt Frontend Outputs
121-
if: (inputs.working_directory == 'frontend' && inputs.command == 'apply')
122-
working-directory: terraform/${{ inputs.working_directory }}/${{ inputs.environment_name }}
123-
id: tg-outputs-frontend
124-
env:
125-
target_env: ${{ inputs.environment_name }}
126-
aws_license_plate: ${{ secrets.AWS_LICENSE_PLATE }}
127-
api_image: ${{ steps.image-tags.outputs.api-image }}
128-
app_env: ${{inputs.app_env}}
129-
stack_prefix: ${{ inputs.stack_prefix }}
130-
repo_name: ${{ github.event.repository.name }}
131-
run: |
132-
terragrunt output -json > outputs.json
133-
#print the output
134-
cat outputs.json
135-
136-
echo "S3_BUCKET_ARN=$(jq -r .s3_bucket_arn.value outputs.json)" >> $GITHUB_OUTPUT
137-
echo "CF_DOMAIN=$(jq -r .cloudfront.value.domain_name outputs.json)" >> $GITHUB_OUTPUT
138-
echo "CF_DISTRIBUTION_ID=$(jq -r .cloudfront.value.distribution_id outputs.json)" >> $GITHUB_OUTPUT
103+
# Run terraform
104+
terragrunt run-all ${{inputs.command}} --terragrunt-non-interactive
105+
terragrunt output -json > outputs.json
106+
#print the output
107+
cat outputs.json
108+
109+
echo "S3_BUCKET_ARN=$(jq -r .frontend_bucket.value.arn outputs.json)" >> $GITHUB_OUTPUT
110+
echo "S3_BUCKET_NAME=$(jq -r .frontend_bucket.value.name outputs.json)" >> $GITHUB_OUTPUT
111+
echo "CF_DOMAIN=$(jq -r .cloudfront.value.domain_name outputs.json)" >> $GITHUB_OUTPUT
112+
echo "CF_DISTRIBUTION_ID=$(jq -r .cloudfront.value.distribution_id outputs.json)" >> $GITHUB_OUTPUT
113+
echo "API_GW_URL=$(jq -r .apigw_url.value outputs.json)" >> $GITHUB_OUTPUT
114+

.github/workflows/.destroy_stack.yml

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,40 +32,13 @@ jobs:
3232
stack-prefix:
3333
name: Stack Prefix
3434
uses: ./.github/workflows/.stack-prefix.yml
35-
api:
36-
name: Destroy API
35+
destroy:
36+
name: Destroy
3737
needs: [stack-prefix]
3838
uses: ./.github/workflows/.deployer.yml
3939
with:
4040
environment_name: ${{ inputs.environment_name }}
4141
command: ${{ inputs.command }}
42-
tag: ${{ inputs.tag }}
4342
app_env: ${{ inputs.app_env }}
44-
working_directory: api
4543
stack_prefix: ${{ needs.stack-prefix.outputs.stack_prefix }}
46-
secrets: inherit
47-
db:
48-
name: Destroy Database
49-
needs: [stack-prefix, api]
50-
uses: ./.github/workflows/.deployer.yml
51-
with:
52-
environment_name: ${{ inputs.environment_name }}
53-
command: ${{ inputs.command }}
54-
working_directory: database
55-
app_env: ${{ inputs.app_env }}
56-
stack_prefix: ${{ needs.stack-prefix.outputs.stack_prefix }}
57-
secrets: inherit
58-
59-
cloudfront:
60-
name: Destroy Cloudfront
61-
needs: [stack-prefix]
62-
uses: ./.github/workflows/.deployer.yml
63-
with:
64-
environment_name: ${{ inputs.environment_name }}
65-
command: ${{ inputs.command }}
66-
tag: ${{ inputs.tag }}
67-
app_env: ${{ inputs.app_env }}
68-
working_directory: frontend
69-
stack_prefix: ${{ needs.stack-prefix.outputs.stack_prefix }}
70-
secrets: inherit
71-
44+
secrets: inherit

infra/main.tf

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# -------------------------------------------------------------------
2+
# Database Module (First)
3+
# -------------------------------------------------------------------
4+
module "database" {
5+
source = "./modules/database"
6+
7+
app_env = var.app_env
8+
app_name = var.app_name
9+
common_tags = var.common_tags
10+
dynamodb_table_name = var.dynamodb_table_name
11+
repo_name = var.repo_name
12+
target_env = var.target_env
13+
}
14+
15+
# -------------------------------------------------------------------
16+
# API Module (Second)
17+
# -------------------------------------------------------------------
18+
module "api" {
19+
source = "./modules/api"
20+
21+
api_cpu = var.api_cpu
22+
api_image = var.api_image
23+
api_memory = var.api_memory
24+
app_env = var.app_env
25+
app_name = var.app_name
26+
app_port = var.app_port
27+
aws_region = var.aws_region
28+
common_tags = var.common_tags
29+
dynamodb_table_name = var.dynamodb_table_name
30+
health_check_path = var.health_check_path
31+
is_public_api = var.is_public_api
32+
max_capacity = var.api_max_capacity
33+
min_capacity = var.api_min_capacity
34+
repo_name = var.repo_name
35+
target_env = var.target_env
36+
37+
providers = {
38+
aws.us-east-1 = aws.us-east-1
39+
}
40+
41+
depends_on = [module.database]
42+
}
43+
44+
# -------------------------------------------------------------------
45+
# Frontend Module (Third)
46+
# -------------------------------------------------------------------
47+
module "frontend" {
48+
source = "./modules/frontend"
49+
50+
app_env = var.app_env
51+
app_name = var.app_name
52+
common_tags = var.common_tags
53+
repo_name = var.repo_name
54+
target_env = var.target_env
55+
56+
providers = {
57+
aws.us-east-1 = aws.us-east-1
58+
}
59+
60+
depends_on = [module.api]
61+
}

0 commit comments

Comments
 (0)