Skip to content

Commit b3adc99

Browse files
committed
move sqs queues to terraform
1 parent 7182fd2 commit b3adc99

File tree

7 files changed

+80
-24
lines changed

7 files changed

+80
-24
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ deploy_prod: check_account_prod
9393
sam deploy $(common_params) --parameter-overrides $(run_env)=prod $(set_application_prefix)=$(application_key) $(set_application_name)="$(application_name)" S3BucketPrefix="$(s3_bucket_prefix)"
9494
@echo "Deploying Terraform..."
9595
$(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
96+
terraform -chdir=terraform/envs/prod init -lockfile=readonly
9797
terraform -chdir=terraform/envs/prod apply -auto-approve -var main_cloudfront_distribution_id="$(MAIN_DISTRIBUTION_ID)"
9898
make postdeploy
9999

@@ -102,7 +102,7 @@ deploy_dev: check_account_dev
102102
sam deploy $(common_params) --parameter-overrides $(run_env)=dev $(set_application_prefix)=$(application_key) $(set_application_name)="$(application_name)" S3BucketPrefix="$(s3_bucket_prefix)"
103103
@echo "Deploying Terraform..."
104104
$(eval MAIN_DISTRIBUTION_ID := $(shell aws cloudformation describe-stacks --stack-name $(application_key) --query "Stacks[0].Outputs[?OutputKey=='CloudfrontDistributionId'].OutputValue" --output text))
105-
terraform -chdir=terraform/envs/qa init
105+
terraform -chdir=terraform/envs/qa init -lockfile=readonly
106106
terraform -chdir=terraform/envs/qa apply -auto-approve -var main_cloudfront_distribution_id="$(MAIN_DISTRIBUTION_ID)"
107107
make postdeploy
108108

@@ -122,8 +122,8 @@ invalidate_cloudfront:
122122
install:
123123
yarn -D
124124
pip install cfn-lint
125-
terraform -chdir=terraform/envs/qa init
126-
terraform -chdir=terraform/envs/prod init
125+
terraform -chdir=terraform/envs/qa init -lockfile=readonly
126+
terraform -chdir=terraform/envs/prod init -lockfile=readonly
127127

128128
test_live_integration: install
129129
yarn test:live

cloudformation/main.yml

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ Parameters:
2222
Description: How long the SQS lambda is permitted to run (in seconds)
2323
Default: 180
2424
Type: Number
25-
SqsMessageTimeout:
26-
Description: MessageVisibilityTimeout for the SQS Lambda queue (should be at least (numMaxRetry + 1)*SqsLambdaTimeout)
27-
Default: 720
28-
Type: Number
2925
S3BucketPrefix:
3026
Description: S3 bucket prefix which will ensure global uniqueness
3127
Type: String
@@ -86,22 +82,14 @@ Resources:
8682
RunEnvironment: !Ref RunEnvironment
8783
LambdaFunctionName: !Sub ${ApplicationPrefix}-lambda
8884
SesEmailDomain: !FindInMap [General, !Ref RunEnvironment, SesDomain]
89-
SqsQueueArn: !GetAtt AppSQSQueues.Outputs.MainQueueArn
85+
SqsQueueArn: !Sub "arn:aws:sqs:${AWS::Region}:${AWS::AccountId}:infra-core-api-sqs-sales"
9086
LinkryKvArn: !GetAtt LinkryRecordsCloudfrontStore.Arn
9187

9288
AppLogGroups:
9389
Type: AWS::Serverless::Application
9490
Properties:
9591
Location: ./logs.yml
9692

97-
AppSQSQueues:
98-
Type: AWS::Serverless::Application
99-
Properties:
100-
Location: ./sqs.yml
101-
Parameters:
102-
QueueName: !Sub ${ApplicationPrefix}-sqs
103-
MessageTimeout: !Ref SqsMessageTimeout
104-
10593
LinkryRecordSetv4:
10694
Condition: IsDev
10795
Type: AWS::Route53::RecordSet
@@ -319,7 +307,7 @@ Resources:
319307
- AppSqsLambdaFunction
320308
Properties:
321309
BatchSize: 5
322-
EventSourceArn: !GetAtt AppSQSQueues.Outputs.MainQueueArn
310+
EventSourceArn: !Sub "arn:aws:sqs:${AWS::Region}:${AWS::AccountId}:infra-core-api-sqs"
323311
FunctionName: !Sub ${ApplicationPrefix}-sqs-lambda
324312
FunctionResponseTypes:
325313
- ReportBatchItemFailures
@@ -330,7 +318,7 @@ Resources:
330318
- AppSqsLambdaFunction
331319
Properties:
332320
BatchSize: 5
333-
EventSourceArn: !GetAtt AppSQSQueues.Outputs.SalesEmailQueueArn
321+
EventSourceArn: !Sub "arn:aws:sqs:${AWS::Region}:${AWS::AccountId}:infra-core-api-sqs-sales"
334322
FunctionName: !Sub ${ApplicationPrefix}-sqs-lambda
335323
FunctionResponseTypes:
336324
- ReportBatchItemFailures
@@ -1011,4 +999,4 @@ Outputs:
1011999

10121000
SalesEmailQueueArn:
10131001
Description: Sales Email Queue Arn
1014-
Value: !GetAtt AppSQSQueues.Outputs.SalesEmailQueueArn
1002+
Value: !Sub "arn:aws:sqs:${AWS::Region}:${AWS::AccountId}:infra-core-api-sqs-sales"

cspell.config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ignorePaths:
66
dictionaryDefinitions: []
77
dictionaries: []
88
words:
9+
- redrive
910
- UIUC
1011
ignoreWords: []
1112
import: []

terraform/envs/prod/main.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ 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
@@ -34,3 +30,8 @@ module "app_alarms" {
3430
priority_sns_arn = var.GeneralSNSAlertArn
3531
standard_sns_arn = var.PrioritySNSAlertArn
3632
}
33+
34+
module "sqs_queues" {
35+
source = "../../modules/sqs"
36+
resource_prefix = var.ProjectId
37+
}

terraform/envs/qa/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ resource "aws_cloudwatch_log_group" "main_app_logs" {
2121
name = "/aws/lambda/${var.ProjectId}-lambda"
2222
retention_in_days = var.LogRetentionDays
2323
}
24+
module "sqs_queues" {
25+
source = "../../modules/sqs"
26+
resource_prefix = var.ProjectId
27+
}

terraform/modules/sqs/main.tf

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
}
6+
}
7+
}
8+
9+
resource "aws_sqs_queue" "app_dlq" {
10+
name = "${var.resource_prefix}-sqs-dlq"
11+
visibility_timeout_seconds = var.sqs_message_timeout
12+
message_retention_seconds = 1209600
13+
}
14+
15+
resource "aws_sqs_queue" "app_queue" {
16+
name = "${var.resource_prefix}-sqs"
17+
visibility_timeout_seconds = var.sqs_message_timeout
18+
redrive_policy = {
19+
deadLetterTargetArn = aws_sqs_queue.app_dlq.arn
20+
maxReceiveCount = 3
21+
}
22+
}
23+
24+
resource "aws_sqs_queue" "sales_email_queue" {
25+
name = "${var.resource_prefix}-sqs-sales"
26+
visibility_timeout_seconds = var.sqs_message_timeout
27+
redrive_policy = {
28+
deadLetterTargetArn = aws_sqs_queue.app_dlq.arn
29+
maxReceiveCount = 3
30+
}
31+
}
32+
33+
output "main_queue_arn" {
34+
description = "Main Queue Arn"
35+
value = aws_sqs_queue.app_queue.arn
36+
}
37+
38+
output "dlq_arn" {
39+
description = "Dead-letter Queue Arn"
40+
value = aws_sqs_queue.app_dlq.arn
41+
}
42+
43+
output "sales_email_queue_arn" {
44+
description = "Sales Email Queue Arn"
45+
value = aws_sqs_queue.sales_email_queue.arn
46+
}

terraform/modules/sqs/variables.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
variable "resource_prefix" {
2+
type = string
3+
description = "Prefix before each resource"
4+
}
5+
6+
variable "sqs_message_timeout" {
7+
type = number
8+
description = "SQS Message timeout in seconds"
9+
default = 720
10+
}
11+
12+
variable "dlq_message_retention" {
13+
type = number
14+
description = "DLQ Message retention in seconds"
15+
default = 1209600
16+
}

0 commit comments

Comments
 (0)