I usually have 'aws-sdk' as a dev dependency in package.json (to keep the zip file smaller). This has always worked fine, until now. I've been testing out WebSockets and kept getting a TypeError for a missing constructor on line 6:
"use strict";
const AWS = require('aws-sdk');
AWS.config.update({ region: process.env.AWS_REGION });
const DDB = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
const APIGW = new AWS.ApiGatewayManagementApi({apiVersion: '2018-11-29', endpoint: process.env.ENDPOINT}); // TypeError!
I have no issues running locally (i.e. the service is present in my node_modules directory).
Also note that there's no problem with DynamoDb on line 5.
I already deleted node_modules and package.lock and did a reinstall.
I tried referencing the service directly and that also didn't work:
require("aws-sdk/clients/apigatewaymanagementapi");
However, if I move 'aws-sdk' from 'devDependencies' to 'dependencies', everything runs without error on AWS.
Am I missing something here?
Hi @virtual-g, that's interesting that line 5 works with both devDependencies and dependencies but line 6 only works if aws-sdk is in dependencies. I'm not sure yet why that would be the case. I'll try to spend some time looking into that.
What is the value of your process.env.ENDPOINT? My instinct is that this could be the problem if it is not what is expected. There is also a good chance that this will conflict with your region that you have specified in your config from another region. You may not need to set the endpoint manually at all. What are you trying to achieve with it?
AWS_REGION is us-west-1.
ENDPOINT is set to "https://9w0jkjciw8.execute-api.us-west-1.amazonaws.com/dev"
SDK version is 2.398.
I initially suspected that ENDPOINT was the problem as well, but it is not. Two reasons:
1) Everything works fine when the sdk is a regular dependency.
2) I took endpoint out of line 6 and tried to construct the service, and it still didn't work.
Is there a way to log what services are available from the sdk after the import?
Hi @virtual-g, I have 2 questions:
Yes, running on Lambda with "aws-sdk" as a devDependency causes the problem. If the built-in sdk version is very behind then that would explain it. Any idea when the update will occur?
:+1: also ran into this issue today
Happens for me as well.
It's been a couple months since this issue was opened.
Any idea when this can get fixed? Our package sizes are getting affected due to this issue, and preventing deploying some critical fixes.
Lambda is still on v2.290.0. See Lambda Execution Environment and Available Libraries for the latest on which version is included.
ApiGatewayManagementApi was include in the SDK beginning in v2.379.0.
The best practices recommendation is to bundle your preferred version of the SDK. Lambda does make periodic updates to the SDK, but those updates are not guaranteed on any specific timeline and the SDK is provided for convenience.
There is a discussion here regarding managing more recent versions of the SDK as Layers: https://github.com/aws/aws-sdk-js/issues/2528
@alpeshgaglani would that approach help with your deployment issue?
It seems like the only good option is to re-architect, and split out our code into a much more granular modules. Not to mention the unavoidable latencies that would be added. It sucks to re-architect based on deployment considerations, though :(.
I couldn't afford to include the full aws-sdk module in my build.
So I created this module aws-apigatewaymanagementapi
It adds the ApiGatewayManagementApi constructor to Lambda's provided aws-sdk module.
It's exactly the same implementation mechanism as the one from aws-sdk-js (it's basically Copy & Paste from it).
To use it:
const ensureApiGatewayManagementApi = require('aws-apigatewaymanagementapi')
const AWS = require('aws-sdk'); // aws-sdk v2.290.0 provided by AWS
ensureApiGatewayManagementApi(AWS)
// You can now use ApiGatewayManagementApi, for example:
const managementApi = new AWS.ApiGatewayManagementApi({
apiVersion: '2018-11-29',
});
Hope it helps!
Just encountered this issue today. No solution?
Hey everyone, this issue can be largely associated with Lambda running on the older version.
You can import the latest version of SDK by following
these guidelines.
I will go ahead and close the issue because of inactivity, please reach out and follow the issue template if anyone has any questions.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.
Most helpful comment
I couldn't afford to include the full
aws-sdkmodule in my build.So I created this module aws-apigatewaymanagementapi
It adds the
ApiGatewayManagementApiconstructor to Lambda's providedaws-sdkmodule.It's exactly the same implementation mechanism as the one from aws-sdk-js (it's basically Copy & Paste from it).
To use it:
Hope it helps!