-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: support for lambda event destinations #1321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #1321 +/- ##
===========================================
- Coverage 94.48% 94.41% -0.08%
===========================================
Files 78 78
Lines 4388 4529 +141
Branches 871 903 +32
===========================================
+ Hits 4146 4276 +130
- Misses 116 119 +3
- Partials 126 134 +8
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diving into the main section next, also going to run some additional tests with conditions.
Something that seems to be missing with conditions is applying a condition to a Function at the resource level when this function (a) references an event bus and other resources and (b) when this function creates either an SQS or SNS resource and make sure the condition is applied to that resources as well:
MyFunction:
Condition: C1
Type: AWS::Serverless::Function
Properties:
...
samtranslator/model/intrinsics.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just repeating the previous make_conditional
- we could override that one instead like this:
def make_conditional(condition, true_data, false_data={'Ref': 'AWS::NoValue'}):
Keeps the same functionality and doesn't copy code.
(a) references an event bus and other resources and |
Here's what I was talking about. The SQS/SNS resources (if SAM generates them) need to have the condition applied to them that's on the Function. Input: Conditions:
MyCondition:
Fn::Equals:
- true
- true
Parameters:
SNSArn:
Type: String
Default: my-sns-arn
Globals:
Function:
AutoPublishAlias: live
EventInvokeConfig:
MaximumEventAgeInSeconds: 70
MaximumRetryAttempts: 1
DestinationConfig:
OnSuccess:
Type: SQS
OnFailure:
Type: SNS
Destination: !Ref SNSArn
Resources:
MyTestFunction:
Condition: MyCondition
Type: AWS::Serverless::Function
Properties:
InlineCode: |
exports.handler = function(event, context, callback) {
var event_received_at = new Date().toISOString();
console.log('Event received at: ' + event_received_at);
console.log('Received event:', JSON.stringify(event, null, 2));
if (event.Success) {
console.log("Success");
context.callbackWaitsForEmptyEventLoop = false;
callback(null);
} else {
console.log("Failure");
context.callbackWaitsForEmptyEventLoop = false;
callback(new Error("Failure from event, Success = false, I am failing!"), 'Destination Function Error Thrown');
}
};
Handler: index.handler
Runtime: nodejs10.x
MemorySize: 1024 Output: ---
Conditions:
MyCondition:
Fn::Equals:
- true
- true
Resources:
MyTestFunctionEventInvokeConfigOnSuccessQueue: # needs to have the condition applied to it
Type: AWS::SQS::Queue
Properties: {}
MyTestFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: MyTestFunctionEventInvokeConfigSQSPolicy
PolicyDocument:
Statement:
- Action: sqs:SendMessage
Resource:
Fn::GetAtt:
- MyTestFunctionEventInvokeConfigOnSuccessQueue
- Arn
Effect: Allow
- PolicyName: MyTestFunctionEventInvokeConfigSNSPolicy
PolicyDocument:
Statement:
- Action: sns:publish
Resource: my-sns-arn
Effect: Allow
Tags:
- Value: SAM
Key: lambda:createdBy
Condition: MyCondition
MyTestFunctionEventInvokeConfig:
Type: AWS::Lambda::EventInvokeConfig
Properties:
MaximumEventAgeInSeconds: 70
MaximumRetryAttempts: 1
DestinationConfig:
OnSuccess:
Destination:
Fn::GetAtt:
- MyTestFunctionEventInvokeConfigOnSuccessQueue
- Arn
OnFailure:
Destination: my-sns-arn
FunctionName:
Ref: MyTestFunction
Qualifier: live
Condition: MyCondition
MyTestFunction:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: "exports.handler = function(event, context, callback) {\n var
event_received_at = new Date().toISOString();\n console.log('Event received
at: ' + event_received_at);\n console.log('Received event:', JSON.stringify(event,
null, 2));\n if (event.Success) {\n console.log(\"Success\");\n
\ context.callbackWaitsForEmptyEventLoop = false;\n callback(null);\n
\ } else {\n console.log(\"Failure\");\n context.callbackWaitsForEmptyEventLoop
= false;\n callback(new Error(\"Failure from event, Success = false,
I am failing!\"), 'Destination Function Error Thrown');\n }\n}; \n"
Tags:
- Value: SAM
Key: lambda:createdBy
MemorySize: 1024
Handler: index.handler
Role:
Fn::GetAtt:
- MyTestFunctionRole
- Arn
Runtime: nodejs10.x
Condition: MyCondition
MyTestFunctionAliaslive:
Type: AWS::Lambda::Alias
Properties:
FunctionVersion:
Fn::GetAtt:
- MyTestFunctionVersionefc149382f
- Version
FunctionName:
Ref: MyTestFunction
Name: live
Condition: MyCondition
MyTestFunctionVersionefc149382f:
DeletionPolicy: Retain
Type: AWS::Lambda::Version
Properties:
FunctionName:
Ref: MyTestFunction
Condition: MyCondition
Parameters:
SNSArn:
Default: my-sns-arn
Type: String |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So excited about the SNS/SQS auto-generation! This is going to be a powerful feature. 😊
examples/2016-10-31/function_lambda_event_destinations/template.yaml
Outdated
Show resolved
Hide resolved
@jlhood and @keetonian Thanks a ton for the comments, cleaned it all up and its ready for review again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there. Mostly documentation-related feedback, but I think there's one miss in the naming of the inline IAM policy for destination permissions.
tests/translator/input/function_with_event_dest_conditional.yaml
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent work!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, this should fix failing tests.
Issue #, if available:
#1269
Description of changes:
Adding support for Event Destinations and async invocation settings (max retry and event max age). More about the Cloudformation property here.
There are four destination resources supported - SQS, SNS, EventBridge and Lambda Function.
Destination property is required for EventBridge event bus and Lambda function. But if the type is SQS/SNS, and the destination is not specified, SAM will auto generate a SQS Queue/ SNS topic for you. To refer the auto generated resources, you can use FunctionLogicalId.DestinationQueue or FunctionLogicalId.DestinationTopic.
The destination property also supports intrinsics. Special handling for single level nested If has been added.
Description of how you validated changes:
Deployed end to end template in examples/
Updated the stack with flipped condition in the same template.
Checklist:
make pr
passesexamples/2016-10-31
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.