In the previous post, we successfully deployed our server app to EC2. However, it still uses http. To overcome this issue, we can use API Gateway to act as a front door to our server app. Here are the steps to connect the EC2 to API Gateway:
Create API
- Open API Gateway Service and click the
Create API
button. - Select
REST API
and then click theBuild
button.
- Enter the API name and click Create API.
Create Resource
- Open the API, then click the
Create resource
button.
- Check the Proxy resource and enter {proxy+} as the resource name, then click Create resource.
- Click on ANY resources, then click
Edit integration
button.
- Select HTTP method, check the HTTP proxy integration, set HTTP method to ANY, set the Endpoint URL to our server app URL http://3.27.113.187:3000/{proxy}, and finally, set Content handling to Passthrough.
- The last step is to add a new URL path parameter with the key proxy and the value method.request.path.proxy, then click the Save button.
Deploy the API
- In the resources page, click
Deploy API
.
- Let’s create a new stage and click Deploy button.
- Finally, after the deployment finishes, we can access our API here.
Handling error path
If you access the index page, you will receive an error page.
We can fix this issue by following these several steps.
- In the
Resources
page, clickCreate Method
button.
- Then we set the Method to be GET, check the HTTP proxy integration, and enter our server app public URL. Finally, click Create method.
- Finally, let’s redeploy our API to prod stage, and when we open our index API, it should not show any error.
Top comments (0)