Open In App

Create a REST API as an Amazon Kinesis Proxy in API Gateway

Improve
Improve
Like Article
Like
Save
Share
Report

Pre-Requisite: AWS 

Amazon Kinesis is a managed, scalable, cloud-based service that enables real-time processing of enormous amounts of data per second in real-time. It is intended for real-time applications and lets developers collect an unlimited amount of data from many sources, scaling up and down that can be run on EC2 instances. It is used to collect, store, and interpret data from big, dispersed sources like event logs and social media feeds. Kinesis delivers the data to various consumers at the same time once it has been processed.

This article explains how to set up and configure a REST API with an AWS integration to access Kinesis. To demonstrate, we construct an example API that allows a client to list the user’s available streams in Kinesis.

To complete the previous tasks, the API offers methods on various resources that call the following:

  • The ListStreams action in Kinesis

We specifically design the API as follows:

  • Expose an HTTP GET method on the API’s /streams resource and link it with the ListStreams action in Kinesis to list the streams in the caller’s account.

Steps to create an API as an AWS service proxy for Kinesis:

Step 1: Go to the AWS console to access the API Gateway console.

Step 2: Select Build under REST API. When the Create Example API popup window displays, click OK.
 

API Gateway

 

Create API

 

Step 3: Choose New API.

Step 4: Enter KinesisProxy in the API name field. Leave the other fields in their default settings.

Step 5: If you want, enter a description in the Description field.

Step 6: Choose to Create API.

Choose the protocol and API and region

 

Step 7: Choose the API root resource. Select Create Resource from the Actions menu.

Select the Actions

 

Step 8: Enter Streams in the Resource Name area, leave the Resource Path and other fields alone and click Create Resource.

Selecting the path

 

Step 9: Select the /Streams resource. Choose Create Method from the Actions menu, then GET from the list, and finally the check mark icon to finish creating the method.

Select the method

 

Select the call

 

Step 10: You must have suitable IAM policies associated with an IAM role in order for the API to invoke Kinesis actions. Go to the IAM dashboard and create a new role.

Create the Roles

 

Select the polices

 

Step 11: Choose a Custom trust policy and paste the following. Click Next.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": "apigateway.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}       
Custom policy

 

Step 12: Attach the AmazonKinesisFullAccess policy to the newly created role and click Next. The AmazonKinesisFullAccess policy is used to enable read-write activities in Kinesis.

permissions policies

 

Step 13: Give the role any name and description you want and click on Create Role.

Give the Role name

 

Create the Role

 

Step 14: Now the role is created. Choose Amazon Service in the method’s Setup box.

Step 15: Choose a region for AWS Region (e.g., us-east-1).

Step 16: Choose Kinesis for AWS Service.

Step 17: Leave the AWS Subdomain blank.

Step 18: Choose POST as the HTTP method.

Step 19: Choose Use action name as the Action Type.

Step 20: Enter ListStreams in the Action field.

Step 21: Copy the ARN of the role we created above and paste it into the Execution role field.

Copy the ARN

 

Step 22: For Content Handling, leave the default setting of Passthrough.

Step 23: Choose Save to complete the method’s initial configuration.

Select the integration type

 

Step 24: Expand the HTTP Headers section in the Integration Request window and choose Add header.

Flow of the execution

 

Add the header

 

Step 25: Enter Content-Type in the Name field.

Step 26: Enter ‘application/x-amz-json-1.1’ in the Mapped field.

Step 27: To save the setting, choose the check mark symbol. To alert Kinesis that the input is of a certain version of JSON, we utilized a request parameter mapping to set the Content-Type header to the static value ‘application/x-amz-json-1.1’.

Name the header

 

Step 28: Expand the Mapping Templates section and choose to Add a mapping template.

Add the mapping template

 

Step 29: Enter application/JSON in the Content-Type field.

Step 30: To preserve the Content-Type option, click the check mark symbol. In Modify passthrough behavior, choose Yes to secure this integration.

Select the content type

 

Secure the integration

 

Step 31: In the template editor, enter {}.

Step 32: To save the mapping template, click the Save button.

Output

 

Step 33: Choose the /streams/GET entry from the Resources pane in the API Gateway console, then the Test invocation option, and finally Test.

Step 34: Assuming you’ve previously generated two streams in Kinesis called “myStream” and “yourStream,” the successful test returns a 200 OK response with the following payload.



Last Updated : 28 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads