Stage name in api gateway using AWS CDK

AWS Serverless developer
When creating a lambda rest API using CDK, by default it creates a stage named prod
import * as cdk from "aws-cdk-lib";
import * as apigw from "aws-cdk-lib/aws-apigateway";
export class ApiGatewayDemo extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// API Gateway REST API
const api = new apigw.LambdaRestApi(this, "Endpoint", {
proxy: false,
});
}
API URL looks like `https://api-id.execute-api.region.amazonaws.com/prod/`
Creating an API with the stage name dev
To set the stage name in AWS Lambda REST API using CDK, use the stageName property.
import * as cdk from "aws-cdk-lib";
import * as apigw from "aws-cdk-lib/aws-apigateway";
export class ApiGatewayDemo extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// API Gateway REST API
const api = new apigw.LambdaRestApi(this, "Endpoint", {
proxy: false,
deployOptions: {
stageName: "dev",
},
});
}
}
Now API URL will look like `https://api-id.execute-api.region.amazonaws.com/dev/`
The above code defines a class called
ApiGatewayDemowhich extends theStackclass from theaws-cdk-liblibrary. TheStackclass represents a cloud resource stack in the AWS CloudFormation service.The
ApiGatewayDemoclass has a single constructor function, which takes three arguments:
scope: an instance of theAppclass from theaws-cdk-liblibrary. TheAppclass represents a CDK app, which is a collection of stacks and assets that can be deployed together.
id: a string that uniquely identifies the stack within the CDK app.
props: optional properties for the stack.Inside the constructor function, the code first calls the
superfunction to call the baseStackclass's constructor function with thescope,id, andpropsarguments. This creates an instance of theStackclass.Next, the code creates an instance of the
LambdaRestApiclass from theaws-apigatewaylibrary. This class represents an AWS Lambda REST API in a CDK app. TheLambdaRestApiclass takes two arguments:
this: the currentApiGatewayDemoinstance, which is passed as the first argument to all methods in the class.
"Endpoint": a string that is used as the identifier for the REST API.The
LambdaRestApiclass has a single property,proxy, which is set tofalsein this example. This means that the REST API will not act as a proxy for another resource or service.The
LambdaRestApiclass also has adeployOptionsproperty, which is an object with astageNameproperty. ThestageNameproperty is set to"dev"in this example.This sets the stage name of the REST API to
"dev". The stage name is used to identify a specific deployment of the API, and it is used as a suffix in the URL of the API (e.g.https://api-id.execute-api.region.amazonaws.com/dev).
We can have multiple stages of the same API, each with its own stage name and separate URL


