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 Answer
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

answered 2 years ago

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