How to Solve ‘Missing Authentication Token Error’ with API Gateway Custom Domain

After setting up everything correctly, you may have ‘Missing Authentication Token Error’ when you call the custom domain while the endpoint from API gateway works.

Surprisingly, this is one of the most common errors I have seen, yet not very well documented. So, here it is. When you encounter this error, check out the suggestion here.

The prerequisite is to have your lambda function working through the endpoint from API gateway. You also need to have the correct domain name and certificate set up. Setting up a lambda function, API gateway or domain name with a certificate is not the scope of this post.

The root cause for this is not what the error message says. It is nothing to do with authentication token. It is to do with the incorrect endpoint.

The raw API endpoint from API gateway contains the url, stage name and resource path as below.

https://f84jseleds.execute-api.us-east-2.amazonaws.com/dev/myapi

If your custom domain name is myapi.custom.domain.com with the base path as helloworld, your end point will be the base url, helloworld and the resource path as myapi.

https:// myapi.custom.domain.com/helloworld/myapi

The most common mistake I find is that setting the same path as the resource path in the Custom Domain Name and try to call the end point as below. Obviously, it does not work.

https:// myapi.custom.domain.com/myapi

If you have the same path in the base path mapping as the resource path, you can call it as below. But, obviously this is not a pretty endpoint. Ideally, you want to have different names so that the endpoint doesn’t look repetitive.

https:// myapi.custom.domain.com/myapi/myapi

If you are using the serverless framework, make sure to configure these parameters correctly.

The resource path comes under events in the function.

functions:
  postApiTest:
    handler: handler.check
    name: post-api-test
    events:
      - http:
          path: check
          method: post
          private: true

The base path mapping for the custom domain comes under the customDomain property in custom

custom:
  customDomain:
    domainName: myapi.custom.domain.com
    stage: dev
    basePath: helloworld
    certificateName: myapi.custom.domain.com
    createRoute53Record: true

You can check out this blog for setting up Custom Domain with serverless. It is not really clear on the difference between resource path and base path mapping, though. It is also a good practice with setting the basePath parameter. In fact, if you leave it empty, serverless doesn’t work.

Now you can call your endpoint and it should work!

I hope this saves you a bit of time trouble shooting your API gateway. If you have any other problems with setting up API gateway with lambda functions, let me know. The chances are I have seen it before and know how to solve it!

Git
How to specify which Node version to use in Github Actions

When you want to specify which Node version to use in your Github Actions, you can use actions/setup-node@v2. The alternative way is to use a node container. When you try to use a publicly available node container like runs-on: node:alpine-xx, the pipeline gets stuck in a queue. runs-on is not …

AWS
Using semantic-release with AWS CodePipeline and CodeBuild

Here is the usual pattern of getting the source from a git repository in AWS CodePipeline. In the pipeline, we use AWS CodeStart to connect to a repo and get the source. Then, we pass it to the other stages, like deploy or publish. For some unknown reasons, CodePipeline downloads …

DBA
mysqldump Error: Unknown table ‘COLUMN_STATISTICS’ in information_schema (1109)

mysqldump 8 enabled a new flag called columm-statistics by default. When you have MySQL client above 8 and try to run mysqldump on older MySQL versions, you will get the error below. mysqldump: Couldn’t execute ‘SELECT COLUMN_NAME, JSON_EXTRACT(HISTOGRAM ‘$”number-of-buckets-specified”‘) FROM information_schema.COLUMN_STATISTICS WHERE SCHEMA_NAME = ‘myschema’ AND TABLE_NAME = ‘craue_config_setting’;’: Unknown …