[AWS] Connect API Gateway to EC2

[AWS] Connect API Gateway to EC2

In this post, we will talk about how to connect the AWS API gateway to EC2.

Normally, we use the API gateway to make an API when we are using Lambda.

However, for some cases, you might want to use an API gateway as a proxy for the EC2 instance server.

Let’s say we have an EC2 instance running the Flask application that we installed from the last post.

We want to make an API that is pointing the server running in, for example, “http://x.x.x.x:5000/ “.

Now, just follow the screenshots.

1. Go to API Gateway console and Click Build in HTTP API

3. Type a name and click Review and Create

4. Click Create

5. Check if your API is available by clicking the invoke URL

6. Click “Route” from the left menu

Here is the important part, you have to put /{proxy+}, otherwise URL API will not cover all the routes that your EC2 server is serving.

7. Move to “integrations” from the left menu

8. Click “Create and attach an integration”

9. Choose “HTTP URI” for the Integration target

Another important part is, you have to type URL like http://yourip:port/{proxy}.

Again, {proxy}, will make your API automatically serve your other routes. (e.g. /static/style.css)

10. Check if it is working!

Happy coding!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: