Quick aws tutorial: Connect Http Api with Custom Domain

How to access a lambda function with your own domain

Posted by Gregor Tätzner


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:

  1. 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!',
    };
};
  1. After you saved your lambda function. Add a new api gateway trigger (click “Add Trigger” in the function view): Add Api Gateway Trigger

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!
  1. 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.

  2. After validation, you should see your activated certificate: AWS certificates 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: AWS domain name details

  3. Almost done: In the domain details add your API mappings. Here you can just select our hello world api.

  4. 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!

pingu clap