Skip to content

Commit 453b6aa

Browse files
authored
Merge pull request #557 from agiledigital/issue-540-serverless-v3
Multiple fixes
2 parents 9493a7b + 42e9675 commit 453b6aa

File tree

11 files changed

+1137
-3627
lines changed

11 files changed

+1137
-3627
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717

1818
strategy:
1919
matrix:
20-
node-version: [12.x, 14.x, 16.x]
20+
node-version: [14.x, 16.x, 18.x]
2121

2222
steps:
2323
- uses: actions/checkout@v2

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ functions:
4949
batchSize: 2 # Optional - default value is 10
5050
maximumBatchingWindowInSeconds: 10 # optional - default is 0 (no batch window)
5151
maxRetryCount: 2 # Optional - default value is 5
52-
kmsMasterKeyId: alias/aws/sqs # optional - default is none (no encryption)
52+
kmsMasterKeyId: !GetAtt SQSQueueKey.Arn # optional - default is none (no encryption) - see Notes on Encryption section below
5353
kmsDataKeyReusePeriodSeconds: 600 # optional - AWS default is 300 seconds
5454
deadLetterMessageRetentionPeriodSeconds: 1209600 # optional - AWS default is 345600 secs (4 days)
5555
visibilityTimeout: 120 # optional (in seconds) - AWS default is 30 secs
@@ -104,6 +104,58 @@ This would be set to 'true' by default if this was the first version of the plug
104104
105105
See also: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-name.html
106106
107+
### Notes on Encryption
108+
109+
If you choose to encrypt your SQS queue, the SNS topic will not be able to send it any messages if you use a managed key (alias/aws/sqs). This is due to an AWS limitation.
110+
111+
See: https://aws.amazon.com/premiumsupport/knowledge-center/sns-topic-sqs-queue-sse-kms-key-policy/
112+
113+
You will need to create a CMK like the following:
114+
115+
```yaml
116+
# To allow SNS to push messages to an encrypted queue, a CMK must be used
117+
SQSQueueCMK:
118+
Type: AWS::KMS::Key
119+
Properties:
120+
KeyPolicy:
121+
Version: "2012-10-17"
122+
Id: key-default-1
123+
Statement:
124+
- Sid: Enable IAM User Permissions
125+
Effect: Allow
126+
Principal:
127+
AWS: !Join
128+
- ""
129+
- - "arn:aws:iam::"
130+
- !Ref "AWS::AccountId"
131+
- ":root"
132+
Action: "kms:*"
133+
Resource: "*"
134+
- Sid: Allow SNS publish to SQS
135+
Effect: Allow
136+
Principal:
137+
Service: sns.amazonaws.com
138+
Action:
139+
- kms:GenerateDataKey
140+
- kms:Decrypt
141+
Resource: "*"
142+
```
143+
144+
and then reference it in the `snsSqs` config with the `kmsMasterKeyId` attribute.
145+
146+
```yaml
147+
functions:
148+
processEvent:
149+
handler: handler.handler
150+
events:
151+
- snsSqs:
152+
# ...
153+
kmsMasterKeyId: !GetAtt SQSQueueKey.Arn
154+
# ...
155+
```
156+
157+
`kmsMasterKeyId` can either be a key ID (simple string) or an ARN or reference to to ARN. Using !Ref on a key will return a key ID and is invalid, so you'll need to use GetAtt and reference the Arn property.
158+
107159
### CloudFormation Overrides
108160

109161
If you would like to override a part of the CloudFormation template

example-service/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
"license": "ISC",
1212
"devDependencies": {
1313
"@agiledigital/serverless-sns-sqs-lambda": "file:../",
14-
"serverless": "^2.19.0"
14+
"serverless": "^3.16.0"
1515
}
1616
}

example-service/serverless.yml

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
service: sns-sqs-service
2-
# Opt-in to the strict config validation that will be default in v3
3-
# See: https://www.serverless.com/framework/docs/deprecations/#new-variables-resolver
4-
variablesResolutionMode: 20210219
52
# Ensure validation issues are treated as errors
63
configValidationMode: error
4+
frameworkVersion: "3"
75

86
provider:
97
name: aws
@@ -25,7 +23,7 @@ functions:
2523
batchSize: 2
2624
maximumBatchingWindowInSeconds: 30
2725
maxRetryCount: 2
28-
kmsMasterKeyId: alias/aws/sqs
26+
kmsMasterKeyId: !GetAtt SQSQueueKey.Arn
2927
kmsDataKeyReusePeriodSeconds: 600
3028
visibilityTimeout: 120
3129
rawMessageDelivery: true
@@ -56,6 +54,32 @@ resources:
5654
Type: AWS::SNS::Topic
5755
Properties:
5856
TopicName: TestTopic
57+
# To allow SNS to push messages to an encrypted queue, a CMK must be used
58+
SQSQueueKey:
59+
Type: AWS::KMS::Key
60+
Properties:
61+
KeyPolicy:
62+
Version: "2012-10-17"
63+
Id: key-default-1
64+
Statement:
65+
- Sid: Enable IAM User Permissions
66+
Effect: Allow
67+
Principal:
68+
AWS: !Join
69+
- ""
70+
- - "arn:aws:iam::"
71+
- !Ref "AWS::AccountId"
72+
- ":root"
73+
Action: "kms:*"
74+
Resource: "*"
75+
- Sid: Allow SNS publish to SQS
76+
Effect: Allow
77+
Principal:
78+
Service: sns.amazonaws.com
79+
Action:
80+
- kms:GenerateDataKey
81+
- kms:Decrypt
82+
Resource: "*"
5983

6084
plugins:
6185
- "@agiledigital/serverless-sns-sqs-lambda"

0 commit comments

Comments
 (0)