Serverless Framework + AWS: automatically creating certificate and domain for your app
This is a guide showing how you can automatically create an SSL certificate on Amazon Certificate Manager (ACM) and configure a subdomain on Route 53 Hosted Zone using Serverless Framework with some wonderful plugins.
Before start:
- You need an AWS account;
- Have a Hosted Zone configured on Route53 with some domain name;
- Have a little bit of knowledge of using Serverless Framework.
Firstly, you need to know that we'll use some Serverless Framework plugins to turn our life easier:
- Serverless Domain Manager to control Route53 rules and API Gateway custom domain configs.
- Serverless Certificate Creator to create certificates on Amazon Certificate Manager and confirm by DNS method.
Note: I’ll show some scripts using NodeJS, but Serverless Framework can be used on almost any language (to not say literally anywhere).
With this in mind, let's go ahead! Start installing dependencies:
npm install serverless-domain-manager serverless-certificate-creator -— save-dev
Create a custom domain section
I usually separate domains to allow customization for each one, it would be like this in my stack:
Of course, the certificate name is chosen by you. I only like to use the same name of the domain to keep easier to locate on ACM.
This is the way how I organize my apps, take this as an example to customize/start.
See our custom
section:
Deploy
Take the following tips and tricks:
Deploying to another stage (not on the default: dev) you need to use the option --stage
(read more here). As an example for the first time you can use:
# First deploy PROD stage serverless create-cert --stage=prodserverless deploy --stage=prod
It’s possible to manually generate a domain using serverless-domain-manager, you can do it using the command serverless create_domain
, this isn’t needed if you have the autoDomain
flag enabled (as mentioned earlier).
Complete stack
See the complete Serverless stack below.