Skip to content
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

fix: getting logging information from lambda #6475

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
chevron~=0.12
click~=8.1
Flask<3.1
boto3>=1.26.109,<2
boto3>=1.29.2,<2
jmespath~=1.0.1
ruamel_yaml~=0.18.5
PyYAML~=6.0,>=6.0.1
Expand Down
4 changes: 3 additions & 1 deletion samcli/lib/observability/cw_logs/cw_log_group_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def for_lambda_function(boto_client_provider: BotoProviderType, function_name: s
"""
log_group_name = ""
try:
function_configuration = boto_client_provider("lambda").get_function_configuration(function_name)
function_configuration = boto_client_provider("lambda").get_function_configuration(
FunctionName=function_name
)
logging_config = function_configuration.get("LoggingConfig")
if logging_config:
log_group_name = logging_config.get("LogGroup")
Expand Down
1 change: 1 addition & 0 deletions tests/integration/logs/test_logs_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def _check_logs(self, cmd_list: List, log_strings: List[str], output: str = "tex
REGULAR_STACK_FUNCTION_LIST = [
"ApiGwFunction",
"SfnFunction",
"FunctionWithCustomLoggingConfig",
]
REGULAR_STACK_APIGW_LIST = [
"HelloWorldServerlessApi",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

def handler(event, context):
print("Hello world from ChildStack/FunctionWithCustomLoggingConfig function")
print("this should be filtered ChildStackFunctionWithCustomLoggingConfig")
return {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

def handler(event, context):
print("Hello world from ChildStack/GrandChildStack/FunctionWithCustomLoggingConfig function")
print("this should be filtered ChildStackGrandChildStackFunctionWithCustomLoggingConfig")
return {}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ Resources:
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"

FunctionWithCustomLoggingConfig:
Type: AWS::Serverless::Function
Properties:
CodeUri: function-with-custom-logging/
Handler: app.handler
Runtime: python3.9
Tracing: Active
LoggingConfig:
LogFormat: JSON
LogGroup: !Sub /aws/lambda/${AWS::StackName}

Outputs:
HelloWorldServerlessApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ Resources:
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"

FunctionWithCustomLoggingConfig:
Type: AWS::Serverless::Function
Properties:
CodeUri: function-with-custom-logging/
Handler: app.handler
Runtime: python3.9
Tracing: Active
LoggingConfig:
LogFormat: JSON
LogGroup: !Sub /aws/lambda/${AWS::StackName}

GrandChildStack:
Type: AWS::Serverless::Application
Properties:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

def handler(event, context):
print("Hello world from FunctionWithCustomLoggingConfig function")
print("this should be filtered FunctionWithCustomLoggingConfig")
return {}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ Resources:
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"

FunctionWithCustomLoggingConfig:
Type: AWS::Serverless::Function
Properties:
CodeUri: function-with-custom-logging/
Handler: app.handler
Runtime: python3.9
Tracing: Active
LoggingConfig:
LogFormat: JSON
LogGroup: !Sub /aws/lambda/${AWS::StackName}

ChildStack:
Type: AWS::Serverless::Application
Properties:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

def handler(event, context):
print("Hello world from FunctionWithCustomLoggingConfig function")
print("this should be filtered FunctionWithCustomLoggingConfig")
return {}
11 changes: 11 additions & 0 deletions tests/integration/testdata/logs/python-apigw-sfn/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ Resources:
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"

FunctionWithCustomLoggingConfig:
Type: AWS::Serverless::Function
Properties:
CodeUri: function-with-custom-logging/
Handler: app.handler
Runtime: python3.9
Tracing: Active
LoggingConfig:
LogFormat: JSON
LogGroup: !Sub /aws/lambda/${AWS::StackName}

Outputs:
HelloWorldServerlessApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from unittest import TestCase
from unittest.mock import Mock, ANY
from unittest.mock import Mock, ANY, call

from parameterized import parameterized

Expand All @@ -26,6 +26,9 @@ def test_must_return_custom_log_group_name(self):
result = LogGroupProvider.for_lambda_function(given_client_provider, "my_function_name")

self.assertEqual(expected, result)
given_client_provider.assert_has_calls(
[call("lambda").get_function_configuration(FunctionName="my_function_name")]
)

def test_must_return_default_log_group_name_with_exception_raised(self):
expected = "/aws/lambda/my_function_name"
Expand Down
Loading