Skip to content

Commit 4c28e3e

Browse files
authored
Move alarms to Terraform (#220)
Also remove old resources from cloudformation
1 parent 9041c1e commit 4c28e3e

File tree

12 files changed

+281
-166
lines changed

12 files changed

+281
-166
lines changed

.github/workflows/manual-prod.yml

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
name: Deploy all resources to PROD (Manual)
2+
run-name: Manual PROD deploy - @${{ github.actor }}
3+
4+
on:
5+
workflow_dispatch:
6+
7+
jobs:
8+
test:
9+
runs-on: ubuntu-latest
10+
timeout-minutes: 15
11+
name: Run Unit Tests
12+
steps:
13+
- uses: actions/checkout@v4
14+
env:
15+
HUSKY: "0"
16+
17+
- name: Set up Node
18+
uses: actions/setup-node@v4
19+
with:
20+
node-version: 22.x
21+
cache: "yarn"
22+
23+
- name: Setup Terraform
24+
uses: hashicorp/setup-terraform@v2
25+
with:
26+
terraform_version: 1.12.2
27+
28+
- name: Restore Yarn Cache
29+
uses: actions/cache@v4
30+
with:
31+
path: node_modules
32+
key: yarn-modules-${{ runner.arch }}-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}-dev
33+
restore-keys: |
34+
yarn-modules-${{ runner.arch }}-${{ runner.os }}-
35+
36+
- name: Run unit testing
37+
run: make test_unit
38+
39+
build:
40+
runs-on: ubuntu-24.04-arm
41+
timeout-minutes: 15
42+
name: Build Application
43+
steps:
44+
- uses: actions/checkout@v4
45+
env:
46+
HUSKY: "0"
47+
48+
- name: Set up Node
49+
uses: actions/setup-node@v4
50+
with:
51+
node-version: 22.x
52+
cache: "yarn"
53+
54+
- name: Restore Yarn Cache
55+
uses: actions/cache@v4
56+
with:
57+
path: node_modules
58+
key: yarn-modules-${{ runner.arch }}-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}-prod
59+
restore-keys: |
60+
yarn-modules-${{ runner.arch }}-${{ runner.os }}-
61+
62+
- name: Run build
63+
run: make build
64+
env:
65+
HUSKY: "0"
66+
VITE_RUN_ENVIRONMENT: prod
67+
RunEnvironment: prod
68+
VITE_BUILD_HASH: ${{ github.sha }}
69+
70+
- name: Upload Build files
71+
uses: actions/upload-artifact@v4
72+
with:
73+
include-hidden-files: true
74+
name: build-prod
75+
path: |
76+
.aws-sam/
77+
dist/
78+
dist_ui/
79+
80+
deploy-prod:
81+
runs-on: ubuntu-latest
82+
timeout-minutes: 30
83+
name: Deploy to Prod and Run Health Check
84+
concurrency:
85+
group: ${{ github.event.repository.name }}-prod
86+
cancel-in-progress: false
87+
permissions:
88+
id-token: write
89+
contents: read
90+
needs:
91+
- test
92+
- build
93+
environment: "AWS PROD"
94+
steps:
95+
- name: Set up Node for testing
96+
uses: actions/setup-node@v4
97+
with:
98+
node-version: 22.x
99+
100+
- name: Setup Terraform
101+
uses: hashicorp/setup-terraform@v2
102+
with:
103+
terraform_version: 1.12.2
104+
105+
- uses: actions/checkout@v4
106+
env:
107+
HUSKY: "0"
108+
- uses: aws-actions/setup-sam@v2
109+
with:
110+
use-installer: true
111+
- name: Set up Python 3.11
112+
uses: actions/setup-python@v5
113+
with:
114+
python-version: 3.11
115+
- name: Download Build files
116+
uses: actions/download-artifact@v4
117+
with:
118+
name: build-prod
119+
- uses: aws-actions/configure-aws-credentials@v4
120+
with:
121+
role-to-assume: arn:aws:iam::298118738376:role/GitHubActionsRole
122+
role-session-name: Manual_Core_Prod_Deployment_${{ github.run_id }}
123+
aws-region: us-east-1
124+
- name: Publish to AWS
125+
run: make deploy_prod
126+
env:
127+
HUSKY: "0"
128+
VITE_RUN_ENVIRONMENT: prod
129+
- name: Call the health check script
130+
run: make prod_health_check

.husky/pre-commit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if [ -n "$STAGED_FILES" ]; then
66
# Run lint on all files (modifies files in the working directory)
77
yarn lint --fix
88
yarn prettier:write
9+
terraform -chdir=terraform/ fmt --recursive
910

1011
echo "Re-adding originally staged files to the staging area..."
1112
# Re-add only the originally staged files

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,20 @@ postdeploy:
9090

9191
deploy_prod: check_account_prod
9292
@echo "Deploying CloudFormation stack..."
93-
terraform -chdir=terraform/envs/prod init
94-
terraform -chdir=terraform/envs/prod apply -auto-approve
9593
sam deploy $(common_params) --parameter-overrides $(run_env)=prod $(set_application_prefix)=$(application_key) $(set_application_name)="$(application_name)" S3BucketPrefix="$(s3_bucket_prefix)"
94+
@echo "Deploying Terraform..."
95+
$(eval MAIN_DISTRIBUTION_ID := $(shell aws cloudformation describe-stacks --stack-name $(application_key) --query "Stacks[0].Outputs[?OutputKey=='CloudfrontDistributionId'].OutputValue" --output text))
96+
terraform -chdir=terraform/envs/prod init
97+
terraform -chdir=terraform/envs/prod apply -auto-approve -var main_cloudfront_distribution_id="$(MAIN_DISTRIBUTION_ID)"
9698
make postdeploy
9799

98100
deploy_dev: check_account_dev
99101
@echo "Deploying CloudFormation stack..."
100102
sam deploy $(common_params) --parameter-overrides $(run_env)=dev $(set_application_prefix)=$(application_key) $(set_application_name)="$(application_name)" S3BucketPrefix="$(s3_bucket_prefix)"
101103
@echo "Deploying Terraform..."
104+
$(eval MAIN_DISTRIBUTION_ID := $(shell aws cloudformation describe-stacks --stack-name $(application_key) --query "Stacks[0].Outputs[?OutputKey=='CloudfrontDistributionId'].OutputValue" --output text))
102105
terraform -chdir=terraform/envs/qa init
103-
terraform -chdir=terraform/envs/qa apply -auto-approve
106+
terraform -chdir=terraform/envs/qa apply -auto-approve -var main_cloudfront_distribution_id="$(MAIN_DISTRIBUTION_ID)"
104107
make postdeploy
105108

106109
invalidate_cloudfront:

cloudformation/alerting.yml

Lines changed: 0 additions & 113 deletions
This file was deleted.

