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.
-
Login to AWS Management Console and select Ireland as the region.
-
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.
-
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.
-
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.
-
Goto the IAM Management console, click on the Roles menu in the left and then click on the Create role button.
-
On the next screen, select API Gateway as the service and click on the Next: Permissions button.
-
On the next screen, click on the Next: Tags button.
-
On the next screen, click on the Next: Review button.
-
On the next screen, type in dojoapiaccessrole as the role name and click on the Create role button.
-
The role is created in no time. Open the dojoapiaccessrole role details, attach an additional AmazonDynamoDBFullAccess policy to the role.
-
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.
-
In the API Gateway Console, click on the Build button for the REST API.
-
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.
-
The API is created in no time. On the next screen, click on the Create Method option under the Action menu.
-
On the next screen, select POST as the method and click on the confirmation icon.
-
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.
-
The method is updated. On the next screen, click on the Integration Request link.
-
On the next screen, expand Mapping Templates section and click on + Add mapping template link.
-
It will add field for the content-type. In the content-type, type in application/json and click on the confirmation icon.
-
It pops up Change passthrough behavior screen and click on Yes button.
-
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.
{ "TableName": "dojotable", "Item": { "id": { "S": "$context.requestId" }, "message": { "S": "$input.path('$.message')" } } }
-
All good. Go back to method configuration and click on the TEST link to start testing the API.
-
On the next screen, copy-paste the following json in the Request Body and click on the Test button.
{ "message": "AWS Dojo Rocks!!!" }
-
The API call executes successfully.
-
You can check the DynamoDB table dojotable and you can see the item stored there due to the API call.
-
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.