2024 07 04 aws cli retrieving cloudwatch logs for lambda with cdk function - bizkite-co/handterm GitHub Wiki


original file name: Codeium Chat - yKbCsJgDSYbvN2MTN1oZIgTVeQTnBr06.md summary: The chat involved a user seeking guidance on how to use AWS CLI commands to retrieve CloudWatch log records for their Lambda function created through AWS CDK. The conversation covered steps to identify Lambda function names, find CloudWatch log group names, filter results using --query parameter, and fetch the latest log events. The user and the assistant discussed constructing commands, filtering results by Lambda function names containing specific strings, inferring log group names, and obtaining the latest log events efficiently. The assistant provided detailed commands and explanations to help the user achieve their tasks effectively. date: 2024-07-04T20:39

Codeium Chat Conversation

Note: This is purely the output of the chat conversation and does not contain any raw data, codebase snippets, etc. used to generate the output.

User at 8:39 pm, Jul 4, 2024

I am debugging my AWS CDK ApiGateway HTTP API, and every time I want to see the result of my latest client-side call to the API, I have to go to the AWS Console.

I was like to construct some AWS CLI commands (I already have it installed and have been using it) that will get the latest CloudWatch log records from the log for the relevant Lambda function.

These are some of the outputs of the CDK stack:

Outputs:
HandTermCdkStack.ApiEndpoint = https://x7jzrjfuv8.execute-api.us-east-1.amazonaws.com/
HandTermCdkStack.BucketName = handterm
HandTermCdkStack.IdentityPoolId = us-east-1:2dd42735-0e86-40f0-9298-e7c1f9b18dfb
HandTermCdkStack.UserPoolClientId = 1li1aa0558h1ugshooc4l69r6n
HandTermCdkStack.UserPoolId = us-east-1_TL47OLcyI
Stack ARN:
arn:aws:cloudformation:us-east-1:318364255844:stack/HandTermCdkStack/1a2f07b0-37db-11ef-a715-0e1174f1a257

Codeium at 8:39 pm, Jul 4, 2024

Based on the outputs of your CDK stack, to construct AWS CLI commands for retrieving the latest CloudWatch log records from the log of a relevant Lambda function, follow these steps. These steps assume you're working in PowerShell on Windows.

  1. Identify the Lambda Function Name: First, you need to know the name of the Lambda function for which you want to retrieve logs. This information might be directly available in your CDK stack outputs if you've configured it to output Lambda function names. If not, you can list all Lambda functions in your account and region with the following command to find the function name:
aws lambda list-functions --region us-east-1

Look through the output for the function related to your API Gateway HTTP API and note the function name.

  1. Find the Log Group Name for the Lambda Function: Lambda functions use log groups in Amazon CloudWatch Logs, typically named /aws/lambda/function-name. Assuming you've found the function name, construct the log group name accordingly.

  2. List the Latest Log Streams: Once you have the log group name, you can list the latest log streams (which contain the log records). Replace function-name with your actual Lambda function name in the following command:

aws logs describe-log-streams --log-group-name /aws/lambda/function-name --region us-east-1 --order-by LastEventTime --descending --limit 1

This command lists the most recent log stream first, limited to the single most recent stream.

  1. Get the Latest Log Events: After identifying the latest log stream, you'll want to retrieve the log events from it. You'll need the logStreamName value from the previous step's output. Replace logStreamName and function-name with the actual values for your setup:
aws logs get-log-events --log-group-name /aws/lambda/function-name --log-stream-name logStreamName --region us-east-1 --limit 5

This command retrieves the latest 5 log events from the specified log stream. Adjust the --limit value as needed to retrieve more or fewer log entries.

Remember to replace placeholders like function-name and logStreamName with actual values relevant to your Lambda function. These commands allow you to quickly check the logs for your Lambda function without navigating through the AWS Console, fitting well into a PowerShell-based workflow.


User at 8:42 pm, Jul 4, 2024

We have the stack name: HandTermCdkStack

Can we write an aws cli that uses the --query switch to get the list of Lambda function names or the CloudWatch log group they write to?


Codeium at 8:42 pm, Jul 4, 2024

Yes, you can use the AWS CLI with the --query option to filter the output and get a list of Lambda function names or the CloudWatch log groups they write to, based on the stack name HandTermCdkStack. Since AWS Lambda functions created by a CloudFormation stack have tags or attributes linking them back to the stack, you can filter Lambda functions based on these attributes.

However, directly querying Lambda functions or their CloudWatch Log Groups by CloudFormation stack name via AWS CLI in one command is not straightforward because Lambda functions do not directly expose the CloudFormation stack name in their descriptive fields accessible via list-functions. Instead, you can approach this task in two steps:

  1. List all resources of a CloudFormation stack, then filter for Lambda functions.
  2. Get the CloudWatch log group name for each Lambda function.

Step 1: List All Lambda Function Names for a Given Stack

This command lists all resources for a given CloudFormation stack, filtering for Lambda functions:

aws cloudformation list-stack-resources --stack-name HandTermCdkStack --region us-east-1 --query "StackResourceSummaries[?ResourceType=='AWS::Lambda::Function'].PhysicalResourceId"

This command returns the names of the Lambda functions associated with HandTermCdkStack.

Step 2: Get CloudWatch Log Group Names for Lambda Functions

Given that you now have a list of Lambda function names, you need to fetch their respective CloudWatch Log Group names. Lambda functions by default write logs to CloudWatch Logs under log groups with the name pattern /aws/lambda/{LambdaFunctionName}. Thus, knowing the Lambda function name enables you to directly construct the log group name. However, if you prefer to fetch this information explicitly or verify it, you'd typically do this on a per-function basis, as there's no direct batch method to get log configuration for multiple Lambda functions simultaneously.

To get the log group for a specific Lambda function, you can actually skip directly querying Lambda for its log group since the naming convention is standard. However, if you wanted to verify or get more details, you'd need to use CloudWatch Logs commands to list or describe log groups, possibly filtering by name prefix (/aws/lambda/), but this is more about log group properties rather than fetching them by Lambda function name.

For scripting purposes, assuming you have a Lambda function name in $lambdaFunctionName, you can construct the log group name like this in PowerShell:

$lambdaFunctionName = "exampleLambdaFunctionName"  # Replace with actual function name
$logGroupName = "/aws/lambda/$lambdaFunctionName"

And if you did need to fetch details about this log group:

aws logs describe-log-groups --log-group-name-prefix $logGroupName --region us-east-1

For a more dynamic approach where you automate this for multiple Lambda functions obtained from step 1, you would likely use a script to iterate over the Lambda function names, construct log group names, and then fetch details or logs as needed.


User at 8:32 am, Jul 5, 2024

How can I make this only return log groups that contain the string GetLog

aws cloudformation list-stack-resources --stack-name HandTermCdkStack --region us-east-1 --query "StackResourceSummaries[?ResourceType=='AWS::Lambda::Function'].PhysicalResourceId"

Codeium at 8:33 am, Jul 5, 2024

To refine the AWS CLI command to only return log groups for Lambda functions that contain the string GetLog in their names, you would typically follow these steps:

  1. Use the aws cloudformation list-stack-resources command to list all Lambda functions associated with the stack.
  2. Filter these Lambda function names for the ones containing GetLog.
  3. Construct the log group names based on the filtered Lambda function names since the log group name conventionally follows the pattern /aws/lambda/{LambdaFunctionName}.

However, because the AWS CLI's --query parameter operates on the output of the command and given that Lambda function names (or their associated CloudWatch Log Groups) are not directly filtered in the list-stack-resources command based on their names, you'll need to perform some post-processing of the command's output.

Here's how you can do it with PowerShell, assuming you're interested in performing all steps in a single pipeline:

aws cloudformation list-stack-resources --stack-name HandTermCdkStack --region us-east-1 --query "StackResourceSummaries[?ResourceType=='AWS::Lambda::Function'].PhysicalResourceId" --output text | ForEach-Object { if ($_ -like '*GetLog*') {$_} }

This command will:

  1. List all Lambda functions associated with HandTermCdkStack.
  2. Convert the output to text for easier parsing in PowerShell (--output text).
  3. Use ForEach-Object to iterate over each line/item of output.
  4. Use an if condition inside the loop to check if the Lambda function name contains GetLog (-like '*GetLog*').
  5. Output the names of the functions that match the condition.

For each Lambda function name that contains GetLog, you can assume their CloudWatch Log Group name by prefixing with /aws/lambda/. However, remember this approach does not directly query CloudWatch Log Groups but infers their names based on Lambda function names. To list or interact with log groups themselves, especially to query logs or log group attributes, you'd use the aws logs CLI commands with the inferred log group names.


User at 8:34 am, Jul 5, 2024

Isn't there a way to filter the results using the --query parameter that is already in the command?


Codeium at 8:34 am, Jul 5, 2024

Yes, you can refine your AWS CLI command to filter Lambda function names directly using the --query parameter to include only those that contain the string GetLog. The --query parameter in the AWS CLI command supports JMESPath, a query language for JSON, which you can use to perform more complex queries directly on the JSON output returned by the command.

If you want to filter the Lambda function names to only include those that contain the string GetLog, you can modify your command as follows:

aws cloudformation list-stack-resources --stack-name HandTermCdkStack --region us-east-1 --query "StackResourceSummaries[?ResourceType=='AWS::Lambda::Function' && contains(PhysicalResourceId, 'GetLog')].PhysicalResourceId"

Here's what this command does:

  • --query "StackResourceSummaries[?ResourceType=='AWS::Lambda::Function' && contains(PhysicalResourceId, 'GetLog')].PhysicalResourceId": This query filters the StackResourceSummaries for resources where ResourceType is AWS::Lambda::Function and the PhysicalResourceId (which is the name of the Lambda function) contains the substring GetLog. It then extracts the PhysicalResourceId for each of these filtered results.

  • contains(PhysicalResourceId, 'GetLog'): This part of the query checks if the PhysicalResourceId value contains the substring GetLog.

This command will return a list of Lambda function names associated with the stack HandTermCdkStack that contain the string GetLog, directly using the AWS CLI without the need for further filtering or post-processing.


User at 8:49 am, Jul 5, 2024

I have this part working:

[08:47:30]❯ aws cloudformation list-stack-resources --stack-name HandTermCdkStack --region us-east-1 --query "StackResourceSummaries[?ResourceType=='AWS::Lambda::Function' && contains(PhysicalResourceId, 'GetLog')].PhysicalResourceId" --output text | Set-Variable logGroupName
∙ $logGroupName = '/aws/lambda/' + $logGroupName$logGroupName
/aws/lambda/HandTermCdkStack-GetLogFunction03FFABB2-UrvQHD0fkbIw
handex-react on  master [$!+] via  v20.12.1 on ☁️ mark (us-east-1) took 2s 
🕐[08:48:07]❯ aws logs describe-log-groups --log-group-name-prefix $logGroupName                                           
- logGroups:
 - arn: arn:aws:logs:us-east-1:318364255844:log-group:/aws/lambda/HandTermCdkStack-GetLogFunction03FFABB2-UrvQHD0fkbIw:*
  creationTime: 1720152675778
  logGroupName: /aws/lambda/HandTermCdkStack-GetLogFunction03FFABB2-UrvQHD0fkbIw
  metricFilterCount: 0
  storedBytes: 0

How do I use that to get the latest events?


Codeium at 8:49 am, Jul 5, 2024

To get the latest log events from the CloudWatch log group you've identified, you can follow these steps. The process involves first finding the most recent log stream within the log group, and then fetching the log events from that stream.

  1. List the Latest Log Stream: You can list the log streams sorted by their last event time in descending order, and limit the results to the most recent one. Use the logGroupName variable you've already set:
$latestLogStreamName = aws logs describe-log-streams --log-group-name $logGroupName --order-by LastEventTime --descending --max-items 1 --query "logStreams[0].logStreamName" --output text

This command fetches the name of the most recently active log stream within the specified log group.

  1. Get the Latest Log Events: Once you have the name of the latest log stream, you can retrieve its log events. Depending on how much data you're interested in, you can limit the number of events retrieved. Here's how to get the latest 10 log events from that stream:
aws logs get-log-events --log-group-name $logGroupName --log-stream-name $latestLogStreamName --limit 10

This command retrieves the last 10 log events from the most recently active log stream within your log group.

If you encounter issues with the variable interpolation (especially $latestLogStreamName not being recognized correctly due to its scope or the way it's generated), ensure you're executing these commands in the same session and context. If necessary, adjust the commands based on your PowerShell version or environment specifics.

Together, these commands allow you to programmatically retrieve the latest log events for the specific Lambda function associated with the GetLog operation within your HandTermCdkStack.

⚠️ **GitHub.com Fallback** ⚠️