Digital Marketing

Integrate API Gateway with SQS

1. Create SQS queue



Open AWS console in services, navigate to Simple Queue Service.
Click on “Create New Queue”
Give queue a name, in our case it is “beaches” and hit “Create Queue”




2. Create IAM Policy for AWS API Gateway to push Request Message to Queue.



Select IAM, Navigate to “policies” and click on “Create Policy”.


Open JSON editor and add the following policy, click on “Review Policy”, name policy as beaches-policy and hit “Create Policy”.

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "sqs:SendMessage",
"Resource": "arn:aws:sqs:your-sqs-arn:123456789:beaches"
}
]
}

Both send and receive permissions:

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"sqs:SendMessage",
"sqs:ReceiveMessage"
],
"Resource": "arn:aws:sqs:us-west-1:your-sqs-arn:beaches"
}
]
}

3. Create IAM Role for AWS API Gateway to push Request Message to Queue.

Select IAM, Navigate to “Roles” and click on “Create Role”. Now, select “API Gateway” and click “Next:Permissions”.

Give a name to the role you created. such as beaches-role. Currently the only attached policy is “AmazonAPIGatewayPushToCloudWatchLogs”.

After role is created, edit the role and attach the former created beaches-policy.

4. Create an API Structure


Name the REST api such as beaches.

Click on “Action” and “create resource” and resource as v1.
Click on “v1”, and hit “action > create resource” to create resource named i88ca.
Click on “enqueue” and hit “action > create method” and from dropdown select “POST”.

5. Integrate the API with SQS

Click on the “POST” method, in “integration type” click on “AWS service”, provide region and AWS service as “SQS”.
Set Path override to Queue Path created in step 1. ( YOUR-ACCOUNT-ID/YOUR-QUEU-NAME
Set execution role to the beaches-role created in step 3. and hit “save”.

6. Modify API Request

Click on “Integrate Request”
Set HTTP header with Name as Content-Type and Mapped from as 'application/x-www-form-urlencoded' .

In “Request body passthrough” select never and click on “add mapping template”, name it as application/json and add the following snippet 
Action=SendMessage&MessageBody=$input.body
This step will transform your request body to the one accepted by SQS.


If you want to save body data as form-urlencoded to SQS, then click on “add mapping template”, name it as application/x-www-form-urlencoded and add the following snippet Action=SendMessage&MessageBody=$util.urlEncode($input.body)

7. Test And Deploy

AWS api gateway provides functionality to test the API.
In the request body, add the test data.
{
"test" : "goyun.info"
}
If you followed all the steps correctly, you should get 200 status.

Comments

Popular posts from this blog

Make online money from the Internet