View a markdown version of this page

Hello CloudWatch Logs - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Hello CloudWatch Logs

The following code example shows how to get started using CloudWatch Logs.

Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

def hello_cloudwatch_logs() -> None: """ Verifies connectivity to CloudWatch Logs by calling DescribeLogGroups and displaying the first page of log groups with name, ARN, and creation time. """ logs_client = boto3.client("logs") try: response = logs_client.describe_log_groups() log_groups = response.get("logGroups", list()) if not log_groups: print("No log groups found in this Region for your account.") else: print(f"Found {len(log_groups)} log group(s):\n") for lg in log_groups: name = lg.get("logGroupName", "N/A") arn = lg.get("arn", "N/A") creation_ms = lg.get("creationTime", 0) creation_dt = datetime.fromtimestamp( creation_ms / 1000, tz=timezone.utc ) print(f" Name: {name}") print(f" ARN: {arn}") print(f" Created: {creation_dt.strftime('%Y-%m-%d %H:%M:%S UTC')}") print() except ClientError as error: logger.error( "Failed to describe log groups: %s", error.response["Error"]["Message"], ) raise
  • For API details, see DescribeLogGroups in AWS SDK for Python (Boto3) API Reference.