The first step of your journey is to deploy API Gateway as it will make all future cutovers transparent to the end-users. Initially, the API Gateway is used as a proxy for the legacy APIs. As you continue to strangle out the monolith in future labs, you will configure the API Gateway to route requests to the new microservices.
In the AWS Management Console, navigate to API Gateway.
Start creating a new REST API by clicking the “Build” button in the “REST API” box. (You will also see another box with title “REST API Private”, DON’T choose that since we want a public API.)
In the next view, configure your API Gateway using the settings below. Then click “Create API”.
Choose the protocol: Select “REST”
Create new API: Select “New API”
API name: Enter Unishop
Description: Enter An API for the Unishop
Endpoint Type: Select “Regional”
When you first create a REST API, there are no resources (URLs) or methods (GET/POST/PUT/etc) created for it. API Gateway allows you to have different resources (URLs or paths) and methods assigned to different back-end APIs. For this lab, however, you will configure the API to proxy all requests to the same back-end URL - the Elastic Beanstalk URL - using the proxy resource and ANY method pattern.
In the center pane, in the “Actions” dropdown, select “Create Resource”.
For “Resource Name”, enter api. Doing so, will also autofill the “Resource Path”. Tick the “Enable API Gateway CORS” box and then click the “Create Resource” button.
You will then add a child resource to the “api” resource. On the left side “Resources” menu select the newly created resource “/api”. From the “Actions” menu, click “Create Resource.”
Check the “Configure as proxy resource” box to configure the new resource as a proxy resource. This will automatically populate the resource name and path (see screenshot below). Once again, tick the “Enable API Gateway CORS” box. Click “Create Resource.”
You will now configure the ANY method pattern under the “/{proxy+}” resource.
By default, the ANY method should have been created under the “/{proxy+}” resource. Click “ANY”. In the method setup on the right, configure the following:
Integration type: Select “HTTP Proxy”
Endpoint URL: Enter the value of “Elastic Beanstalk LegacyApplication URL” in your cheat sheet and append api/{proxy} to the end. (If you forget to copy this value to your cheat sheet, please refer to this step.)
Content Handling: Select “Passthrough”
Here is a SAMPLE Endpoint URL: http://legacyapplication.eba-cjskn6u3.us-east-2.elasticbeanstalk.com/api/{proxy}. Note that you should find YOUR OWN in the Elastic Beanstalk Console. If you forgot to note down your own Elastic Beanstalk Endpoint URL, please refer to the part 3 of Environment Setup.
Click the “Save” button to save your changes. You will now see the following:
On the resources left side menu, select the “/{proxy+}” resource. From the “Actions” menu, click “Enable CORS.”
Here you will add a header. In the “Access-Control-Allow-Headers” field, delete the entire default content, and replace it with the text below. Once you’re done, click “Enable CORS and replace existing CORS headers.”
'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,Access-Control-Allow-Origin'
Confirm your changes by clicking the “Yes, replace existing values” button.
Click the “ANY” method under “/{proxy+}”. The content pane will show a graphical-like view of the full round-trip path of requests, starting with the client in a box on the left, and the back-end resource in a box on the right. In the client box there is a “Test” link. Click that link.
In the method test view, select “GET” as the method and enter unicorn as the path. Then, click the “Test” button. The response body (right side of the pane) should show the JSON representation of all the unicorns.
Finally, you will deploy the API. In the center pane, click the “Actions” dropdown and click “Deploy API.”
In the Deploy API dialog window that appears, select “[New Stage]” in the Deployment stage dropdown and enter prod as the stage name. Then click “Deploy”.
You can test the REST API proxy by clicking on the “Invoke URL” link and then appending /api/unicorn to the URL in your browser. You will again see an XML or JSON serialization of the unicorns. Please note down this “Invoke URL” in your cheat sheet (You should paste it under “API Gateway Invoke URL”). If you are ready, let’s move on!
Both API Gateway and S3 (used in the part 2) allow you to use your own domain name. For example, you might use api.example.com and www.example.com for API Gateway and S3 respectively. You can read more about configuring API Gateway and S3 to use custom domain names in the documentation.