0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Calling third-party API on AWS

Last updated at Posted at 2020-05-11

We wanted to call third-party API on AWS. Since they are third-party API, we should call them one by one and dodge to call multiple time at the same time. Otherwise, we might be blocked by providers.

SQS + Lambda

SQS + lambda should be easy to implement, but not sure if it fits with our purpose.
Therefore, we studied how SQS + Lambda works.

architecture.png

Configure SQS

The type of SQS is standard. The default visibility timeout is 30 sec.

sqs-configure.png

Configure Lambda

Add trigger to lambda

add-trigger.png

Set concurrency to 1

concurrency.png

Timeout is 5 for testing

Timeout is 5 sec.
timeout.png

Source code

This lambda will be timeout, but it is on purpose.

from time import sleep
import json

def lambda_handler(event, context):

    print("start python lambda");
    print(event)

    sleep(10)

    print("10 sec latter")

    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Now, ready to go

We sent messages to SQS via AWS console.

send-message.png

send-message1.png

Expected result

Since lambda's timeout is 1 sec and the lambda sleeps 5 sec, every lambda must be timeout.
We assumes that SQS doesn't run all lambda in the queue concurrently, so each lambda should be executed one by one without 30 sec interval which is the default visibility timeout.

Cloudwatch

When we sent messages in a row like "1" -> "2" -> "3", then the log showed up like below.

cloudwatch-log.png

The red block is "1", the blue block is "2" and the yellow block is "3". The first red block is executed at 17:37:05, the first blue block is executed at 17:37:10. Also, the second red block is executed almost 30 sec later. It seems that the default visibility timeout (30 sec) works well.

What this log shows?

If SQS tried to run lambda concurrentry, those message should be invisible. The invisible messages have to wait 30 sec because of the default visibility timeout. However, the blue block is executed at 17:37:10. That means that SQS waited until the first lambda had finished.

Conclustion

It is worth to consider SQS + lambda for calling third-party API.

*本記事は @qualitia_cdevの中の一人、伊藤さんに書いていただきました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?