Serverless-application-model: AWS::Serverless::Api Property "RestApiId" does not support If, Map Functions

Created on 27 Feb 2020  路  4Comments  路  Source: aws/serverless-application-model

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:

  1. Expected result is that the given AWS::Serverless::Function would be associated with the given API Gateway returned by the If or FindInMap function.

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.

All 4 comments

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.')

Was this page helpful?
0 / 5 - 0 ratings