dherault / dherault/serverless-offline

CredentialsProviderError using ApiGatewayManagementApiClient

Open
#1,864 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
5.3k
Forks
811
Avg merge
2d 4h
Merged PRs (30d)
3

Description

## 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

```yaml
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

```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};
};
```

```console
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. (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. (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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.