Skip to content

Commit c427712

Browse files
authored
Merge pull request #1 from bcgov/feat/serverless
feat: serverless
2 parents 8d42d93 + 5ea9f5d commit c427712

Some content is hidden

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

46 files changed

+3014
-1941
lines changed

.github/scripts/pause.sh

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/bash
2-
# This script pauses AWS resources (ECS service and RDS Aurora cluster) in the current AWS account.
2+
# This script pauses AWS resources (ECS service) in the current AWS account.
3+
# Note: DynamoDB doesn't require pausing like RDS as it's pay-per-request
34

45
set -e # Exit on error
56

@@ -28,29 +29,7 @@ function validate_args() {
2829
fi
2930
}
3031

31-
# Check if Aurora DB cluster exists and get its status
32-
function check_aurora_cluster() {
33-
local cluster_id="${STACK_PREFIX}-aurora-${ENVIRONMENT}"
34-
local status=$(aws rds describe-db-clusters --db-cluster-identifier "$cluster_id" \
35-
--query 'DBClusters[0].Status' --output text 2>/dev/null || echo "false")
36-
echo "$status"
37-
}
3832

39-
# Pause Aurora DB cluster if available
40-
function pause_aurora_cluster() {
41-
local cluster_id="${STACK_PREFIX}-aurora-${ENVIRONMENT}"
42-
local status=$1
43-
44-
if [ "$status" = "false" ]; then
45-
echo "Skipping Aurora pause operation: DB cluster does not exist"
46-
return
47-
elif [ "$status" = "available" ]; then
48-
echo "Pausing Aurora cluster: $cluster_id"
49-
aws rds stop-db-cluster --db-cluster-identifier "$cluster_id" --no-cli-pager --output json
50-
else
51-
echo "DB cluster is not in an available state. Current state: $status"
52-
fi
53-
}
5433

5534
# Check if ECS cluster exists
5635
function check_ecs_cluster() {
@@ -92,16 +71,13 @@ function pause_ecs_service() {
9271
# Main execution
9372
validate_args
9473

95-
# Check and pause Aurora cluster
96-
aurora_status=$(check_aurora_cluster)
97-
[ "$aurora_status" = "false" ] || echo "Aurora cluster status: $aurora_status"
98-
9974
# Check and pause ECS service
10075
ecs_status=$(check_ecs_cluster)
10176
[ "$ecs_status" = "INACTIVE" ] || echo "ECS cluster status: $ecs_status"
10277

10378
# Perform pause operations
10479
pause_ecs_service "$ecs_status"
105-
pause_aurora_cluster "$aurora_status"
80+
81+
echo "Pause completed. Note: DynamoDB doesn't require pausing as it uses pay-per-request billing."
10682

10783
echo "Pause operations completed"

.github/scripts/resume.sh

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/bash
2-
# This script resumes AWS resources (ECS service and RDS Aurora cluster) in the specified AWS account.
2+
# This script resumes AWS resources (ECS service) in the specified AWS account.
3+
# Note: DynamoDB doesn't require resuming like RDS as it's always available
34

45
set -e # Exit on error
56

@@ -25,44 +26,28 @@ check_parameters() {
2526
exit 1
2627
fi
2728
}
28-
29-
# Function to check if DB cluster exists and get its status
30-
check_db_cluster() {
31-
local prefix=$1
32-
local env=$2
33-
local cluster_id="${prefix}-aurora-${env}"
34-
local status=$(aws rds describe-db-clusters --db-cluster-identifier ${cluster_id} --query 'DBClusters[0].Status' --output text 2>/dev/null || echo "not-found")
29+
# Check if ECS cluster exists
30+
function check_ecs_cluster() {
31+
local cluster_name="ecs-cluster-${STACK_PREFIX}-node-api-${ENVIRONMENT}"
32+
local status=$(aws ecs describe-clusters --clusters "$cluster_name" \
33+
--query 'clusters[0].status' --output text 2>/dev/null || echo "INACTIVE")
3534
echo "$status"
3635
}
3736

38-
# Function to start DB cluster
39-
start_db_cluster() {
40-
local prefix=$1
41-
local env=$2
42-
local cluster_id="${prefix}-aurora-${env}"
43-
44-
echo "Starting DB cluster ${cluster_id}..."
45-
aws rds start-db-cluster --db-cluster-identifier ${cluster_id} --no-cli-pager --output json
46-
47-
echo "Waiting for DB cluster to be available..."
48-
if ! aws rds wait db-cluster-available --db-cluster-identifier ${cluster_id}; then
49-
echo "Timeout waiting for DB cluster to become available"
50-
return 1
51-
fi
52-
53-
echo "DB cluster is now available"
54-
return 0
55-
}
5637

5738
# Function to resume ECS service
5839
resume_ecs_service() {
5940
local prefix=$1
6041
local env=$2
6142
local cluster="ecs-cluster-${prefix}-node-api-${env}"
6243
local service="${prefix}-node-api-${env}-service"
44+
local cluster_status=$3
6345

46+
if [ "$cluster_status" != "ACTIVE" ]; then
47+
echo "Skipping ECS resume operation: Cluster $cluster does not exist"
48+
return
49+
fi
6450
echo "Resuming ECS service ${service} on cluster ${cluster}..."
65-
6651
# Update scaling policy
6752
aws application-autoscaling register-scalable-target \
6853
--service-namespace ecs \
@@ -90,21 +75,11 @@ main() {
9075
local prefix=$2
9176

9277
echo "Starting to resume resources for environment: ${env} with stack prefix: ${prefix}"
93-
94-
# Check DB cluster status
95-
local db_status=$(check_db_cluster "$prefix" "$env")
96-
97-
if [ "$db_status" == "not-found" ]; then
98-
echo "Skipping resume operation, DB cluster does not exist"
99-
return 0
100-
elif [ "$db_status" == "stopped" ]; then
101-
start_db_cluster "$prefix" "$env" || return 1
102-
else
103-
echo "DB cluster is not in a stopped state. Current state: $db_status"
104-
fi
105-
106-
# Resume ECS service
107-
resume_ecs_service "$prefix" "$env"
78+
# Check and pause ECS service
79+
ecs_status=$(check_ecs_cluster)
80+
[ "$ecs_status" = "INACTIVE" ] || echo "ECS cluster status: $ecs_status"
81+
# Resume ECS service (DynamoDB is always available)
82+
resume_ecs_service "$prefix" "$env" "$ecs_status"
10883

10984
echo "Resources have been resumed successfully"
11085
}

.github/workflows/.builds.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
strategy:
2323
matrix:
2424
# Only building frontend containers to run PR based e2e tests
25-
package: [backend, migrations, frontend]
25+
package: [backend, frontend]
2626
timeout-minutes: 10
2727
steps:
2828
- uses: bcgov/action-builder-ghcr@v4.0.0

.github/workflows/.deployer.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ jobs:
8484
env:
8585
target_env: ${{ inputs.environment_name }}
8686
aws_license_plate: ${{ secrets.AWS_LICENSE_PLATE }}
87-
flyway_image: ghcr.io/${{github.repository}}/migrations:${{inputs.tag}}
8887
api_image: ghcr.io/${{github.repository}}/backend:${{inputs.tag}}
8988
app_env: ${{inputs.app_env}}
9089
stack_prefix: ${{ inputs.stack_prefix }}
@@ -98,7 +97,6 @@ jobs:
9897
env:
9998
target_env: ${{ inputs.environment_name }}
10099
aws_license_plate: ${{ secrets.AWS_LICENSE_PLATE }}
101-
flyway_image: ghcr.io/${{github.repository}}/migrations:${{inputs.tag}}
102100
api_image: ghcr.io/${{github.repository}}/backend:${{inputs.tag}}
103101
app_env: ${{inputs.app_env}}
104102
stack_prefix: ${{ inputs.stack_prefix }}
@@ -115,7 +113,6 @@ jobs:
115113
env:
116114
target_env: ${{ inputs.environment_name }}
117115
aws_license_plate: ${{ secrets.AWS_LICENSE_PLATE }}
118-
flyway_image: ghcr.io/${{github.repository}}/migrations:${{inputs.tag}}
119116
api_image: ghcr.io/${{github.repository}}/backend:${{inputs.tag}}
120117
app_env: ${{inputs.app_env}}
121118
stack_prefix: ${{ inputs.stack_prefix }}

.github/workflows/.e2e.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ jobs:
2626
BACKEND_IMAGE: ghcr.io/${{ github.repository }}/backend:${{ inputs.tag }}
2727
FLYWAY_IMAGE: ghcr.io/${{ github.repository }}/migrations:${{ inputs.tag }}
2828
FRONTEND_IMAGE: ghcr.io/${{ github.repository }}/frontend:${{ inputs.tag }}
29+
IS_OFFLINE: 'true' # this is for backend to run in offline mode
2930
run: docker compose up -d --wait
3031
continue-on-error: true
3132
- name: Docker Compose Logs

.github/workflows/.tests.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,6 @@ jobs:
1616
if: ${{ ! github.event.pull_request.draft }}
1717
runs-on: ubuntu-24.04
1818
timeout-minutes: 5
19-
services:
20-
postgres:
21-
image: postgis/postgis:17-3.5 # Updated to PostgreSQL 17 with PostGIS 3.5
22-
env:
23-
POSTGRES_PASSWORD: default
24-
options: >-
25-
--health-cmd pg_isready
26-
--health-interval 10s
27-
--health-timeout 5s
28-
--health-retries 5
29-
ports:
30-
- 5432:5432
3119
steps:
3220
- uses: bcgov-nr/action-test-and-analyse@v1.3.0
3321
env:

AWS-DEPLOY.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# How To Deploy to AWS using Terraform
2+
3+
## Prerequisites
4+
5+
1. BCGov AWS account/namespace.
6+
7+
## Steps to be taken in the console(UI) to setup the secret in github for terraform deployment
8+
9+
1. [Login to console via IDIR MFA](https://login.nimbus.cloud.gov.bc.ca/)
10+
2. Navigate to IAM, click on policies on left hand menu.
11+
3. Click on `Create policy` button and switch from visual to JSON then paste the below snippet
12+
13+
```json
14+
{
15+
"Version": "2012-10-17",
16+
"Statement": [
17+
{
18+
"Sid": "IAM",
19+
"Effect": "Allow",
20+
"Action": ["iam:*"],
21+
"Resource": ["*"]
22+
},
23+
{
24+
"Sid": "S3",
25+
"Effect": "Allow",
26+
"Action": ["s3:*"],
27+
"Resource": ["*"]
28+
},
29+
{
30+
"Sid": "Cloudfront",
31+
"Effect": "Allow",
32+
"Action": ["cloudfront:*"],
33+
"Resource": ["*"]
34+
},
35+
{
36+
"Sid": "ecs",
37+
"Effect": "Allow",
38+
"Action": ["ecs:*"],
39+
"Resource": "*"
40+
},
41+
{
42+
"Sid": "ecr",
43+
"Effect": "Allow",
44+
"Action": ["ecr:*"],
45+
"Resource": "*"
46+
},
47+
{
48+
"Sid": "Dynamodb",
49+
"Effect": "Allow",
50+
"Action": ["dynamodb:*"],
51+
"Resource": ["*"]
52+
},
53+
{
54+
"Sid": "APIgateway",
55+
"Effect": "Allow",
56+
"Action": ["apigateway:*"],
57+
"Resource": ["*"]
58+
},
59+
{
60+
"Sid": "Cloudwatch",
61+
"Effect": "Allow",
62+
"Action": ["cloudwatch:*"],
63+
"Resource": "*"
64+
},
65+
{
66+
"Sid": "EC2",
67+
"Effect": "Allow",
68+
"Action": ["ec2:*"],
69+
"Resource": "*"
70+
},
71+
{
72+
"Sid": "Autoscaling",
73+
"Effect": "Allow",
74+
"Action": ["autoscaling:*"],
75+
"Resource": "*"
76+
},
77+
{
78+
"Sid": "KMS",
79+
"Effect": "Allow",
80+
"Action": ["kms:*"],
81+
"Resource": "*"
82+
},
83+
{
84+
"Sid": "SecretsManager",
85+
"Effect": "Allow",
86+
"Action": ["secretsmanager:*"],
87+
"Resource": "*"
88+
},
89+
{
90+
"Sid": "CloudWatchLogs",
91+
"Effect": "Allow",
92+
"Action": ["logs:*"],
93+
"Resource": "*"
94+
},
95+
{
96+
"Sid": "WAF",
97+
"Effect": "Allow",
98+
"Action": ["wafv2:*"],
99+
"Resource": "*"
100+
},
101+
{
102+
"Sid": "ELB",
103+
"Effect": "Allow",
104+
"Action": ["elasticloadbalancing:*"],
105+
"Resource": "*"
106+
},
107+
{
108+
"Sid": "AppAutoScaling",
109+
"Effect": "Allow",
110+
"Action": ["application-autoscaling:*"],
111+
"Resource": "*"
112+
}
113+
114+
]
115+
}
116+
```
117+
4. Then create a role by clicking `create role` button and then selecting (custom trust policy radio button).
118+
5. Paste the below JSON after making modifications to set trust relationships of the role with your github repo(<repo_name> ex: bcgov/quickstart-aws-containers) .
119+
120+
```json
121+
{
122+
"Version": "2012-10-17",
123+
"Statement": [
124+
{
125+
"Effect": "Allow",
126+
"Principal": {
127+
"Federated": "arn:aws:iam::<account_number>:oidc-provider/token.actions.githubusercontent.com"
128+
},
129+
"Action": "sts:AssumeRoleWithWebIdentity",
130+
"Condition": {
131+
"StringLike": {
132+
"token.actions.githubusercontent.com:sub": "repo:<repo_name>:*"
133+
},
134+
"ForAllValues:StringEquals": {
135+
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
136+
"token.actions.githubusercontent.com:iss": "https://token.actions.githubusercontent.com"
137+
}
138+
}
139+
}
140+
]
141+
}
142+
```
143+
6. Click on Next button, then add the policies after searching for it and then enabling it by checking the checkbox.
144+
7. Finally give a role name for ex: `GHA_CI_CD` and then click on `create role` button.
145+
7. After the role is created copy the ARN, it would be like `arn:aws:iam::<account_number>:role/<role_name>` , `role_name` is what was created on step 4.
146+
8. Paste this value into github secrets, repository secret or environment secret based on your needs. The key to use is `AWS_DEPLOY_ROLE_ARN`
147+
9. Paste the license plate value( 6 alphanumeric characters ex: `ab9okj`) without the env as a repository secret. The Key to use is `AWS_LICENSE_PLATE`
148+
10. After this the github action workflows would be able to deploy the stack to AWS.

0 commit comments

Comments
 (0)