Skip to content
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

AWS::Serverless::Api Property "RestApiId" does not support If, Map Functions #1470

Closed
colinbjohnson opened this issue Feb 27, 2020 · 9 comments
Labels
area/intrinsics Ref, If, Sub, GetAtt, ... type/feature

Comments

@colinbjohnson
Copy link

colinbjohnson commented Feb 27, 2020

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.
@ShreyaGangishetty
Copy link

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.

@ayozemr
Copy link

ayozemr commented Aug 11, 2020

I was going crazy with this error until found this issue... Thanks for looking into it!

@sandun-rezg
Copy link

I have the same issue.
RestApiId property of Api event must reference a valid resource in the same template.
Any update on this issue?

@si3mshady
Copy link

si3mshady commented Oct 28, 2020

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

@Kanakala
Copy link

hi @si3mshady Have you figured it out on how to use a restAPI resource over cross templates?

@smohadjer
Copy link

I could setup an api in yaml and reference it in my lambda using RestApiId as shown below:

  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      Name: MyApi
      BinaryMediaTypes:
        - multipart/form-data
  SendFormEmail:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./send-form-email/send-form-email.zip
      FunctionName: send-form-email
      Handler: index.handler
      MemorySize: 128
      Timeout: 60
      Policies:
        - AmazonSESFullAccess
      Environment:
        Variables:
          SOURCE_EMAIL_ADDRESS: '{{resolve:ssm:EMAIL_NOTIFICATION_SOURCE:1}}'
      Events:
        Download:
          Type: Api
          Properties:
            Path: /api/sendForm
            Method: post
            RestApiId: !Ref MyApi

@urko-b
Copy link

urko-b commented Apr 7, 2022

you figured it out on how to use a restAPI resource over cross

Hi any new information about this problem? Is it available to use an api in cross template?

@hoffa
Copy link
Contributor

hoffa commented Oct 17, 2022

You might be able to get this to work by adding AWS::LanguageExtensions to Transform as such:

Transform:
  - AWS::LanguageExtensions
  - AWS::Serverless-2016-10-31

AWS::LanguageExtensions resolves intrinsic functions if the value is known when Transforms are run.

See #2533 for more information.

@hoffa
Copy link
Contributor

hoffa commented Nov 3, 2022

Closing in favor of #2533.

@hoffa hoffa closed this as completed Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/intrinsics Ref, If, Sub, GetAtt, ... type/feature
Projects
None yet
Development

No branches or pull requests