Skip to main content
If your Bedrock-powered application runs on an Amazon Elastic Compute Cloud (EC2) instance (a virtual machine) rather than AWS Lambda, your logging strategy changes slightly. From a VM you can either ship local logs using an agent or have the application call CloudWatch Logs directly using the AWS SDK (for example, Boto3). Writing logs directly to CloudWatch centralizes observability and avoids relying solely on the instance filesystem. Key operational considerations when writing logs from a VM:
  • CloudWatch Logs expects event timestamps in milliseconds since the epoch.
  • When appending to an existing log stream, CloudWatch requires the correct sequenceToken (upload sequence token). If the stream is new, create the log group and log stream before calling put_log_events.
  • Your application needs IAM permissions like logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents, and optionally logs:DescribeLogStreams.
  • On EC2, attach an IAM role (instance profile) to the instance so your code can use temporary credentials automatically. For hosts outside AWS, provide programmatic credentials (access key/secret key) with minimal necessary scope.
The image is a simple infographic titled "Workflow: Logging in VM" showing two semicircles: the left (blue) says "Create SDK client to CloudWatch Logs" and the right (green) says "Use put log events method to write directly to a custom CloudWatch log group," with cloud/log icons.
Example: writing logs from EC2 to CloudWatch using Boto3
  • This sample shows the common sequence:
    1. Ensure the log group and log stream exist.
    2. Query the upload sequence token (if any).
    3. Call put_log_events.
    4. Handle InvalidSequenceTokenException/DataAlreadyAcceptedException by refetching the token and retrying once.
Grant the required IAM permissions to the identity used by your code: typically logs:CreateLogGroup, logs:CreateLogStream, logs:DescribeLogStreams, and logs:PutLogEvents. On EC2, prefer an instance profile (IAM role); if running outside AWS, use short-lived credentials and scope them narrowly.
Comparing logging approaches for Bedrock applications
  • Choose based on where compute runs, privacy/governance needs, and credential provisioning.
Links and references
An infographic titled "Workflow: Logging Dependencies" shows a target with an arrow on the left and a vertical sequence of colorful service icons and labels on the right. The labels (Bedrock Model, Direct CloudWatch, Lambda App Logging) list each approach's code dependencies and required IAM/permissions.
Summary and best practices
  • For AWS Lambda: write to stdout/stderr and ensure the execution role has logging permissions; CloudWatch capture is automatic.
  • For EC2 / VMs: either run a log shipping agent or have the app call CloudWatch Logs APIs directly; use instance roles to avoid long-lived credentials.
  • For external hosts: use programmatic credentials scoped to only the required CloudWatch permissions and rotate them regularly.
  • For Bedrock-level tracing: enable model invocation logging in the Bedrock service when you need centralized model I/O capture — but review privacy and governance requirements before enabling.

Watch Video