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 réponse
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

répondu il y a 2 ans

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions