Important Note: You will create AWS resources during the exercise which will incur cost in your AWS account. It is recommended to clean-up the resources as soon as you finish the exercise to minimize the cost.

Using API Gateway as Proxy for AWS Services

Amazon API Gateway allows to create an API directly in front of an AWS service API. This way the API works as proxy to the AWS Service. In this exercise, you learn to configure a Proxy API in API Gateway in front of a DynamoDB table.

Step1: Pre-Requisite


You need to have an AWS account with administrative access to complete the exercise. If you don’t have an AWS account, kindly use the link to create free trial account for AWS.

Step2: Create DynamoDB Table


You create a DynamoDB table dojotable which is configured behind the API Proxy in API Gateway.

  1. Login to AWS Management Console and select Ireland as the region.

  2. The exercise is using the new UI console for DynamoDB. Goto DynamoDB console. Select Tables menu in the left and then click on the Create table button.

    Amazon DynamoDB

  3. On the next screen, type in dojotable as the table name. Type in id as the partition key with data type selected as String. Keep rest of the configuration to the default and click on the Create table button.

    Amazon DynamoDB

  4. The table is created in no time. Next, you create an IAM Role for the API Gateway.

Step3: Create API Gateway IAM Role


You create an IAM Role which used by the API Gateway for authorization to call other AWS Services.

  1. Goto the IAM Management console, click on the Roles menu in the left and then click on the Create role button.

    API Gateway

  2. On the next screen, select API Gateway as the service and click on the Next: Permissions button.

    API Gateway

  3. On the next screen, click on the Next: Tags button.

  4. On the next screen, click on the Next: Review button.

  5. On the next screen, type in dojoapiaccessrole as the role name and click on the Create role button.

    API Gateway

  6. The role is created in no time. Open the dojoapiaccessrole role details, attach an additional AmazonDynamoDBFullAccess policy to the role.

    API Gateway

  7. The role is ready. Make note of the role ARN as you need it later when configuring the API. You now create and Run Proxy API in API Gateway.

Step4: Create Proxy API


In this step, you configure REST API in API Gateway which works as proxy to the DynamoDB Table.

  1. In the API Gateway Console, click on the Build button for the REST API.

    API Gateway

  2. On the next screen, select New API option. Type in dojoproxyapi for the API Name. Keep rest of the configuration to the default and click on the Create API button.

    API Gateway

  3. The API is created in no time. On the next screen, click on the Create Method option under the Action menu.

    API Gateway

  4. On the next screen, select POST as the method and click on the confirmation icon.

    API Gateway

  5. The method is created in no time. On the next screen, select AWS Service option for the integration type. Select eu-west-1 as the region. Select DynamoDB as the AWS Service. Select POST as the HTTP Method. Select Use action name for the Action Type. Type in PutItem for the Action. Provide API IAM role ARN in the Execution role field. Select Passthrough for the Content Handling. Finally, click on the Save button.

    API Gateway

  6. The method is updated. On the next screen, click on the Integration Request link.

    API Gateway

  7. On the next screen, expand Mapping Templates section and click on + Add mapping template link.

    API Gateway

  8. It will add field for the content-type. In the content-type, type in application/json and click on the confirmation icon.

    API Gateway

  9. It pops up Change passthrough behavior screen and click on Yes button.

    API Gateway

  10. On the same screen, copy-paste the following model mapping for the content-type and click on the Save button.In this model, the API is expecting a json input with message as a field. The API will use API input to create a request json matching the schema below and then it will pass it to DynamoDB PutItem method. The PutItem method expecting request in the format below and it will insert the item in the table.

    API Gateway

    { 
        "TableName": "dojotable",
        "Item": {
        "id": {
                "S": "$context.requestId"
                },
            "message": {
                "S": "$input.path('$.message')"
            }
        }
    }
    
  11. All good. Go back to method configuration and click on the TEST link to start testing the API.

    API Gateway

  12. On the next screen, copy-paste the following json in the Request Body and click on the Test button.

    API Gateway

    {
      "message":  "AWS Dojo Rocks!!!"
    }
    
  13. The API call executes successfully.

    API Gateway

  14. You can check the DynamoDB table dojotable and you can see the item stored there due to the API call.

    API Gateway

  15. This finishes the exercise. Follow-up the next step to clean up the resources so that you don’t incur any cost post the exercise.

Step5: Clean up


Delete dojoproxyapi API in API Gateway.

Delete dojotable DynamoDB Table.

Delete dojoapiaccessrole IAM Role.

Thanks and hope you enjoyed the exercise.


Back to the Exercises