This article modifies the AWS Go CDK "httpapi-lambda" example, found here. This example creates an HTTPs API with a simple Lambda function from a Go script.

The example above was modified with the following with the following: 1) to use an ECR Lambda container instead of a Lambda function 2) to create a REST API with API gateway 3) to allow POST requests 4) to configure method options, integration requests, and a CORS enpoint

Background Link to heading

The following provides background to various services as they relate to this article and the Infrastructure as Code (IaC) templates, i.e., CloudFormation and the GO CDK.

Lambda Link to heading

AWS Lambda is a serverless computing service that can be used to deploy HTTP-based APIs. However, using external libraries requires additional steps. In this post, we are using an Docker image to build the required libraries inside of the Lambda function.

ECR Link to heading

Amazon Elastic Container Registry (ECR) is a fully managed container registry service that makes it easy to store, manage, and deploy Docker container images. By using ECR to host a Lambda function in a container, you have more control over the runtime environment and dependencies.

API Gateway Link to heading

API Gateway acts as a front-end for your Lambda functions, providing a secure and scalable way to expose your functions as HTTP(S) APIs. API Gateway allows you to define various HTTP methods (GET, POST, PUT, DELETE, etc.) for different resource paths. You map these methods to specific Lambda functions to handle requests.

CloudFormation Stack Link to heading

Architecture

This CloudFormation template defines a stack that includes an ECR hosted Lambda function, and an API Gateway endpoint with the necessary configurations for integration to enable POST requests.

Here are a couple of notes about the script above: 1) instead of creating a Lambda directly, an ECR image is created from the Docker image's source directory 2) I wasn't able to use awscdkapigatewayv2alpha/v2 to link the Lambda to API Gateway for POST requests with non-root paths, i.e., "/". Instead, I used awscdk/v2/awsapigateway to create a REST API and a resource, i.e., "/correlation" 3) I'm not sure why, but I also need to configure in integration and methods option to make the API callable from my local machine. This doesn't happen when making this connection in the console. Also, I needed to specify the structure of the response model to be "application/json": awsapigateway.Model_EMPTY_MODEL() for POST requests.