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 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则