eslint-plugin-stedi-aws-rules icon indicating copy to clipboard operation
eslint-plugin-stedi-aws-rules copied to clipboard

Intialize SDK clients outside of handler

Open RafalWilinski opened this issue 5 years ago • 3 comments

Rule name

initialize-awssdk-outside-handler

Use case description

Initializing AWS SDK client instances inside handler code slows down the execution and does not share them between executions. AWS SDK clients should be initialized outside of handler code to reduce costs and make handlers execution faster.

Examples

Incorrect

import S3 from 'aws-sdk/clients/s3';

exports.handler = async (event) => {
	const s3 = new S3();
}

Correct

import S3 from 'aws-sdk/clients/s3';

const s3 = new S3();

exports.handler = async (event) => {
    ...
}

RafalWilinski avatar Oct 02 '20 08:10 RafalWilinski

That may have some issues when we start assuming different roles on every invocation

aisamu avatar Oct 02 '20 11:10 aisamu

That may have some issues when we start assuming different roles on every invocation

You can still update the role after the client has been initialized. However, I don’t know if this would have any performance benefits. It would be good to have some data or a reference before implementing this as a rule.

tvanhens avatar Oct 03 '20 17:10 tvanhens

I found this: https://dev.to/sosnowski/optimizing-aws-lambda-3m13#code-and-package-structure

RafalWilinski avatar Oct 03 '20 18:10 RafalWilinski