Part 2 - Configure Resource - API Gateway

In this section, you will update the API Gateway to route requests to the inventory service deployed on ECS.

Create Resource

  1. In the AWS Management Console, navigate to API Gateway. In the API Gateway Console, select the “Unishop” API you created in the Facade Configuration lab.

  2. In the vertical navigation bar on the left, go to “Resources.”

  3. You will start by creating a new child resource under the “/api” resource. Select the “/api” resource. Then Click on “Actions,” and in the drop-down list, select “Create Resource.”

    select api directory

    select create resource

  4. Enter unicorn for resource name. The resource path will be automatically filled in. Then, check “Enable API Gateway CORS.” Finally, click “Create Resource.”

    select create finish

  5. Under the newly created resource “/unicorn”, create another resource. Similar to step 3, select the “/unicorn” resource, and inside the “Actions” drop-down list, click “Create Resource.” Enter {id} (with the brackets) for resource name. Delete the auto-filled resource path and enter {id} (same as the resource name). The brackets tell API Gateway to treat whatever is inside as a variable so that the client can pass useful information with it. Check “Enable API Gateway CORS.” Finally, click “Create Resource.”

    id resource create finish

Add Methods

  1. First, you will add a method that listens for GET requests under “/unicorn” resource. This request fetches a list of inventory items. Select the “/unicorn” resource in the “Actions” drop-down list select “Create Method.”

    select method directory

  2. A drop-down list will appear right beneath “/unicorn” resource. Select “GET” and click on the check mark right next to it.

    select GET

  3. Next, route requests to the Load balancer which was created in part 1. You need to setup the method to point to the load balancer. Select “Integration Type” as HTTP. For “Endpoint URL”, enter the value of “Load balancer URL” in your cheat sheet, and append api/unicorn. (If you forget to copy that value to your cheat sheet, please refer to step 9 of this section.) The other configurations can be left with the default options. Finally, hit “Save.”

    The URL will be of the form http://inventoryservice-{ACCOUNT_ID}.{AWS_REGION}.elb.amazonaws.com

    Setup GET method

  4. Now you will add a method that listens for POST requests under “/unicorn”. This is used to add items to the inventory. To do this repeat step 1 - 3 but select POST in Step 2.

    POST method

  5. Repeat step 1 - 3 under the “{id}” resource to create an “ANY” method.

    • “Endpoint URL”: Enter the value of “Load balancer URL” in your cheat sheet and append api/unicorn/{id}. (If you forget to copy that value to your cheat sheet, please refer to step 9 of this section.)
    • Tick “Use HTTP Proxy integration” ANY method
  6. Repeat step 1 - 3 under the “{id}” resource to create a “GET”.

    • “Endpoint URL”: Enter the value of “Load balancer URL” in your cheat sheet and append api/unicorn/{id}. (If you forget to copy that value to your cheat sheet, please refer to step 9 of this section.)

    GET method

Add Authorizer

Some requests require that users are authenticated. Next, you will add authorizer to methods that handles those requests, which are the POST method and ANY method you just added. You will be using the same authorizer you created in the part 3 of Serverless Basket Service. For more detailed instructions on how to add authorizer to methods, please refer to the Add Methods section of that part.

  1. Select the POST method under “/unicorn”, then click on “Method Request.” On the following page, set “Authorization” to CognitoAuth.

    ANY method

    ANY method

  2. Select the ANY method under “/unicorn/{id}”, and repeat the same process in the previous step.

    You should not add any authorizer for the GET method under “/unicorn/{id}” resource. In the part 4 of this lab, you will use a tool called Locust to test that endpoint. If you add an authorizer for that endpoint, the test code will fail.

Add CORS Header

Finally, you need to enable CORS for both of the newly added resources.

  1. Select the “/unicorn” resource, and under the “Actions” drop-down list, select “Enable CORS.”

    CORS enable

  2. Similar to the Create Method section in the part 1 of Facade Configuration, replace the “Access-Control-Allow-Headers” field with the following. Hit “Enable CORS” and “Yes, replace existing CORS headers”.

    'Access-Control-Allow-Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'
    

    CORS enable finish

  3. Repeat step 1 and 2 for the “/unicorn/{id}” resource.

Deploy API

Deploy the API again to execute the changes.

  1. In the “Actions” drop-down list, select “Deploy API.”

    Deploy API

  2. In the subsequent pop-up window, select “prod” for deployment stage, and click the “Deploy” button.

    Select Stage

Congratulations!

You have strangled out the final service from the monolith. Lets go to our website to see how it works. Paste the value of “Unishop Frontend URL in S3” from your cheat sheet to your browser to visit Unishop.

Website

The unicorns are now seemlessly loading from the containerized Inventory service! Although you cannot see any difference in the frontend, you will know the new Inventory service in ECS has been used after completing the next two parts. In the next part, we will load test the newly strangled service.