Skip to main content

How to Create Custom Domain for your API using Serverless and AWS.

00:01:45:29

This guide provides step-by-step instructions on how to automate the process of creating an SSL certificate using Amazon Certificate Manager (ACM) and configuring a subdomain using Serverless Domain Manager and using Route 53 Hosted Zone.

Pre Requisites:

  • You need an AWS account.
  • Have a little bit of knowledge of using Serverless Frameworks.

Firstly, you need to know that we’ll use some Serverless Framework plugins to turn our life easier:

Install the Required Plugins

bash
npm install --save-dev serverless-domain-manager  serverless-certificate-creator serverless-hosted-zone

Serverless Template

plugins:
  - serverless-domain-manager
  - serverless-certificate-creator
  - serverless-hosted-zone
custom:
  enabled:
    dev: false
    production: true
  customDomain:
    domainName: ${env:API_DOMAIN}
    basePath: ""
    certificateName: mohammadhariszia.live
    createRoute53Record: true
    autoDomain: true
    enabled: ${self:custom.enabled.${opt:stage, self:provider.stage}}
  customCertificate:
    certificateName: mohammadhariszia.live
    hostedZoneNames: mohammadhariszia.live.
    rewriteRecords: true
    enabled: ${self:custom.enabled.${opt:stage, self:provider.stage}}
  hostedZone:
    name: mohammadhariszia.live.

Note: You can see that the template has been parameterized for running the custom domain and certificate for only the production environment.

Make sure to replace Certificate Name and add required variables in env.

Create Zone

serverless create-zone --stage production
Image showing command output

Create Certificate

serverless create-cert --stage production

Important!

Open your Route53 and get the required NameServers of aws and add them to your domain manager. Secondly, check for the CNAME DNS record in your Route53 and add that to your domain manager as well.

Wait for 2–5 mins for AWS to verify your certificate.

Create Domain

serverless create_domain --stage production

Deploy

serverless deploy --stage production

And Voila! You have done it!


Originally published on Medium.