Skip to content

Commit 92fe5b9

Browse files
committed
fix: remove SQS/SNS resources to fix slow destroy in terraform-production-ready
1 parent e059612 commit 92fe5b9

15 files changed

Lines changed: 11 additions & 220 deletions

File tree

terraform-tutorial/terraform-production-ready/assets/step1/main.tf

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ variable "vpc_cidr" {
5555
default = "10.0.0.0/16"
5656
}
5757

58-
variable "message_retention_seconds" {
59-
type = number
60-
default = 86400
61-
}
62-
6358
# ══════════════════════════════════════════════════════════════════════════════
6459
# 网络层
6560
# ══════════════════════════════════════════════════════════════════════════════
@@ -325,51 +320,6 @@ resource "aws_dynamodb_table" "users" {
325320
}
326321
}
327322

328-
resource "aws_sqs_queue" "task_dlq" {
329-
name = "${var.app_name}-${var.environment}-tasks-dlq"
330-
message_retention_seconds = 1209600
331-
}
332-
333-
resource "aws_sqs_queue" "tasks" {
334-
name = "${var.app_name}-${var.environment}-tasks"
335-
visibility_timeout_seconds = 60
336-
message_retention_seconds = var.message_retention_seconds
337-
}
338-
339-
resource "aws_sqs_queue_redrive_policy" "tasks" {
340-
queue_url = aws_sqs_queue.tasks.id
341-
redrive_policy = jsonencode({
342-
deadLetterTargetArn = aws_sqs_queue.task_dlq.arn
343-
maxReceiveCount = 5
344-
})
345-
}
346-
347-
resource "aws_sns_topic" "alerts" {
348-
name = "${var.app_name}-${var.environment}-alerts"
349-
}
350-
351-
resource "aws_sns_topic_subscription" "alerts_to_queue" {
352-
topic_arn = aws_sns_topic.alerts.arn
353-
protocol = "sqs"
354-
endpoint = aws_sqs_queue.tasks.arn
355-
}
356-
357-
resource "aws_sqs_queue_policy" "allow_sns" {
358-
queue_url = aws_sqs_queue.tasks.id
359-
policy = jsonencode({
360-
Version = "2012-10-17"
361-
Statement = [{
362-
Effect = "Allow"
363-
Principal = { Service = "sns.amazonaws.com" }
364-
Action = "sqs:SendMessage"
365-
Resource = aws_sqs_queue.tasks.arn
366-
Condition = {
367-
ArnEquals = { "aws:SourceArn" = aws_sns_topic.alerts.arn }
368-
}
369-
}]
370-
})
371-
}
372-
373323
# ══════════════════════════════════════════════════════════════════════════════
374324
# 存储层
375325
# ══════════════════════════════════════════════════════════════════════════════
@@ -462,11 +412,6 @@ resource "aws_iam_policy" "app" {
462412
Action = ["s3:GetObject", "s3:PutObject"]
463413
Resource = ["${aws_s3_bucket.static_assets.arn}/*"]
464414
},
465-
{
466-
Effect = "Allow"
467-
Action = ["sqs:SendMessage", "sqs:ReceiveMessage", "sqs:DeleteMessage"]
468-
Resource = aws_sqs_queue.tasks.arn
469-
},
470415
{
471416
Effect = "Allow"
472417
Action = ["dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:Query"]
@@ -516,10 +461,6 @@ output "backup_bucket" {
516461
value = aws_s3_bucket.backups.bucket
517462
}
518463

519-
output "task_queue_url" {
520-
value = aws_sqs_queue.tasks.url
521-
}
522-
523464
output "users_table" {
524465
value = aws_dynamodb_table.users.name
525466
}

terraform-tutorial/terraform-production-ready/assets/step2/main.tf

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ module "web" {
5959
module "data" {
6060
source = "./modules/data"
6161

62-
app_name = var.app_name
63-
environment = var.environment
64-
message_retention_seconds = var.message_retention_seconds
62+
app_name = var.app_name
63+
environment = var.environment
6564
}
6665

6766
# ── 存储层:S3 静态资源 + 备份 ────────────────────────────────────────────
@@ -80,7 +79,7 @@ module "security" {
8079
environment = var.environment
8180

8281
static_bucket_arn = module.storage.static_bucket_arn
83-
task_queue_arn = module.data.task_queue_arn
82+
task_queue_arn = "" # SQS removed
8483
users_table_arn = module.data.users_table_arn
8584
app_config_arn = aws_ssm_parameter.app_config.arn
8685
log_group_arn = aws_cloudwatch_log_group.app.arn
Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# 数据与消息层:DynamoDB + SQS + SNS
2-
# 对应三层架构中的数据层和应用层间的异步通信
1+
# 数据层:DynamoDB
32

43
resource "aws_dynamodb_table" "users" {
54
name = "${var.app_name}-${var.environment}-users"
@@ -17,48 +16,3 @@ resource "aws_dynamodb_table" "users" {
1716
type = "S"
1817
}
1918
}
20-
21-
resource "aws_sqs_queue" "dlq" {
22-
name = "${var.app_name}-${var.environment}-tasks-dlq"
23-
message_retention_seconds = 1209600
24-
}
25-
26-
resource "aws_sqs_queue" "tasks" {
27-
name = "${var.app_name}-${var.environment}-tasks"
28-
visibility_timeout_seconds = 60
29-
message_retention_seconds = var.message_retention_seconds
30-
}
31-
32-
resource "aws_sqs_queue_redrive_policy" "tasks" {
33-
queue_url = aws_sqs_queue.tasks.id
34-
redrive_policy = jsonencode({
35-
deadLetterTargetArn = aws_sqs_queue.dlq.arn
36-
maxReceiveCount = 5
37-
})
38-
}
39-
40-
resource "aws_sns_topic" "alerts" {
41-
name = "${var.app_name}-${var.environment}-alerts"
42-
}
43-
44-
resource "aws_sns_topic_subscription" "alerts_to_queue" {
45-
topic_arn = aws_sns_topic.alerts.arn
46-
protocol = "sqs"
47-
endpoint = aws_sqs_queue.tasks.arn
48-
}
49-
50-
resource "aws_sqs_queue_policy" "allow_sns" {
51-
queue_url = aws_sqs_queue.tasks.id
52-
policy = jsonencode({
53-
Version = "2012-10-17"
54-
Statement = [{
55-
Effect = "Allow"
56-
Principal = { Service = "sns.amazonaws.com" }
57-
Action = "sqs:SendMessage"
58-
Resource = aws_sqs_queue.tasks.arn
59-
Condition = {
60-
ArnEquals = { "aws:SourceArn" = aws_sns_topic.alerts.arn }
61-
}
62-
}]
63-
})
64-
}

terraform-tutorial/terraform-production-ready/assets/step2/modules/data/outputs.tf

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,3 @@ output "users_table_name" {
55
output "users_table_arn" {
66
value = aws_dynamodb_table.users.arn
77
}
8-
9-
output "task_queue_url" {
10-
value = aws_sqs_queue.tasks.url
11-
}
12-
13-
output "task_queue_arn" {
14-
value = aws_sqs_queue.tasks.arn
15-
}
16-
17-
output "alert_topic_arn" {
18-
value = aws_sns_topic.alerts.arn
19-
}
20-
21-
output "db_secret_arn" {
22-
value = ""
23-
}

terraform-tutorial/terraform-production-ready/assets/step2/modules/data/variables.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,3 @@ variable "billing_mode" {
1010
type = string
1111
default = "PAY_PER_REQUEST"
1212
}
13-
14-
variable "message_retention_seconds" {
15-
type = number
16-
default = 86400
17-
}

terraform-tutorial/terraform-production-ready/assets/step2/modules/security/main.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ resource "aws_iam_policy" "app" {
3737
Action = ["s3:GetObject", "s3:PutObject"]
3838
Resource = ["${var.static_bucket_arn}/*"]
3939
},
40-
{
41-
Effect = "Allow"
42-
Action = ["sqs:SendMessage", "sqs:ReceiveMessage", "sqs:DeleteMessage"]
43-
Resource = var.task_queue_arn
44-
},
4540
{
4641
Effect = "Allow"
4742
Action = ["dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:Query"]

terraform-tutorial/terraform-production-ready/assets/step2/modules/security/variables.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ variable "static_bucket_arn" {
1010
type = string
1111
}
1212

13-
variable "task_queue_arn" {
14-
type = string
15-
}
16-
1713
variable "users_table_arn" {
1814
type = string
1915
}

terraform-tutorial/terraform-production-ready/assets/step2/outputs.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ output "backup_bucket" {
1414
value = module.storage.backup_bucket_name
1515
}
1616

17-
output "task_queue_url" {
18-
value = module.data.task_queue_url
19-
}
20-
2117
output "users_table" {
2218
value = module.data.users_table_name
2319
}

terraform-tutorial/terraform-production-ready/assets/step2/variables.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,3 @@ variable "vpc_cidr" {
1212
type = string
1313
default = "10.0.0.0/16"
1414
}
15-
16-
variable "message_retention_seconds" {
17-
type = number
18-
default = 86400
19-
}

terraform-tutorial/terraform-production-ready/assets/step4/main.tf

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ module "web" {
5656
module "data" {
5757
source = "./modules/data"
5858

59-
app_name = var.app_name
60-
environment = var.environment
61-
message_retention_seconds = var.message_retention_seconds
59+
app_name = var.app_name
60+
environment = var.environment
6261
}
6362

6463
module "storage" {
@@ -75,7 +74,7 @@ module "security" {
7574
environment = var.environment
7675

7776
static_bucket_arn = module.storage.static_bucket_arn
78-
task_queue_arn = module.data.task_queue_arn
77+
task_queue_arn = "" # SQS removed
7978
users_table_arn = module.data.users_table_arn
8079
app_config_arn = aws_ssm_parameter.app_config.arn
8180
log_group_arn = aws_cloudwatch_log_group.app.arn

0 commit comments

Comments
 (0)