antun
antun

Reputation: 2297

Serverless Error: Unable to setup base domain mappings for

I am deploying a Lambda function with Serverless. I did the following:

  1. Got my codebase set-up and running locally (using serverless-offline).
  2. Deployed my Lambda function to the dev stage (serverless deploy -v)and tested it there. Since I deployed to dev first, it took the default endpoint of image.example.com/ogImage (where ogImage is my function).
  3. Deployed my Lambda function successfully to the prod stage. However I then discovered that the custom domain was still pointing to the dev stage.
  4. Went into the AWS console manually, under API Gateway>Custom domain names>image.example.com, clicked on Configure API mappings, and switched it to use the prod API and prod stage (see screenshot below)

That fixed the problem and now the custom domain (image.example.com/ogImage) is pointing to the prod stage.

However when I went to deploy again by running serverless deploy -v --stage prod, I get the error:

  Error: Error: Unable to setup base domain mappings for image.example.com
      at ServerlessCustomDomain.<anonymous> (/Users/me/software/git/zhxword-image/node_modules/serverless-domain-manager/dist/index.js:164:27)
      at Generator.throw (<anonymous>)
      at rejected (/Users/me/software/git/zhxword-image/node_modules/serverless-domain-manager/dist/index.js:6:65)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

I am pretty sure that the manual step 4 is what messed it up. How can I reliably set it up so that the prod stage is special, and always points to the custom domain, and I can publish repeatedly? I am fine with the whatever approach is standard for other stages - e.g. a path like image.example.com/dev/ogImage, or no custom domain.

Here's the serverless.yml

app: zhxw-image                                                            
service: zhxw-image-service                                                
  
plugins:
  - serverless-offline                                                     
  - serverless-domain-manager
    
custom:
  customDomain:
    domainName: image.example.com                                          
    basePath: ''                                                           
    stage: ${self:provider.stage}                                          
    createRoute53Record: true                                              
  serverless-offline:                                                      
    host: '0.0.0.0'                                                        
    httpPort: 4000                                                         
    
provider:
  name: aws
  runtime: nodejs12.x                                                      
  apiGateway:
    binaryMediaTypes:                                                      
      - '*/*'                                                              
      - 'image/png'                                                        
      - 'image/jpeg'                                                       
      - 'image/gif'
      - 'text/html'                                                        
functions:
  ogImage:
    handler: handler.ogImage
    ...

Upvotes: 1

Views: 2731

Answers (2)

Jason Mullings
Jason Mullings

Reputation: 1032

ServerlessCustomDomain requires an endpoint to bind to, try adding an event:

events:
  - http:
      path: /{any+}
      method: any

Upvotes: 0

Sarthak Jain
Sarthak Jain

Reputation: 2096

If you want to use the same domain for dev and prod, you can make use of the basePath attribute. Set it to 'dev' when stage=dev and set it to 'prod' when stage=prod

Following serverless.yml should work for you.

app: zhxw-image                                                            
service: zhxw-image-service                                                
  
plugins:
  - serverless-offline                                                     
  - serverless-domain-manager
    
custom:
  customDomain:
    domainName: image.example.com                                          
    basePath: ${self:provider.stage}  #### Changed here                                                            
    stage: ${self:provider.stage}                                          
    createRoute53Record: true                                              
  serverless-offline:                                                      
    host: '0.0.0.0'                                                        
    httpPort: 4000  
provider:
  name: aws
  runtime: nodejs12.x
  stage: ${opt:stage, 'dev'}   #### Changed here

Running the following will create the endpoint image.example.com/dev/...

serverless deploy -v --stage dev

And this will create the endpoint image.example.com/prod/...

serverless deploy -v --stage prod

Upvotes: 1

Related Questions