无服务器 Sns Sqs Lambda
这是AWS lambda 函数的无服务器框架插件 。 现在,它
可以直接订阅 SNS 主题。 不过,如果你想
提供重试能力和错误处理, 您需要写很多
锅炉板,用于在Lambda号与Lambda号之间添加一个队列和一个死字队列
SNS话题. 此插件允许您使用 : batchSize
(单位:千美元) maxRetryCount 仅作为直接签署该议题.
目录
安装
运行 npm install 在您的无服务器项目中 .
$ npm install --save-dev @agiledigital/serverless-sns-sqs-lambda
将插件添加到您的无服务器. yml 文件
plugins:
- "@agiledigital/serverless-sns-sqs-lambda"
设置
以 snsSqs 事件提供Lambda 函数,插件会添加 AWS SNS 话题和订阅, SQS 队列和死字母队列,以及Lambda 的角色需要.
functions:
processEvent:
handler: handler.handler
events:
- snsSqs:
name: TestEvent # Required - choose a name prefix for the event queue
topicArn: !Ref Topic # Required - SNS topic to subscribe to
omitPhysicalId: true # Optional - default value is false but recommended to be set to true for new deployments (see below)
batchSize: 2 # Optional - default value is 10
maximumBatchingWindowInSeconds: 10 # optional - default is 0 (no batch window)
maxRetryCount: 2 # Optional - default value is 5
kmsMasterKeyId: !GetAtt SQSQueueKey.Arn # optional - default is none (no encryption) - see Notes on Encryption section below
kmsDataKeyReusePeriodSeconds: 600 # optional - AWS default is 300 seconds
deadLetterMessageRetentionPeriodSeconds: 1209600 # optional - AWS default is 345600 secs (4 days)
deadLetterQueueEnabled: true # optional - default is true
visibilityTimeout: 120 # optional (in seconds) - AWS default is 30 secs
rawMessageDelivery: true # Optional - default value is true
enabled: true # Optional - default value is true
filterPolicy: # Optional - filter messages that are handled
pets:
- dog
- cat
# Overrides for generated CloudFormation templates
# Mirrors the CloudFormation docs but uses camel case instead of title case
#
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-queues.html
mainQueueOverride:
maximumMessageSize: 1024
...
deadLetterQueueOverride:
maximumMessageSize: 1024
...
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html
eventSourceMappingOverride:
sourceAccessConfigurations:
- Type: SASL_SCRAM_256_AUTH
URI: arn:aws:secretsmanager:us-east-1:01234567890:secret:MyBrokerSecretName
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sns-subscription.html
subscriptionOverride:
region: ap-southeast-2
resources:
Resources:
Topic:
Type: AWS::SNS::Topic
Properties:
TopicName: TestTopic
plugins:
- "@agiledigital/serverless-sns-sqs-lambda"
省略物理指数选项是什么?
AWS 允许您省略 SQS 资源的物理ID( 队列名称) .
在这种情况下,名称由AWS根据逻辑ID(Cloud Formation资源名)自动生成.
这提供了一些好处,例如能够进行需要替换的更新,而不必担心队列名称的80个字符最大长度.
然而,如果需要按名称引用队列(这应该少有),而不是CloudFormation的引用,你可能必须关闭它,以便名称稳定.
如果这是插件的第一个版本, 将默认设定为“ true ” 。 然而,由于插件已经在使用中, 它可能打破已有的堆栈, 并且可能不向后兼容, 因此默认情况下我们设置它为“ 假的 ” 。 将它切换到一个已存在的堆栈上的“ true” 已经测试过, 它似乎可以工作, 尽管它确实创建了新的队列并删除旧队列 。 在已有的堆栈上切换选项, 以你自己的风险 .
关于加密的说明
如果您选择加密您的 SQS 队列, SNS 话题将无法使用管理下的密钥( 化名/ aws/ sqs) 发送任何消息 。 这是由于AWS的限制.
您需要创建如下的CMK :
# To allow SNS to push messages to an encrypted queue, a CMK must be used
SQSQueueCMK:
Type: AWS::KMS::Key
Properties:
KeyPolicy:
Version: "2012-10-17"
Id: key-default-1
Statement:
- Sid: Enable IAM User Permissions
Effect: Allow
Principal:
AWS: !Join
- ""
- - "arn:aws:iam::"
- !Ref "AWS::AccountId"
- ":root"
Action: "kms:*"
Resource: "*"
- Sid: Allow SNS publish to SQS
Effect: Allow
Principal:
Service: sns.amazonaws.com
Action:
- kms:GenerateDataKey
- kms:Decrypt
Resource: "*"
并随后在 snsSqs 配置为 kmsMasterKeyId 属性.
functions:
processEvent:
handler: handler.handler
events:
- snsSqs:
# ...
kmsMasterKeyId: !GetAtt SQSQueueKey.Arn
# ...
kmsMasterKeyId 可以是密钥ID(简单字符串),也可以是ARN或指向ARN. 使用! 对一个密钥的校正会返回一个密钥ID并且无效,所以您需要使用GetAtt并引用Arn属性.
云态覆盖
如果您想要覆盖 CloudFormation 模板的一部分 此插件生成的 CloudFormation 可以通过原始的 CloudFormation 以覆盖上述配置选项.
配置必须提供骆驼大小写键, 除此之外,你可以使用"云形"配置 由AWS指定.
例如, 如果您想要覆盖主队列的最大MessageSize 您可以在 AWS 文档 将关键骆驼大小写(“最大误差”)并传入覆盖部分:
events:
- snsSqs:
name: Example
...
mainQueueOverride:
maximumMessageSize: 1024