This is a quick tutorial series for you serverless folk! We create a AWS lambda function with a api gatway trigger. By default aws gives you these kinda ugly api urls: https://<api_id>.execute-api.<aws_region>.amazonaws.com
. Can we make it better? Of course with custom domains!
To show the complete process, we start with a new lambda function. We are doing it the old-school way without cloudformation templates or other automation to keep it simple:
- In the AWS console search for lambda and create a new nodejs function called hello-world with all default options. Paste this handler code in the index.js file, it should later return the text Api Gateway with Custom Domain!:
exports.handler = async (event) => {
return {
statusCode: 200,
body: 'Api Gateway with Custom Domain!',
};
};
- After you saved your lambda function. Add a new api gateway trigger (click “Add Trigger” in the function view):
Now you should be able to call your method with the default api endpoint url. But lets not stop here!
> curl https://<api_id>.execute-api.<aws_region>.amazonaws.com/hello-world
Api Gateway with Custom Domain!
Since api gateways only work with ssl, we need to configure a certificate. Luckily aws makes this really easy for us and also issues new certificates for free! So if you dont own a certificate for your domain yet, head to the Certificate Manager in the aws console to create a new one. If you want to use the url
api.<your_domain>.<tld>
you should insert this value in the wizard or use a asterisk:*.<your_domain>.<tld>
. In the next step you should select a validation method and follow the instructions.After validation, you should see your activated certificate: In the aws console now go to API Gateway and select in the left menu Custom domain names. Create a new custom domain and type in your domain. You should also select your ACM certificate from the last step. The console should greet you now with your domain details:
Almost done: In the domain details add your API mappings. Here you can just select our hello world api.
Finally you should create a CNAME entry in the DNS settings of your registrar to the API Gateway domain name (in my example
d-crzrplwa20.execute-api.eu-central-1.amazonaws.com
). When the changes are propagated we are done and you can query your function with your custom domain!
> curl https://<your_domain>.<tld>/hello-world
Api Gateway with Custom Domain!