Gunty
Gunty

Reputation: 2479

AWS Lambda resetting the whole script after a few API calls

I have deployed an AWS Lambda package which interprets trading alerts from tradingview.com.

These alerts are sent to the server at intervals ranging from subhour to a few hours inbetween.

I have a trade log in my application which keeps track of state and other pieces of data.

This trade log gets reset after period of time and i do not know why. My guess is that this happens because the whole script gets reset.

Why is this happening?

Upvotes: 0

Views: 387

Answers (1)

Marcin
Marcin

Reputation: 238139

Your lambda function runs in a temporary environment when its invoked:

When a Lambda function is invoked, AWS Lambda launches an execution context based on the configuration settings you provide. The execution context is a temporary runtime environment that initializes any external dependencies of your Lambda function code, such as database connections or HTTP endpoints.

What's more, the lambda will keep your temporary environment for some time. It is often not reset after each invitation:

After a Lambda function is executed, AWS Lambda maintains the execution context for some time in anticipation of another Lambda function invocation.

Most importantly,you can't rely on it for keeping persistent data between invocation:

When you write your Lambda function code, do not assume that AWS Lambda automatically reuses the execution context for subsequent function invocations.

For persistent storage of data, such as your trade logs, you have to use external data store. Often, for this DynamoDB is used, but can be ElastiCache, RDS, S3 and more.

Upvotes: 2

Related Questions