cloudformation/logs.yml

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
AWSTemplateFormatVersion: '2010-09-09'
22
Description: Stack Log Groups
33
Transform: AWS::Serverless-2016-10-31
4-
Parameters:
5-
LambdaFunctionName:
6-
Type: String
7-
AllowedPattern: ^[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+$
8-
LogRetentionDays:
9-
Type: Number
4+
105
Resources:
11-
AppApiLambdaLogGroup:
12-
Type: AWS::Logs::LogGroup
13-
DeletionPolicy: Retain
14-
UpdateReplacePolicy: Retain
15-
Properties:
16-
LogGroupName:
17-
Fn::Sub: /aws/lambda/${LambdaFunctionName}
18-
RetentionInDays:
19-
Ref: LogRetentionDays
206
AppAuditLog:
217
Type: "AWS::DynamoDB::Table"
228
DeletionPolicy: "Retain"

cloudformation/main.yml

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ Parameters:
66
RunEnvironment:
77
Type: String
88
AllowedValues: ["dev", "prod"]
9-
AlertSNSArn:
10-
Description: SNS Queue to send general alarm alerts to (prod only)
11-
Type: String
12-
Default: arn:aws:sns:us-east-1:298118738376:infra-monitor-alerts
13-
PriorityAlertSNSArn:
14-
Description: SNS Queue to send priority alarm alerts to (prod only)
15-
Type: String
16-
Default: arn:aws:sns:us-east-1:298118738376:infra-core-api-priority-alerts
179
ApplicationPrefix:
1810
Type: String
1911
Description: Application prefix, no ending dash
@@ -46,10 +38,8 @@ Conditions:
4638
Mappings:
4739
General:
4840
dev:
49-
LogRetentionDays: 7
5041
SesDomain: "aws.qa.acmuiuc.org"
5142
prod:
52-
LogRetentionDays: 90
5343
SesDomain: "acm.illinois.edu"
5444
ApiGwConfig:
5545
dev:
@@ -103,10 +93,6 @@ Resources:
10393
Type: AWS::Serverless::Application
10494
Properties:
10595
Location: ./logs.yml
106-
Parameters:
107-
LambdaFunctionName: !Sub ${ApplicationPrefix}-lambda
108-
LogRetentionDays:
109-
!FindInMap [General, !Ref RunEnvironment, LogRetentionDays]
11096

11197
AppSQSQueues:
11298
Type: AWS::Serverless::Application
@@ -116,18 +102,6 @@ Resources:
116102
QueueName: !Sub ${ApplicationPrefix}-sqs
117103
MessageTimeout: !Ref SqsMessageTimeout
118104

119-
AppAlarms:
120-
Condition: IsProd
121-
Type: AWS::Serverless::Application
122-
Properties:
123-
Location: ./alerting.yml
124-
Parameters:
125-
AlertSNSArn: !Ref AlertSNSArn
126-
PriorityAlertSNSArn: !Ref PriorityAlertSNSArn
127-
ApplicationPrefix: !Ref ApplicationPrefix
128-
ApplicationFriendlyName: !Ref ApplicationFriendlyName
129-
MainCloudfrontDistributionId: !GetAtt AppFrontendCloudfrontDistribution.Id
130-
131105
LinkryRecordSetv4:
132106
Condition: IsDev
133107
Type: AWS::Route53::RecordSet

terraform/envs/prod/main.tf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ provider "aws" {
1818
}
1919
}
2020

21-
import {
22-
to = aws_cloudwatch_log_group.main_app_logs
23-
id = "/aws/lambda/${var.ProjectId}-lambda"
24-
}
2521
resource "aws_cloudwatch_log_group" "main_app_logs" {
2622
name = "/aws/lambda/${var.ProjectId}-lambda"
2723
retention_in_days = var.LogRetentionDays
2824
}
25+
26+
module "app_alarms" {
27+
source = "../../modules/alarms"
28+
main_cloudfront_distribution_id = var.main_cloudfront_distribution_id
29+
resource_prefix = var.ProjectId
30+
priority_sns_arn = var.GeneralSNSAlertArn
31+
standard_sns_arn = var.PrioritySNSAlertArn
32+
}

terraform/envs/prod/variables.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,17 @@ variable "ProjectId" {
88
default = "infra-core-api"
99
}
1010

11+
variable "main_cloudfront_distribution_id" {
12+
type = string
13+
description = "(temporary) ID for the cloudfront distribution that serves the main application"
14+
}
15+
16+
variable "GeneralSNSAlertArn" {
17+
type = string
18+
default = "arn:aws:sns:us-east-1:298118738376:infra-monitor-alerts"
19+
}
20+
21+
variable "PrioritySNSAlertArn" {
22+
type = string
23+
default = "arn:aws:sns:us-east-1:298118738376:infra-core-api-priority-alerts"
24+
}

0 commit comments

Comments
 (0)