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

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南