问题
I am trying to use aws-sdk inside lambda but I can't seem to figure it out.
var AWS = require('aws-sdk');
AWS.config.update();
var DDB = new AWS.DynamoDB({ apiVersion: "2012-10-08" });
exports.handler = function (event, context, callback) {
var url_handler = event.requestContext.domainName + "/" + event.requestContext.stage;
var scanParams = {
TableName: "tbl-web-socket-connection",
ProjectionExpression: "id"
};
DDB.scan(scanParams, function (err, data) {
console.log(err, "Error");
if (err) {
callback(null, {
statusCode: 500,
body: JSON.stringify(err)
});
} else {
console.log(AWS, "AWSSS");
var apigwManagementApi = new AWS.ApiGatewayManagementApi({
apiVersion: "2018-11-29",
endpoint: event.requestContext.domainName + "/" + event.requestContext.stage
});
}
});
};
This is what I declaered on the lambda function, but it gives giving me the error "AWS.ApiGatewayManagementApi is not a constructor at Response." on the cloud watch.
Did I miss something? Like maybe including the aws-sdk on the lambda function itself?
Edit: Updated to display the whole lambda function
回答1:
As of May 15, 2019 you can run the Lambda as node version 10.x and the ApiGatewayManagementApi is included in the aws-sdk by default.
https://aws.amazon.com/about-aws/whats-new/2019/05/aws_lambda_adds_support_for_node_js_v10/
回答2:
To those people who have the same problem as me
It looks like that the version of “aws-sdk” available in lambda execution is 2.29 ApiGatewayManagementApi is added in 2.379
That's why it cannot call the constructor.
To solve this problem, I created a layer with the aws-sdk with it and add those layer to the lambda function.
回答3:
I just ran into a the same error message however my problem was that in my SAM Template I scoped my Lambda CodeUri wrong and thus the node_modules folder was not being deployed with the lambda.
I had my lambda code in a sub directory of my project.
This was my WRONG definition:
GetUserFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.getHandler
Timeout: 30
Runtime: nodejs8.10
CodeUri: ./user
Changing it to this fixed it:
GetUserFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.getHandler
Timeout: 30
Runtime: user/nodejs8.10
CodeUri: ./
来源:https://stackoverflow.com/questions/54781774/using-aws-sdk-inside-lambda-aws-apigatewaymanagementapi-is-not-a-constructor-a