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..!
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.