How to create an API gateway with multiple stages in AWS Serverless Application Model (SAM)?

0

Hi,

I am using the AWS Serverless Application Model (SAM) to create an API gateway which calls a Lambda function. When I do so, I notice the API gateway is created with two stages by default called "Stage" and "Prod". Below is my template code:

  GetAllOrdersFunction:
	Type: AWS::Serverless::Function
	Properties:
	  CodeUri: orders_api/
	  Handler: orders.get_all_orders
	  Runtime: python3.9
	  Policies:
		- CloudWatchPutMetricPolicy: {}
		- DynamoDBReadPolicy:
			TableName: !Ref OrdersTable
	  Events:
		GetAllOrders:
		  Type: Api
		  Properties:
			Path: /id
			Method: GET

How can I modify my template to customize the number of stages that are created for my API gateway and the names of them? E.g. creating three stages with the names v1, v2, and v3?

Thanks.

1개 답변
0

Hi You have the possibility to pas routesettings https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-stage.html#cfn-apigatewayv2-stage-routesettings

But SAM will merge that if original api has some

For stagenames there is a possibility to create the stages and your api independently instead of relying on sam function resource

By the way creating your own stage will not prevent sam creating default stage and prod stagenames

Resources:
    Myv1Api: 
       Type: AWS::Serverless::Api
       Properties:
           StageName: v1
           OpenApiVersion: 2.0

   Lambdafunction:
       Type: AWS::Serverless::Function
       Events: 
           MyApiv1Event:
               RestApi: !Ref Myv1APi

This way the defaults will be prevented and you create your v1 stage

답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인