Repository navigation
Description
Update: This top-level comment contains the finalized spec based on feedback from the community. Next step is implementation. We're looking for contributors who can help implement this feature. Thanks!
Description:
Per #349 (comment), creating a separate RFC for a SAM syntax that would allow functions provided by nested applications (AWS::Serverless::Application) to be attached to event triggers via the same Events syntax as AWS::Serverless::Function resources.
High-Level Approach: Add a new SAM resource type: AWS::Serverless::FunctionReference. It doesn't create a new AWS::Lambda::Function resource. Instead, it allows you to specify the name and ARN of an existing function and then attach event sources to it using the exact same Events syntax as AWS::Serverless::Function.
The nice thing about this approach is it's not strictly tied to nested apps. It could be used with Fn::ImportValue for example, if you want to add triggers to a function created in a separate stack in your account.
However, we will offer a simplified syntax for the nested application use case specifically.
Example 1: Nested stack with function reference
# Example native CloudFormation nested stack that creates a function and outputs the function's name as template output variable NestedFunctionName
NestedStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: <url>
NestedFunctionExample:
Type: AWS::Serverless::FunctionReference
Properties:
FunctionName: !GetAtt NestedStack.Outputs.NestedFunctionName
Events:
GetRoot:
Type: Api
Properties:
Path: /
Method: GETExample 2: Nested application with function reference
# Example nested SAR application that creates a function and outputs the function's name as template output variable NestedFunctionName
NestedApp:
Type: AWS::Serverless::Application
Properties:
Location:
ApplicationId: <app id>
SemanticVersion: 1.0.0
NestedFunctionExample:
Type: AWS::Serverless::FunctionReference
Properties:
FunctionName: !GetAtt NestedApp.Outputs.NestedFunctionName
Events:
GetRoot:
Type: Api
Properties:
Path: /
Method: GETExample 3: More compact syntax for nested application use case
# Example nested SAR application that creates a function and outputs the function's name as template output variable NestedFunctionName
NestedApp:
Type: AWS::Serverless::Application
Properties:
Location:
ApplicationId: <app id>
SemanticVersion: 1.0.0
Events:
NestedFunctionName: # This tells SAM to use !GetAtt NestedApp.Outputs.NestedFunctionName whenever we need the function name
- Type: SQS
Properties:
Queue: !GetAtt MyQueue.Arn
BatchSize: 10
- Type: Api
Properties:
Path: /
Method: GETExample 4: Attaching events to function whose name is exported by a different stack
# Assumes other stack exports function name as ExportedFunctionName
ImportValueExample:
Type: AWS::Serverless::FunctionReference
Properties:
FunctionName: !ImportValue ExportedFunctionName
Events:
SQSTrigger:
Type: SQS
Properties:
Queue: !GetAtt MyQueue.Arn
BatchSize: 10Notes/Questions:
- Does SAM need both name and ARN specified or is one or the other sufficient? Might be event-specific... I know API events + implicit swagger require the function name and not ARN and AWS::Lambda::Permission can take name, ARN or alias ARN so it might be safer to require both? Alternatively, we could try to parse the ARN for the function name, but that's error-prone and also the ARN could be an intrinsic function dict and not the actual value itself.
- Decided to go with just FunctionName and construct the ARN assuming the function is in the same account.
- Implementation needs to support Conditions.
Activity
[-]RFC: Support Events on functions within AWS::Serverless::Application[/-][+]RFC: Support adding Events to existing functions[/+][-]RFC: Support adding Events to existing functions[/-][+]RFC: Support adding events to existing functions[/+]sgates commentedon Mar 22, 2019
I believe this would solve the major issue we're currently having, that is, sharing a single API Gateway across multiple AWS::Serverless::Application's. This would allow us to define groups of lambdas and endpoints in a modular way (in a sub stack) and then bring them all together in a unified API front end. This would be a huge win for those experiencing the pain of 200 resource limit, max policy size limits, or juggling multiple gateways for a "single" API.
mikebroberts commentedon Mar 22, 2019
Definitely useful. I think this is a good first pass. Vague things on my mind:
jlhood commentedon Mar 25, 2019
Thanks for the feedback, @mikebroberts!
I think we can simplify it to only require FunctionName and then construct the ARN if necessary assuming the function is owned in the same account (see below for why cross-account wouldn't be supported).
Good question. In cases where I've seen traffic shifting used, you end up doing something like
!Sub ${MyLambda}:livewherever you need to specify a function name and it all "just works." So I think you could do the same forAWS::Serverless::FunctionReferencespecifying the alias for the function name and SAM should do the right thing, but I might be missing something. Is there a specific use case you can think of where this could be an issue?This is an interesting use case. After playing around with this, I don't see a way SAM can help in the cross-account case, because when you use SAM to add an event trigger to a function, it creates an
AWS::Lambda::Permissionresource in the template to add permissions allowing the relevant service principal to invoke the function. Lambda doesn't allow you to add a permission to a Lambda function owned by a different account so this wouldn't deploy.jlhood commentedon Mar 26, 2019
We were discussing this today and while we agreed
AWS::Serverless::FunctionReferenceis a more general solution (works withAWS::Serverless::Application,AWS::CloudFormation::StackandFn::ImportValue), we should try to provide an even more concise experience for the specific use case of adding triggers to functions defined within anAWS::Serverless::Applicationresource. We came up with this syntax:Concerns:
Wanted to put this out to the community for feedback. @sgates @mikebroberts Any strong objections or feedback?
sgates commentedon Mar 26, 2019
I think this makes sense - In this scenario, the API Gateway still lives in the main template, and then groups of lambdas (the NestedApps) would effectively be given access to it through the FunctionReferences in the main template? If I'm understanding that correctly I think that would be fine.
Would this mean in the nested stack, the lambdas defined there don't need Event properties because those "trickle down" from the main template?
jlhood commentedon Mar 26, 2019
Thanks for the quick response, @sgates! In general, this is more a developer experience/syntax discussion, not changing the proposed functionality described in the original
AWS::Serverless::FunctionReferenceRFC. Just adding extra convenience for the nested app use case.I'll play around with the syntax. You're right that we might be able to get it even closer to the Events syntax. The only caveat is that we have to map the function name somewhere, although the Events object keys could do that as long as we're willing to accept the risk of my 3rd concern.
The nested stack template would have to include the function name as a template output, but would not have to export the output.
Thanks, that's helpful feedback!
jlhood commentedon Mar 26, 2019
I think this is the closest I can get to looking like the Events property of
AWS::Serverless::Function:I had to turn it into an array of event triggers, because normally SAM uses different keys for different event triggers, but if we're using the key for the function name, we need a way to support multiple event triggers on the same function.
sgates commentedon Mar 27, 2019
This makes sense to me, and the syntax isn't as confusing as the previous version because it explicitly calls out "Events" - more intuitive IMO. Thanks for all the effort on this!
TeddyHandleman commentedon Apr 3, 2019
@jlhood To me this looks like it's going in the right direction. The only thing I see that may be a potential issue is integration with an OpenAPI document via the DefinitionBody parameter in an
AWS::Serverless::Apiresource.For example:
In the OpenAPI document you might have an APIG integration like so:
However, maybe your solution just means a change of
MyFunction.Arn->NestedApp.Outputs.MyFunctionArnTeddyHandleman commentedon Apr 30, 2019
@jlhood any further updates?
jlhood commentedon Jun 25, 2019
@TeddyHandleman You're right that if someone is managing their own swagger definition body, they would need to substitute the nested application output function in the API GW integration as you specified.
21 remaining items
markoradinovic commentedon May 19, 2020
I decided to write my own macro function that will allow me to define shared api gateway in parent stack and then use reference to exported api gateway from separated SAM stacks. You can find my project here https://github.com/4-cube/cf-shared-apigwv2
Currently, only ApiGatewayV2 (HttpApi) is supported and there is no support to specify Auth per event (I plan to add support for this).
pathikrit-repo-personal commentedon Sep 2, 2020
Hey just wanted to know whether this feature is out or not basically our problem is something like this-
We want all our api resource to have the same endpoint (base url if you may) as all of this one web app api routes but the problem is that we hit 200 resource limit on SAM so now we are trying to group and break each feature (lambda, layers, api events related to that feature) in each child template but as far as I see there's no provision to break a api gateway into multiple template resource or make it in parent template and import into any of these child template resources. So if you have any comments in that or have any way outs it would really help me going.
and as always appreciate your efforts.
jeff1evesque commentedon Nov 4, 2020
Working with AWS Support Engineer who pointed me to this issue. Adding a trivial comment in this issue, so I can be updated on this issue. Ultimately, I have a parent APIG created in CloudFormation, then a CodePipeline that "deploys" a SAM template - which I was hoping to deploy a lambda function using the
restApiIdfrom the parent CloudFormation stack. However, these approaches are not compatible (SAM vs CloudFormation api ID's).omenking commentedon Dec 19, 2020
I want to be able to create isolate SAM applications and I want to attach these functions to an existing API Gateway.
The reason is that I have a service where I offer different functionality to different customers, but without this, I'll have to compose a unique CFN per customer, and this not very modular nor maintainable.
I'm reading through this long thread to check where this ticket is at and if its solves this case or another.
Maybe its better related to #349
I found these tickets via this StackOverflow post:
https://stackoverflow.com/questions/55992260/whats-the-correct-way-to-structure-a-large-api-gateway-using-aws-sam-nested-app
omenking commentedon Dec 20, 2020
Okay I'm back and I have my own solution which I posted over here (CFN templates included):
#1335 (comment)
My Solutions: I wanted to attach services to an existing API Gateway, So instead of using implicit events where SAM relies on generating out a Swagger template, I just explicitly defined an
AWS::ApiGateway::Resource,AWS::ApiGateway::MethodandAWS::Lambda::Permissionand in my SAM template and it worked.I can't test via
sam local start-apibut I can still test lambdas viasam local start-lambdawhich is good enough.I don't care about nesting or import values so this solution works well for me.
fiexes aws#866
Jacco commentedon Feb 7, 2021
I am back :-)
Redid the changes from the old PR on fresh develop checkout (it was a while ago)...
Added two examples in the integration tests. One of the integration tests uses an application from the serverlessrepo that I created for this purpose.
I am pretty sure all scenarios described in top of this issue are supported.
If we go ahead with this PR, I can add the needed documentation changes.
Let me know whats missing and how we can move forward.
artiomnist commentedon Aug 13, 2021
Hey everyone!
Any progress on this feature? @Jacco @jlhood
We're looking to move to nested applications to avoid the resource limit and can see this feature adding massive value in terms of sharing an API Gateway and having multiple nested applications with functions reference the API Gateway!
Thank you for all your work!
yohanrw commentedon Aug 30, 2021
@sgates Hello, we are hitting the same problem of the stack resource limit. However in your reply you mentioned you are working with multiple APIs. Then,
dgokcin commentedon Mar 24, 2022
any update on this issue? we really want to bind functions to an existing api gateway.
cortexcompiler commentedon Apr 27, 2022
I agree, I think there are a number of use cases for adding integrations to existing API Gateways from separate stacks. I personally like to group my Lambdas and APIs by domain/subdomain in order to isolate changes in repositories and stacks. To keep a reasonable number of (Edge Optimized) API Gateways it makes sense to share Gateways rather than have one per subdomain. This is in addition to the stack limitations folks have previously mentioned.
We are resorting to using explicit
AWS::ApiGateway::ResourceandAWS::ApiGateway::MethodResources in the separate "Lambda Stack" referencing the exported Gateway and root resource from the "Gateway Stack". We used aAWS::ApiGateway::RestApiwith a root level GET method and MOCK integration, along with a Stage and Deployment. You can export the RestApiId and RootResource Id. SAM does some deploy magic for you, so you do need to get comfortable with the relationship between a Deployment and a Stage. (this is not my favorite solution, but it works)Gateway Stack snippet:
Lambda Stack snippet: