Customise Aws API Gateway Name in Serverless Framework

serverless framework

When using serverless framework to deploy aws api gateway, by default the api name will be stage-servicename, like sit-test-service or dev-test-service.

If you want to customise the api name, you can do it through resources configuration.

1
2
3
4
5
6
7
8
serverless.yml

resources:
Resources:
ApiGatewayRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: "yourServiceName"

I use a seperate resource file, it looks like this

1
2
3
4
serverless.yml

resources:
Resources: ${file(resources.yml)}
1
2
3
4
5
6
resources.yml

ApiGatewayRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: "yourServiceName"
  • When you deploy again after adding custom api name, serverless will call aws cloudformation update-stack based on the cloudformation json template. It won’t create a new api, just rename it by updating the existing api. The existing api id won’t change, so the existing cloudfront or custom dns won’t be affected.

CLI

if you want to do this via command line,

  • first list APIS and IDs under your account:
1
aws apigateway get-rest-apis
  • Use the Id to update name of API
1
aws apigateway update-rest-api --rest-api-id IDOfTheAPIThatNeedsTobeUpdated --patch-operations op=replace,path=/name,value=NewName