How do I use EC2 instance as an API server? Currently using a C++ lambda

Viewed 506

AWS newbie here.

Currently I am using C++ custom runtime for Lambda (as illustrated in this tutorial https://aws.amazon.com/blogs/compute/introducing-the-c-lambda-runtime/)

I integrated it with the AWS API gateway so when the GET/POST on the API is called, my lambda function like this is called,

static invocation_response my_handler(invocation_request const& req)

The "req.payload" would contain the JSON request I sent. So far so good.

For latency/experiment related reasons (also the fact that lack of in memory caching is an issue), I want to do the same using an EC2 instance. i.e. When the REST API is called with a JSON payload, it forwards the request to the C++ code running on my EC2. In other words, as if my lambda was running on an EC2 instance.

How do I achieve this?

3 Answers

You are looking for a different API Gateway Integration.

For Lambda, this is typically Lambda proxy integration or Lambda non-proxy (custom) integration. [1]

If you want to reach an EC2 instance, deploy an arbitrary C++ application which exposes an HTTP endpoint and use the HTTP proxy integration or the HTTP custom integration. [2]

If you want to protect your EC2 instances and make them accessible from the API Gateway only, you can use a Private Integration. [3]

I would recommend not trying to install Lambda-like environments on EC2, but using docker containers on EC2 instances instead. That is far easier to deploy and to manage. You can manage your C++ instance via Elastic Container Service (ECS) by specifying your EC2 instance as underlying ECS Container Instance. [4]

Using ECS, you can leverage the full capabilities of EC2 (being the underlying EC2 instance's large memory) and ease of deployment (being the easy-to-use container deployment tools). You could use the build-in ALB integration to point your API Gateway at the ECS' cluster ALB endpoint via an HTTP integration. Furthermore, you could upload your C++ code to ECR as a docker image and reference it in an ECS task set.

References

[1] https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-integrations.html
[2] https://docs.aws.amazon.com/apigateway/latest/developerguide/setup-http-integrations.html#api-gateway-set-up-http-proxy-integration-on-proxy-resource
[3] https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-private-integration.html
[4] https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ECS_instances.html

Is it possible to use SQS queues to decouple your application? Lambda can push the message to the SQS queue, which you EC2 instance running C++ code is polling, and once the request is processed, EC2 can push the request in an output queue, to be consumed by lambda to sent the result back to the user.

The closest possible way to run it "as AWS does" would be to configure a Firecracker Micro VM as this is exactly how AWS run LAmbdas/Fargate.

However, the easiest way to try and run it would be by using SAM Local. By doing this it will invoke the Lambda function on a local Docker container.

Alternatively try looking at localstack.

Related