AWS API Gateway Setup
April 18, 2020
This is less for everyone else, but more for me to write down how I’ve set this up.
I’ve recently setup an API Gateway at https://api.andrewnorman.uk where I can host random APIs. The API Gateway Web UI makes it pretty easy. After setting up route 53 (not going to cover that here), and setting up a certificate (also won’t cover that here) you can add a custom domain name for your API gateway by clicking ‘Custom domain name’ and going through the steps. Once done, you’ll need to add API mappings. These map from your domain to an API Gateway app. A gateway app can either be setup on the root path, or on a sub path. I’ve set mine up on the root path, and I’ll deploy the entire application each time. But each immediate sub path I can setup with a different backing lambda. For instance, I’ve setup a health check as a separate lambda. The ‘/health’ endpoint is mapped in my project to that lambda.
To setup the project, I used the SAM CLI.
Just run sam init
to setup a sample project. To deploy the demo app, run
sam build
, and sam deploy --guided
.
Running multiple lambdas can be setup in the template.yaml
file. Set them up
under the Resources
key.
Resources:
HeadlinePuzzleEncryptor:
Type: AWS::Serverless::Function
Properties:
CodeUri: HeadlinePuzzleEncryptor
Handler: uk.andrewnorman.headlinepuzzle.App::handleRequest
Runtime: java8
MemorySize: 512
Events:
HeadlinePuzzleEncrypt:
Type: Api
Properties:
Path: /headline-puzzle/encrypt
Method: post
SomeOtherThing:
...
Then just create a project folder in the same path as the template file that
matches your resource name (so HeadlinePuzzleEncryptor
in this example). Throw
in the appropriate build files (I use gradle), and setup a handler to handle the
request.
I assume that it’s possible to run different runtimes all in the same API gateway.
But I haven’t tested this yet. In the above, the URL this will map to is
api.andrewnorman.uk/headline-puzzle/encrypt
. If you were to map the gateway API
to a path, that would sit inbetween andrewnorman.uk
and /headline-puzzle
.
As you can see, I’ve setup an encryptor API for the headline puzzle I was talking about the other week. I’ll add a blog page on that soon.