or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

aws-sdk-integration.mdbase64.mddynamodb.mdenvironment-variables.mdindex.mdlru-cache.mdmiddy-integration.mdtype-utilities.mdutility-class.md
tile.json

aws-sdk-integration.mddocs/

AWS SDK Integration

Utilities for integrating with AWS SDK v3 clients, including middleware for adding Powertools user agent information for usage tracking.

Capabilities

Add User Agent Middleware

Add the Powertools for AWS Lambda user agent middleware to AWS SDK v3 clients.

/**
 * Add the Powertools for AWS Lambda user agent middleware to an AWS SDK v3 client.
 * This middleware appends the feature name and Powertools version to the user agent string
 * for usage tracking. The middleware is used to secure continued investment in the project.
 *
 * The middleware will append information in the format: "PT/<feature>/<version> PTEnv/<environment>"
 * Example: "PT/Tracer/2.29.0 PTEnv/nodejs20.x"
 *
 * @param client - The AWS SDK v3 client to add the middleware to
 * @param feature - The feature name to be added to the user agent (e.g., 'Tracer', 'Logger', 'Metrics')
 * @throws Error if the client does not match the expected AWS SDK v3 interface
 */
function addUserAgentMiddleware(client: unknown, feature: string): void;

SDK Client Type Guard

Type guard to check if a client is a valid AWS SDK v3 client.

/**
 * Type guard to check if the client provided is a valid AWS SDK v3 client.
 * Validates that the client has the required methods and properties for middleware integration.
 *
 * @param client - The client to check
 * @returns Type guard indicating if client is a valid SdkClient
 */
function isSdkClient(client: unknown): client is SdkClient;

SDK Client Interface

/**
 * Interface representing a valid AWS SDK v3 client.
 */
interface SdkClient {
  /** Method to send commands to AWS services */
  send: Function;
  /** Client configuration object */
  config: object;
  /** Middleware stack for request/response processing */
  middlewareStack: {
    /** Get list of middleware names */
    identify: () => string[];
    /** Add middleware relative to another middleware */
    addRelativeTo: (middleware: Function, options: object) => void;
  };
}

Usage Examples

Basic Usage with DynamoDB Client

import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';

// Create AWS SDK v3 client
const dynamoDBClient = new DynamoDBClient({});

// Add Powertools user agent middleware
addUserAgentMiddleware(dynamoDBClient, 'MyFeature');

// Now all requests will include Powertools user agent information
// The user agent will include: "PT/MyFeature/2.29.0 PTEnv/nodejs20.x"

With Multiple AWS Clients

import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { S3Client } from '@aws-sdk/client-s3';
import { SQSClient } from '@aws-sdk/client-sqs';
import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';

const dynamoDBClient = new DynamoDBClient({});
const s3Client = new S3Client({});
const sqsClient = new SQSClient({});

// Add middleware to all clients
addUserAgentMiddleware(dynamoDBClient, 'Logger');
addUserAgentMiddleware(s3Client, 'Logger');
addUserAgentMiddleware(sqsClient, 'Logger');

Type Guard Usage

import { isSdkClient } from '@aws-lambda-powertools/commons';
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';

function processClient(client: unknown) {
  if (isSdkClient(client)) {
    // TypeScript knows client is SdkClient here
    console.log('Valid AWS SDK v3 client');
    // Can safely access client.send, client.config, etc.
  } else {
    console.log('Not a valid AWS SDK v3 client');
  }
}

const dynamoDBClient = new DynamoDBClient({});
processClient(dynamoDBClient); // Valid AWS SDK v3 client
processClient({}); // Not a valid AWS SDK v3 client

Custom Powertools Utility Integration

import { addUserAgentMiddleware, isSdkClient } from '@aws-lambda-powertools/commons';

class MyCustomUtility {
  private client: unknown;

  constructor(client: unknown) {
    if (!isSdkClient(client)) {
      throw new Error('Invalid AWS SDK client provided');
    }

    this.client = client;

    // Add user agent middleware for tracking
    addUserAgentMiddleware(client, 'CustomUtility');
  }

  // ... rest of utility implementation
}

Error Handling

import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';

// The function will warn but not throw on invalid clients
const invalidClient = { notAnSdkClient: true };

// This will log a warning to console but not throw
addUserAgentMiddleware(invalidClient, 'MyFeature');
// Console output: "Failed to add user agent middleware [Error: ...]"

Middleware Behavior

The user agent middleware:

  1. Idempotent: If Powertools middleware is already present, it won't be added again
  2. Non-Intrusive: Failures to add middleware are logged as warnings but don't throw errors
  3. Smart Updates: If a generic Powertools user agent is present (PT/NO-OP), it's updated with the specific feature name
  4. Position: Added after the AWS SDK's built-in user agent middleware
  5. Format: Adds user agent string in format PT/<feature>/<version> PTEnv/<environment>

Integration with Powertools Utilities

This utility is used internally by other Powertools for AWS Lambda utilities:

  • Logger: Adds "PT/Logger/x.x.x" to user agent
  • Metrics: Adds "PT/Metrics/x.x.x" to user agent
  • Tracer: Adds "PT/Tracer/x.x.x" to user agent

When building custom utilities, use the same pattern to contribute to usage tracking.