AWS REST API Gateway #1

My journey to a basic achievement: “Hello World”

As a learning stepping-stone for one of my recent projects, I decided to set myself the challenge of getting a simple AWS REST API Gateway up and running. In short, this service was to be implemented using Infrastructure-as-Code (IaC) to create a functional REST API Gateway in AWS that will return a message of “Hello World” on the API’s root public URL.

The simplest REST API integration is called ‘MockIntegration‘. It does not require a separate backend, such as Lambda function or http endpoint, and so I chose to use this whilst getting started.

The chosen IaC language/library was AWS Cloud Development Kit (CDK) for TypeScript.

Along The Way

I learned that it can sometimes be fairly straight-forward to create the items/objects you want within the graphical AWS web console, but it can be more challenging to then replicate what you have built using IaC.

The main example of this was figuring out where to put the actual “Hello World” message in CDK code. I knew where to do this in the AWS console (under the GET method’s integration response) but was struggling to translate this knowledge into code.

In the end it was the aws command line tool that came to my rescue, giving me the responseTemplate syntax that I needed. The following command revealed how to do it (after I had added the integration response in the AWS console first, and re-deployed the API stage so the config was live):

> aws apigateway get-integration-response --rest-api-id <api_id> --resource-id <resource_id> --http-method GET --status-code 200

The Code

Anyhow, the code below assumes you have already run cdk init, to get the basic app/stack structure, and are inserting code into a stack constructor method.

First, I created the main API instance:

readonly api: RestApi

this.api = new RestApi(this, 'MyRestApi', {
   description: 'My REST API Gateway',
   endpointTypes: [aws_apigateway.EndpointType.REGIONAL],
   deployOptions: {
      stageName: 'development',

Next, the root MockIntegration is needed:

this.api.root.addMethod('GET', new MockIntegration({
   integrationResponses: [
         statusCode: '200',
         responseTemplates: {
            "application/json": "#set($inputRoot = $input.path('$'))\n{ message : \"Hello World\" }"
   passthroughBehaviour: PassthroughBehaviour.NEVER,
   requestTemplates: {
      'application/json': '{ "statusCode": 200 }',
}), {
   methodResponses: [
         statusCode: '200',
         responseModels: {
            'application/json': Model.EMPTY_MODEL

The Result

Finally, with the above code blocks in my stack constructor method, I had gathered together a sufficient amount of instruction for AWS to deploy my stack for me:

> npx cdk deploy <stack_name> [--profile <profile_name>]

Once successfully deployed, browsing to the output URL provided the desired “Hello World” message which I had been seeking.

Goal achieved! Overall, as a learner of CDK for TypeScript, I found this to be a useful exercise to understand AWS API Gateway better.

Useful Links

Here are some links that helped me on my journey:

Official AWS CDK Docs