Skip to content

CredentialsProviderError using ApiGatewayManagementApiClient #1864

Description

@jamesstidard

Bug Report

Current Behavior

I'm trying to get my web sockets to work offline, but I get a CredentialsProviderError when trying to send a message using the ApiGatewayManagementApiClient.

It's probably just using the wrong endpoint in the config (like how the offline DynamoDB works), but I'm not sure what I'm getting slightly wrong.

Would really appreciate any help.

Sample Code

  • file: serverless.yml
org: myorg
app: sls-ws
service: sls-ws

plugins:
  - serverless-offline

provider:
  name: aws
  runtime: nodejs20.x

functions:
  ws_connect:
    handler: handler.connect
    events:
      - websocket: $connect
  ws_message:
    handler: handler.message
    events:
      - websocket: $default
  ws_disconnect:
    handler: handler.disconnect
    events:
      - websocket: $disconnect
  • file: handler.js
const {ApiGatewayManagementApiClient} = require("@aws-sdk/client-apigatewaymanagementapi");
const {PostToConnectionCommand} = require("@aws-sdk/client-apigatewaymanagementapi");

exports.connect = async (event, context) => {
  return {statusCode: 200, body: null};
};

exports.message = async (event, context) => {
  const endpoint = "http://localhost:3001";
  const connectionId = event.requestContext.connectionId;
  const payload = {ConnectionId: connectionId, Data: "pong"};

  const apiGatewayClient = new ApiGatewayManagementApiClient({ endpoint });
  const command = new PostToConnectionCommand(payload);
  await apiGatewayClient.send(command);  // <-- ERRORS HERE
};

exports.disconnect = async (event, context) => {
  return {statusCode: 200, body: null};
};
Starting Offline at stage dev (us-east-1)

Offline [http for lambda] listening on http://localhost:3002
Function names exposed for local invocation by aws-sdk:
   * ws_connect: sls-ws-dev-ws_connect
   * ws_message: sls-ws-dev-ws_message
   * ws_disconnect: sls-ws-dev-ws_disconnect
route '$connect (λ: ws_connect)'
route '$default (λ: ws_message)'
route '$disconnect (λ: ws_disconnect)'
Offline [websocket] listening on ws://localhost:3001
Offline [http for websocket] listening on http://localhost:3001
(λ: ws_connect) RequestId: 773ac6bb-b34f-4bff-83b6-feb27996589d  Duration: 86.82 ms  Billed Duration: 87 ms
received connection
✖ Unhandled exception in handler 'ws_message'.
✖ CredentialsProviderError: Could not load credentials from any providers
    at ./node_modules/.pnpm/@aws-sdk+credential-provider-node@3.901.0/node_modules/@aws-sdk/credential-provider-node/dist-cjs/index.js:129:13
    at ./node_modules/.pnpm/@smithy+property-provider@4.2.0/node_modules/@smithy/property-provider/dist-cjs/index.js:50:39
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async coalesceProvider (./node_modules/.pnpm/@smithy+property-provider@4.2.0/node_modules/@smithy/property-provider/dist-cjs/index.js:76:24)
    at async ./node_modules/.pnpm/@smithy+property-provider@4.2.0/node_modules/@smithy/property-provider/dist-cjs/index.js:95:24
    at async ./node_modules/.pnpm/@smithy+core@3.14.0/node_modules/@smithy/core/dist-cjs/index.js:62:23
    at async ./node_modules/.pnpm/@aws-sdk+middleware-logger@3.901.0/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:33:22
    at async exports.message (./handler.js:15:3)
    at async MessagePort.<anonymous> (file://./node_modules/.pnpm/serverless-offline@14.4.0_serverless@4.20.2/node_modules/serverless-offline/src/lambda/handler-runner/worker-thread-runner/workerThreadHelper.js:24:14)
✖ CredentialsProviderError: Could not load credentials from any providers
    at ./node_modules/.pnpm/@aws-sdk+credential-provider-node@3.901.0/node_modules/@aws-sdk/credential-provider-node/dist-cjs/index.js:129:13
    at ./node_modules/.pnpm/@smithy+property-provider@4.2.0/node_modules/@smithy/property-provider/dist-cjs/index.js:50:39
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async coalesceProvider (./node_modules/.pnpm/@smithy+property-provider@4.2.0/node_modules/@smithy/property-provider/dist-cjs/index.js:76:24)
    at async ./node_modules/.pnpm/@smithy+property-provider@4.2.0/node_modules/@smithy/property-provider/dist-cjs/index.js:95:24
    at async ./node_modules/.pnpm/@smithy+core@3.14.0/node_modules/@smithy/core/dist-cjs/index.js:62:23
    at async ./node_modules/.pnpm/@aws-sdk+middleware-logger@3.901.0/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:33:22
    at async exports.message (./handler.js:15:3)
    at async MessagePort.<anonymous> (file://./node_modules/.pnpm/serverless-offline@14.4.0_serverless@4.20.2/node_modules/serverless-offline/src/lambda/handler-runner/worker-thread-runner/workerThreadHelper.js:24:14)

Expected behavior/code

Environment

  • serverless version: v4.20.2
  • serverless-offline version: v14.4.0
  • node.js version: 🤷‍♂️
  • OS: macOS 26.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions