Skip to content

Commit 34828b5

Browse files
committed
Merge branch 'VED-80-id-sync-sqs' into VED-480-Number-Update-terraform
2 parents aae2c94 + b82910b commit 34828b5

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

terraform/sqs_id_sync.tf

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Standard SQS Queue
2+
3+
resource "aws_sqs_queue" "id_sync_queue" {
4+
name = "${local.short_prefix}-id-sync-queue"
5+
kms_master_key_id = aws_kms_alias.id_sync_sqs_encryption.name
6+
visibility_timeout_seconds = 60
7+
redrive_policy = jsonencode({
8+
deadLetterTargetArn = aws_sqs_queue.id_sync_dlq.arn
9+
maxReceiveCount = 4
10+
})
11+
}
12+
13+
# DLQ for id-sync-queue
14+
15+
resource "aws_sqs_queue" "id_sync_dlq" {
16+
name = "${local.short_prefix}-id-sync-dlq"
17+
}
18+
19+
resource "aws_sqs_queue_redrive_allow_policy" "id_sync_queue_redrive_allow_policy" {
20+
queue_url = aws_sqs_queue.id_sync_dlq.id
21+
22+
redrive_allow_policy = jsonencode({
23+
redrivePermission = "byQueue",
24+
sourceQueueArns = [aws_sqs_queue.id_sync_queue.arn]
25+
})
26+
}
27+
28+
# IAM policy.
29+
# TODO: this is currently a global allow policy.
30+
# Refine this to allow receive from our lambda, and send from MNS
31+
32+
data "aws_iam_policy_document" "id_sync_sqs_policy" {
33+
statement {
34+
sid = "id-sync-queue SQS statement"
35+
effect = "Allow"
36+
37+
principals {
38+
type = "AWS"
39+
identifiers = ["*"]
40+
}
41+
42+
actions = [
43+
"sqs:SendMessage",
44+
"sqs:ReceiveMessage"
45+
]
46+
resources = [
47+
aws_sqs_queue.id_sync_queue.arn
48+
]
49+
}
50+
}
51+
52+
resource "aws_sqs_queue_policy" "id_sync_sqs_policy" {
53+
queue_url = aws_sqs_queue.id_sync_queue.id
54+
policy = data.aws_iam_policy_document.id_sync_sqs_policy.json
55+
}

terraform/temp_id_sync_sqs_kms.tf

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# NOTE. This is a temporary file.
2+
# Eventually the aws_kms_key "id_sync_sqs_key" will go into infra/kms.tf
3+
4+
locals {
5+
6+
# from infra/environments/non-prod/variables.tfvars
7+
# NOTE: this is only going to work in non-prod for now.
8+
9+
imms_account_id = "345594581768"
10+
admin_role = "root"
11+
dev_ops_role = "role/DevOps"
12+
auto_ops_role = "role/auto-ops"
13+
14+
# from infra/kms.tf
15+
16+
policy_statement_allow_administration = {
17+
Sid = "AllowKeyAdministration",
18+
Effect = "Allow",
19+
Principal = {
20+
AWS = "arn:aws:iam::${local.imms_account_id}:${local.admin_role}"
21+
},
22+
Action = [
23+
"kms:Create*",
24+
"kms:Describe*",
25+
"kms:Enable*",
26+
"kms:List*",
27+
"kms:Put*",
28+
"kms:Update*",
29+
"kms:Revoke*",
30+
"kms:Disable*",
31+
"kms:Get*",
32+
"kms:Delete*",
33+
"kms:ScheduleKeyDeletion",
34+
"kms:CancelKeyDeletion",
35+
"kms:GenerateDataKey*",
36+
"kms:Decrypt",
37+
"kms:Tag*"
38+
],
39+
Resource = "*"
40+
}
41+
42+
policy_statement_allow_auto_ops = {
43+
Sid = "KMSKeyUserAccess",
44+
Effect = "Allow",
45+
Principal = {
46+
AWS = "arn:aws:iam::${local.imms_account_id}:${local.auto_ops_role}"
47+
},
48+
Action = [
49+
"kms:Encrypt",
50+
"kms:GenerateDataKey*"
51+
],
52+
Resource = "*"
53+
}
54+
55+
policy_statement_allow_devops = {
56+
Sid = "KMSKeyUserAccessForDevOps",
57+
Effect = "Allow",
58+
Principal = {
59+
AWS = "arn:aws:iam::${local.imms_account_id}:${local.dev_ops_role}"
60+
},
61+
Action = [
62+
"kms:Encrypt",
63+
"kms:GenerateDataKey*"
64+
],
65+
Resource = "*"
66+
}
67+
68+
# -- New elements relating to id_sync are below here
69+
70+
# MNS id/role: ultimately these should go in infra/environments/<env>/variables.tfvars
71+
72+
mns_account_id = "631615744739"
73+
mns_admin_role = "role/nhs-mns-events-lambda-delivery"
74+
75+
policy_statement_allow_mns = {
76+
Sid = "AllowMNSLambdaDelivery",
77+
Effect = "Allow",
78+
Principal = {
79+
AWS = "arn:aws:iam::${local.mns_account_id}:${local.mns_admin_role}"
80+
},
81+
Action = "kms:GenerateDataKey",
82+
Resource = "*"
83+
}
84+
}
85+
86+
resource "aws_kms_key" "id_sync_sqs_encryption" {
87+
description = "KMS key for MNS service access"
88+
key_usage = "ENCRYPT_DECRYPT"
89+
enable_key_rotation = true
90+
policy = jsonencode({
91+
Version = "2012-10-17",
92+
Id = "key-consolepolicy-3",
93+
Statement = [
94+
local.policy_statement_allow_administration,
95+
local.policy_statement_allow_auto_ops,
96+
local.policy_statement_allow_devops,
97+
local.policy_statement_allow_mns
98+
]
99+
})
100+
}
101+
102+
resource "aws_kms_alias" "id_sync_sqs_encryption" {
103+
name = "alias/imms-event-id-sync-sqs-encryption"
104+
target_key_id = aws_kms_key.id_sync_sqs_encryption.key_id
105+
}

terraform/variables.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,7 @@ data "aws_kms_key" "existing_lambda_encryption_key" {
9494
data "aws_kms_key" "existing_kinesis_encryption_key" {
9595
key_id = "alias/imms-batch-kinesis-stream-encryption"
9696
}
97+
98+
data "aws_kms_key" "existing_id_sync_sqs_encryption_key" {
99+
key_id = "alias/imms-event-id-sync-sqs-encryption"
100+
}

0 commit comments

Comments
 (0)