-
Notifications
You must be signed in to change notification settings - Fork 31
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
base: master
Are you sure you want to change the base?
Conversation
c813fad
to
e6eba8f
Compare
e6eba8f
to
adf2771
Compare
S6243 has the title "Reusable resources should be initialized at construction time of Lambda functions"
adf2771
to
e7c46b5
Compare
There was a problem hiding this 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
"extra": { | ||
"replacementRules": [ | ||
|
||
], | ||
"legacyKeys": [ | ||
|
||
] | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
rules/S6243/python/rule.adoc
Outdated
@@ -0,0 +1,111 @@ | |||
This rule raises when resources are recreated on every Lambda function invocation instead of being reused. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
rules/S6243/python/rule.adoc
Outdated
|
||
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. |
There was a problem hiding this comment.
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
rules/S6243/python/rule.adoc
Outdated
==== 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() | ||
} | ||
---- |
There was a problem hiding this comment.
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
|
|
You can preview this rule here (updated a few minutes after each push).
Review
A dedicated reviewer checked the rule description successfully for: