Description:
The AWS::Serverless::Api does not support particular functions such as Fn::If and Fn::FindInMap. My use case is that particular AWS::Serverless::Function resources my SAM template may have events coming from different API Gateways depending on a parameter I pass in.
Steps to reproduce the issue:
Given the following template:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Conditions:
IsProd:
Fn::Equals:
- !Ref Environment
- prod
Parameters:
Environment:
Type: String
Default: prod
AllowedValues:
- prod
Description: >
sam-app
Sample SAM Template for sam-app
Globals:
Function:
Timeout: 3
Resources:
PublicAWSApiGateway:
Type: AWS::Serverless::Api
Properties:
Name: Public AWS Api Gateway
StageName: Staging
PrivateAWSApiGateway:
Type: AWS::Serverless::Api
Properties:
Name: Private AWS Api Gateway
StageName: Staging
HelloWorldFunctionPrivate:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.8
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
RestApiId: !If [ IsProd, !Ref PublicAWSApiGateway, !Ref PrivateAWSApiGateway ]
Outputs: {}
Run the command:
sam validate --debug
Observed result:
The following result is returned:
Error: [InvalidResourceException('HelloWorldFunctionPrivate', 'Event with id [HelloWorld] is invalid. Api Event must reference an Api in the same template.'), InvalidResourceException('HelloWorldFunctionPublic', 'Event with id [HelloWorld] is invalid. Api Event must reference an Api in the same template.')] ('HelloWorldFunctionPrivate', 'Event with id [HelloWorld] is invalid. Api Event must reference an Api in the same template.') ('HelloWorldFunctionPublic', 'Event with id [HelloWorld] is invalid. Api Event must reference an Api in the same template.')
Expected result:
AWS::Serverless::Function would be associated with the given API Gateway returned by the If or FindInMap function.Thanks for the feature request! We have passed this along to our product team for possible prioritization. Please +1 on the feature to help with prioritization.
I was going crazy with this error until found this issue... Thanks for looking into it!
I have the same issue.
RestApiId property of Api event must reference a valid resource in the same template.
Any update on this issue?
I am unable to import an exported RestApi from a CFN template to a SAM template (which is CFN under the hood)
Stack A - excerpt
Outputs:
SandboxApiOutput:
Description: Description of value to be outputted
Value: !Ref booksapiE1885304
Export:
Name: "BookAPI-Reference-for-lambda-function"
Stack B - excerpt
MyLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: I-DID-IT-MyLambda
Handler: index.Handler
# CodeUri:
Events:
MyLambda:
Type: Api
Properties:
Path: /example
Method: post
RestApiId:
Fn::ImportValue:
"BookAPI-Reference-for-lambda-function"
Error Message ->
[InvalidResourceException('MyLambda', 'Event with id [MyLambda] is invalid. Api Event must reference an Api in the same template.')] ('MyLambda', 'Event with id [MyLambda] is invalid. Api Event must reference an Api in the same template.')
Most helpful comment
Thanks for the feature request! We have passed this along to our product team for possible prioritization. Please +1 on the feature to help with prioritization.