Issue while Importing RestApi Id

0

I am creating API gateway using the first template, and exported the same RestApi Id which i want to use in second template.

first template

AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Application.
Resources:
  AdminApi:
    Type: 'AWS::Serverless::Api'
    Properties:
      StageName: Dev
      Auth:
        AddDefaultAuthorizerToCorsPreflight: false
        DefaultAuthorizer: MyCognitoAuthorizer
        Authorizers:
          MyCognitoAuthorizer:
            UserPoolArn: >-
              arn:aws:cognito-idp:us-east-1:278556492334:userpool/us-east-1_H3XrefWaA
            Identity:
              Header: Authorization
      Cors:
        AllowMethods: '''GET,POST,OPTIONS'''
        AllowHeaders: '''Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'''
        AllowOrigin: '''http://localhost:3000'''
  UserFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      Architectures:
        - x86_64
      Handler: 'User::User.LambdaEntryPoint::FunctionHandlerAsync'
      Role: null
      Policies:
        - AWSLambdaBasicExecutionRole
      Events:
        GetUser:
          Type: Api
          Properties:
            Path: '/Users'
            Method: GET
            RestApiId: !Ref AdminApi
            Auth:
              Authorizer: MyCognitoAuthorizer
              AuthorizationScopes:
                - aws.cognito.signin.user.admin
        AddUser:
          Type: Api
          Properties:
            Path: '/User'
            Method: POST
            RestApiId: !Ref AdminApi
            Auth:
              Authorizer: MyCognitoAuthorizer
              AuthorizationScopes:
                - aws.cognito.signin.user.admin
 
Globals:
  Function:
    Runtime: dotnet6
    CodeUri: ''
    MemorySize: 256
    Timeout: 30
Outputs:
  AdminApiRestApiId:
    Description: AdminAPI gateway
    Value: !Ref AdminApi
    Export:
      Name: AdminApiRestApiId


In this template I am trying to Import RestApi id that we created and exported in first template but i am facing a error " Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [TermsAndConditionFunction] is invalid. Event with id [GetTermsAndConditions] is invalid. Property 'RestApiId' should be a string. Failed to publish AWS Serverless application "

Here is the second template

AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Application.

Resources:
  TermsAndConditionFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      Architectures:
        - x86_64
      Handler: 'TermsAndConditions::TermsAndConditions.LambdaEntryPoint::FunctionHandlerAsync'
      Role: null
      Policies:
        - AWSLambdaBasicExecutionRole
      Events:
        GetTermsAndConditions:
          Type: Api
          Properties:
            Path: '/TermsAndConditions'
            Method: GET
            RestApiId: !ImportValue AdminApiRestApiId
            Auth:
              Authorizer: MyCognitoAuthorizer
              AuthorizationScopes:
                - aws.cognito.signin.user.admin
        AddTermsAndCondition:
          Type: Api
          Properties:
            Path: '/TermsAndConditions'
            Method: POST
            RestApiId: !ImportValue AdminApiRestApiId
            Auth:
              Authorizer: MyCognitoAuthorizer
              AuthorizationScopes:
                - aws.cognito.signin.user.admin
 
Globals:
  Function:
    Runtime: dotnet6
    CodeUri: ''
    MemorySize: 256
    Timeout: 30
Outputs: {}

Any suggestions or Solution would be appreciated. Thanks in advance..!

1 Answer
1
Accepted Answer

Hi,

it is unfortunately not possible to reference an AWS::Serverless::Api resource defined in another template, as stated in the documentation https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-api.html#sam-function-api-restapiid

This cannot reference an AWS::Serverless::Api resource defined in another template.

In your case, you could either merge the templates or create the API Gateway resource and methods manually.

Please upvote/accept this answer if it was helpful

profile pictureAWS
EXPERT
answered a year ago
profile picture
EXPERT
reviewed a year ago
  • Is their any other way to create API gateway and use in different template?

  • The only way I can think of right now would be to leverage CloudFormation directly and not SAM. That would allow you to refer to the API Gateway resource in another stack at the cost of having to create methods and resources on the API Gateway explicitly.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions