Skip to content

SONARPY-3049: Create rule S6243: AWS clients should be reused across Lambda invocations #5148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

github-actions[bot]
Copy link
Contributor

You can preview this rule here (updated a few minutes after each push).

Review

A dedicated reviewer checked the rule description successfully for:

  • logical errors and incorrect information
  • information gaps and missing content
  • text style and tone
  • PR summary and labels follow the guidelines

@Seppli11 Seppli11 force-pushed the rule/S6243-add-python branch from c813fad to e6eba8f Compare June 24, 2025 13:05
@Seppli11 Seppli11 changed the title Create rule S6243 SONARPY-3049: Create rule S6243: AWS clients should be reused across Lambda invocations Jun 24, 2025
@Seppli11 Seppli11 force-pushed the rule/S6243-add-python branch from e6eba8f to adf2771 Compare June 24, 2025 13:14
S6243 has the title "Reusable resources should be initialized at
construction time of Lambda functions"
@Seppli11 Seppli11 force-pushed the rule/S6243-add-python branch from adf2771 to e7c46b5 Compare June 24, 2025 13:16
@Seppli11 Seppli11 requested a review from ghislainpiot June 24, 2025 13:18
Copy link
Contributor

@ghislainpiot ghislainpiot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I left small comments

Comment on lines +18 to +25
"extra": {
"replacementRules": [

],
"legacyKeys": [

]
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The add language GH action moved the java metadata.json a level up so it can be shared. If you're question is about the extra key, I'm not sure either. However, I didn't want to touch it, since something might need it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was referring to the extra key, Not sure why it is there

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Searching through the rspec repo, it's far from the only rule with the "extra" key. There are also quite a few rules with pretty much the same JSON block. So maybe it was once part of the template, but I couldn't trace it in the rspec repository

@@ -0,0 +1,111 @@
This rule raises when resources are recreated on every Lambda function invocation instead of being reused.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This rule raises when resources are recreated on every Lambda function invocation instead of being reused.
This rule raises an issue when resources are recreated on every Lambda function invocation instead of being reused.

I like the other way of saying it, but no strong feeling


Availability risks may also emerge under high load scenarios, as the additional overhead from resource recreation can cause functions to timeout more frequently or hit concurrency limits sooner than necessary.

Increased AWS service API throttling is another potential issue, as recreating resources may lead to more frequent authentication requests and connection establishments, potentially triggering rate limits.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rule is generic, so not just AWS services

Comment on lines 25 to 63
==== Noncompliant code example

[source,python,diff-id=1,diff-type=noncompliant]
----
import boto3

def lambda_handler(event, context):
s3_client = boto3.client('s3') # Noncompliant: Client created inside handler

response = s3_client.get_object(
Bucket='my-bucket',
Key='my-key'
)

return {
'statusCode': 200,
'body': response['Body'].read()
}
----

==== Compliant solution

[source,python,diff-id=1,diff-type=compliant]
----
import boto3

s3_client = boto3.client('s3') # Compliant

def lambda_handler(event, context):
response = s3_client.get_object(
Bucket='my-bucket',
Key='my-key'
)

return {
'statusCode': 200,
'body': response['Body'].read()
}
----
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code blocks can be made much smaller

Copy link

Quality Gate passed Quality Gate passed for 'rspec-tools'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
0 Dependency risks
No data about Coverage
No data about Duplication

See analysis details on SonarQube

Copy link

Quality Gate passed Quality Gate passed for 'rspec-frontend'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
0 Dependency risks
No data about Coverage
No data about Duplication

See analysis details on SonarQube

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants