Skip to content

Commit 563583d

Browse files
mndevecihawflaukaidih
authored
Release/v1.29.0 (#1769) (#1773)
* Mq event source (#60) * Support AmazonMQ as event source * Set black hook language version to python3 * chore: version bump (#64) * Black reformat Co-authored-by: Kaidi He <[email protected]> Co-authored-by: Wing Fung Lau <[email protected]> Co-authored-by: Kaidi He <[email protected]>
1 parent a9efafe commit 563583d

File tree

14 files changed

+339
-8
lines changed

14 files changed

+339
-8
lines changed

docs/cloudformation_compatibility.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ StartingPosition All
108108
BatchSize All
109109
======================== ================================== ========================
110110

111+
MQ
112+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113+
======================== ================================== ========================
114+
Property Name Intrinsic(s) Supported Reasons
115+
======================== ================================== ========================
116+
Broker All
117+
Queues All
118+
SourceAccessConfigurations All
119+
======================== ================================== ========================
120+
111121
MSK
112122
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113123
======================== ================================== ========================

docs/internals/generated_resources.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,36 @@ AWS::Lambda::Permission MyFunction\ **MyTrigger**\ Permission
326326
AWS::Lambda::EventSourceMapping MyFunction\ **MyTrigger**
327327
================================== ================================
328328

329+
MQ
330+
^^^^^^^
331+
332+
Example:
333+
334+
.. code:: yaml
335+
336+
MyFunction:
337+
Type: AWS::Serverless::Function
338+
Properties:
339+
...
340+
Events:
341+
MyTrigger:
342+
Type: MQ
343+
Properties:
344+
Broker: arn:aws:mq:us-east-2:123456789012:broker:MyBroker:b-1234a5b6-78cd-901e-2fgh-3i45j6k178l9
345+
SourceAccessConfigurations:
346+
Type: BASIC_AUTH
347+
URI: arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c
348+
...
349+
350+
Additional generated resources:
351+
352+
================================== ================================
353+
CloudFormation Resource Type Logical ID
354+
================================== ================================
355+
AWS::Lambda::Permission MyFunction\ **MyTrigger**\ Permission
356+
AWS::Lambda::EventSourceMapping MyFunction\ **MyTrigger**
357+
================================== ================================
358+
329359
MSK
330360
^^^^^^^
331361

samtranslator/model/eventsources/pull.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class PullEventSource(ResourceMacro):
1111
"""Base class for pull event sources for SAM Functions.
1212
13-
The pull events are Kinesis Streams, DynamoDB Streams, Kafka Streams and SQS Queues. All of these correspond to an
13+
The pull events are Kinesis Streams, DynamoDB Streams, Kafka Topics, ActiveMQ Queues and SQS Queues. All of these correspond to an
1414
EventSourceMapping in Lambda, and require that the execution role be given to Kinesis Streams, DynamoDB
1515
Streams, or SQS Queues, respectively.
1616
@@ -31,6 +31,9 @@ class PullEventSource(ResourceMacro):
3131
"DestinationConfig": PropertyType(False, is_type(dict)),
3232
"ParallelizationFactor": PropertyType(False, is_type(int)),
3333
"Topics": PropertyType(False, is_type(list)),
34+
"Broker": PropertyType(False, is_str()),
35+
"Queues": PropertyType(False, is_type(list)),
36+
"SourceAccessConfigurations": PropertyType(False, is_type(list)),
3437
}
3538

3639
def get_policy_arn(self):
@@ -60,16 +63,17 @@ def to_cloudformation(self, **kwargs):
6063
except NotImplementedError:
6164
function_name_or_arn = function.get_runtime_attr("arn")
6265

63-
if not self.Stream and not self.Queue:
66+
if not self.Stream and not self.Queue and not self.Broker:
6467
raise InvalidEventException(
65-
self.relative_id, "No Queue (for SQS) or Stream (for Kinesis, DynamoDB or MSK) provided."
68+
self.relative_id,
69+
"No Queue (for SQS) or Stream (for Kinesis, DynamoDB or MSK) or Broker (for ActiveMQ) provided.",
6670
)
6771

6872
if self.Stream and not self.StartingPosition:
6973
raise InvalidEventException(self.relative_id, "StartingPosition is required for Kinesis, DynamoDB and MSK.")
7074

7175
lambda_eventsourcemapping.FunctionName = function_name_or_arn
72-
lambda_eventsourcemapping.EventSourceArn = self.Stream or self.Queue
76+
lambda_eventsourcemapping.EventSourceArn = self.Stream or self.Queue or self.Broker
7377
lambda_eventsourcemapping.StartingPosition = self.StartingPosition
7478
lambda_eventsourcemapping.BatchSize = self.BatchSize
7579
lambda_eventsourcemapping.Enabled = self.Enabled
@@ -79,6 +83,8 @@ def to_cloudformation(self, **kwargs):
7983
lambda_eventsourcemapping.MaximumRecordAgeInSeconds = self.MaximumRecordAgeInSeconds
8084
lambda_eventsourcemapping.ParallelizationFactor = self.ParallelizationFactor
8185
lambda_eventsourcemapping.Topics = self.Topics
86+
lambda_eventsourcemapping.Queues = self.Queues
87+
lambda_eventsourcemapping.SourceAccessConfigurations = self.SourceAccessConfigurations
8288

8389
destination_config_policy = None
8490
if self.DestinationConfig:
@@ -170,3 +176,12 @@ class MSK(PullEventSource):
170176

171177
def get_policy_arn(self):
172178
return ArnGenerator.generate_aws_managed_policy_arn("service-role/AWSLambdaMSKExecutionRole")
179+
180+
181+
class MQ(PullEventSource):
182+
"""MQ event source."""
183+
184+
resource_type = "MQ"
185+
186+
def get_policy_arn(self):
187+
return ArnGenerator.generate_aws_managed_policy_arn("service-role/AWSLambdaAMQExecutionRole")

samtranslator/model/lambda_.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class LambdaEventSourceMapping(Resource):
7070
"ParallelizationFactor": PropertyType(False, is_type(int)),
7171
"StartingPosition": PropertyType(False, is_str()),
7272
"Topics": PropertyType(False, is_type(list)),
73+
"Queues": PropertyType(False, is_type(list)),
74+
"SourceAccessConfigurations": PropertyType(False, is_type(list)),
7375
}
7476

7577
runtime_attrs = {"name": lambda self: ref(self.logical_id)}

samtranslator/validator/sam_schema/schema.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,9 @@
477477
{
478478
"$ref": "#/definitions/AWS::Serverless::Function.MSKEvent"
479479
},
480+
{
481+
"$ref": "#/definitions/AWS::Serverless::Function.MQEvent"
482+
},
480483
{
481484
"$ref": "#/definitions/AWS::Serverless::Function.SQSEvent"
482485
},
@@ -609,6 +612,26 @@
609612
],
610613
"type": "object"
611614
},
615+
"AWS::Serverless::Function.MQEvent": {
616+
"additionalProperties": false,
617+
"properties": {
618+
"Broker": {
619+
"type": "string"
620+
},
621+
"Queues": {
622+
"type": "array"
623+
},
624+
"SourceAccessConfigurations": {
625+
"type": "array"
626+
}
627+
},
628+
"required": [
629+
"Broker",
630+
"Queues",
631+
"SourceAccessConfigurations"
632+
],
633+
"type": "object"
634+
},
612635

613636
"AWS::Serverless::Function.SQSEvent": {
614637
"additionalProperties": false,

tests/translator/input/amq.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Resources:
2+
MQFunction:
3+
Type: 'AWS::Serverless::Function'
4+
Properties:
5+
CodeUri: s3://sam-demo-bucket/queues.zip
6+
Handler: queue.mq_handler
7+
Runtime: python2.7
8+
Events:
9+
MyMQQueue:
10+
Type: MQ
11+
Properties:
12+
Broker: arn:aws:mq:us-east-2:123456789012:broker:MyBroker:b-1234a5b6-78cd-901e-2fgh-3i45j6k178l9
13+
Queues:
14+
- "Queue1"
15+
SourceAccessConfigurations:
16+
- Type: BASIC_AUTH
17+
URI: arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Resources:
2+
MQFunction:
3+
Type: 'AWS::Serverless::Function'
4+
Properties:
5+
CodeUri: s3://sam-demo-bucket/queues.zip
6+
Handler: queue.mq_handler
7+
Runtime: python2.7
8+
Events:
9+
MyMQQueue:
10+
Type: MQ
11+
Properties:
12+
Queues:
13+
- "Queue1"
14+
SourceAccessConfigurations:
15+
- Type: BASIC_AUTH
16+
URI: arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c

tests/translator/output/amq.json

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"Resources": {
3+
"MQFunction": {
4+
"Type": "AWS::Lambda::Function",
5+
"Properties": {
6+
"Code": {
7+
"S3Bucket": "sam-demo-bucket",
8+
"S3Key": "queues.zip"
9+
},
10+
"Handler": "queue.mq_handler",
11+
"Role": {
12+
"Fn::GetAtt": [
13+
"MQFunctionRole",
14+
"Arn"
15+
]
16+
},
17+
"Runtime": "python2.7",
18+
"Tags": [{
19+
"Value": "SAM",
20+
"Key": "lambda:createdBy"
21+
}]
22+
}
23+
},
24+
"MQFunctionMyMQQueue": {
25+
"Type": "AWS::Lambda::EventSourceMapping",
26+
"Properties": {
27+
"EventSourceArn": "arn:aws:mq:us-east-2:123456789012:broker:MyBroker:b-1234a5b6-78cd-901e-2fgh-3i45j6k178l9",
28+
"FunctionName": {
29+
"Ref": "MQFunction"
30+
},
31+
"Queues": ["Queue1"],
32+
"SourceAccessConfigurations": [
33+
{
34+
"Type": "BASIC_AUTH",
35+
"URI": "arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c"
36+
}
37+
]
38+
}
39+
},
40+
"MQFunctionRole": {
41+
"Type": "AWS::IAM::Role",
42+
"Properties": {
43+
"ManagedPolicyArns": [
44+
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
45+
"arn:aws:iam::aws:policy/service-role/AWSLambdaAMQExecutionRole"
46+
],
47+
"Tags": [
48+
{
49+
"Value": "SAM",
50+
"Key": "lambda:createdBy"
51+
}
52+
],
53+
"AssumeRolePolicyDocument": {
54+
"Version": "2012-10-17",
55+
"Statement": [{
56+
"Action": [
57+
"sts:AssumeRole"
58+
],
59+
"Effect": "Allow",
60+
"Principal": {
61+
"Service": [
62+
"lambda.amazonaws.com"
63+
]
64+
}
65+
}]
66+
}
67+
}
68+
}
69+
}
70+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"Resources": {
3+
"MQFunction": {
4+
"Type": "AWS::Lambda::Function",
5+
"Properties": {
6+
"Code": {
7+
"S3Bucket": "sam-demo-bucket",
8+
"S3Key": "queues.zip"
9+
},
10+
"Handler": "queue.mq_handler",
11+
"Role": {
12+
"Fn::GetAtt": [
13+
"MQFunctionRole",
14+
"Arn"
15+
]
16+
},
17+
"Runtime": "python2.7",
18+
"Tags": [{
19+
"Value": "SAM",
20+
"Key": "lambda:createdBy"
21+
}]
22+
}
23+
},
24+
"MQFunctionMyMQQueue": {
25+
"Type": "AWS::Lambda::EventSourceMapping",
26+
"Properties": {
27+
"EventSourceArn": "arn:aws:mq:us-east-2:123456789012:broker:MyBroker:b-1234a5b6-78cd-901e-2fgh-3i45j6k178l9",
28+
"FunctionName": {
29+
"Ref": "MQFunction"
30+
},
31+
"Queues": ["Queue1"],
32+
"SourceAccessConfigurations": [
33+
{
34+
"Type": "BASIC_AUTH",
35+
"URI": "arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c"
36+
}
37+
]
38+
}
39+
},
40+
"MQFunctionRole": {
41+
"Type": "AWS::IAM::Role",
42+
"Properties": {
43+
"ManagedPolicyArns": [
44+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
45+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaAMQExecutionRole"
46+
],
47+
"Tags": [
48+
{
49+
"Value": "SAM",
50+
"Key": "lambda:createdBy"
51+
}
52+
],
53+
"AssumeRolePolicyDocument": {
54+
"Version": "2012-10-17",
55+
"Statement": [{
56+
"Action": [
57+
"sts:AssumeRole"
58+
],
59+
"Effect": "Allow",
60+
"Principal": {
61+
"Service": [
62+
"lambda.amazonaws.com"
63+
]
64+
}
65+
}]
66+
}
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)