Aws-sdk-js: CognitoIdentityServiceProvider.adminSetUserPassword is not a function in Lambda, how to change aws-sdk to a newer version in AWS Lambda to use it

Created on 22 May 2019  路  14Comments  路  Source: aws/aws-sdk-js

When using cognitoIdentityServiceProvider.adminSetUserPassword in the Lambda, got the following execution result

Response:
{
  "errorType": "TypeError",
  "errorMessage": "cognitoIdentityService.adminSetUserPassword is not a function",
  "trace": [
    "TypeError: cognitoIdentityService.adminSetUserPassword is not a function",
    "    at Runtime.exports.handler (/var/task/index.js:27:28)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:65:25)",
    "    at process._tickCallback (internal/process/next_tick.js:68:7)"
  ]
}

Request ID:
"a67215b9-7343-482e-8f61-9ba4e96632a1"

This is the code:

const AWS = require('aws-sdk');
const cognitoIdentityService = new AWS.CognitoIdentityServiceProvider({apiVersion: '2016-04-18', region: 'us-east-1'});

exports.handler = (event, context, callback) => {
    const userPoolId = event.userPoolId;
    const userName = event.userName;
    const password = event.password;

    if (!userPoolId) {
        return callback('Missing userPoolId in the request body.');
    }
    if (!userName) {
        return callback('Missing userName in the request body.');
    }
    if (!password) {
        return callback('Missing password in the request body.');
    }

    const params = {
       Password: password,
       Permanent: true,
       Username: userName,
       UserPoolId: userPoolId
    };

    cognitoIdentityService.adminSetUserPassword(params, (err, data) => {
        if (!err) {
            console.log('Successfull...');
            console.log(data);
            callback(null, {
                statusCode: 201,
            })
        } else {
            console.log('Error...');
            console.log(err, err.stack);
            callback(err);
        }
    });
};

Other Amazon Cognito Identity Provider services seem to work fine, I only get this error for adminSetUserPassword.

closing-soon guidance

Most helpful comment

Because of the package size, aws-sdk usually doesn't get shipped as part of the deployment. ( ie serverless project)
How often does lambda have its aws-sdk upgraded?

All 14 comments

@syqrefind update your aws-sdk to the latest version. The function was added as of version 2.449.0.

Confirmed that cognitoIdentityService.adminSetUserPassword was added in API 2.449.0
Changelog: https://github.com/aws/aws-sdk-js/blob/3f1e54f1da1b3a87f8ec19c8bb32ac1e66a7be75/.changes/2.449.0.json#L2-L6

Closing this issue, please reopen if you have any questions.

So since Lambda provides me with only the old version, how do I change it to the latest version? @trivikr

btw I don't think I can reopen this issue

Looks like GitHub doesn't have an option for original reporter to open closed issues.
This request has been open for 3+ years now, I've asked for an update https://github.com/isaacs/github/issues/583#issuecomment-495768980

@syqrefind update your aws-sdk to the latest version. The function was added as of version 2.449.0.

Hey @dsandor do you know how to upgrade the aws-sdk version in AWS Lambda Functions?

@syqrefind When you say you want to update the aws-sdk version in AWS Lambda Functions what exactly do you mean?

When executing a Lambda the version of the sdk is supplied by the dependencies you zipped up with your lambda code. So update the reference in your code bundle and that is what is used by the Lambda in the execution context.

So since it looks like you are using nodejs you would update your package.json like this:

npm i aws-sdk@latest

Then package and deploy the lambda and it will work.

I face the same problem, adminSetUserPassword work on local, but fail on lambda.
How do I know which version of sdk is running on lambda, seems serverless deploy striped the sdk and using the default sdk on lambda which is older than 2.xx .

Hi @syqrefind @lyh1win

The SDK bundled in Lambda runtime is 'partly' updated. Please refer to the lambda doc for more information. To access the newest SDK in Lambda, it's preferable to use the deployment package and bundle your local SDK.

Because of the package size, aws-sdk usually doesn't get shipped as part of the deployment. ( ie serverless project)
How often does lambda have its aws-sdk upgraded?

Hi @vanerleo, you can view AWS Lambda FAQs to find answers specific to AWS Lambda
There's a Contact Us link below

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

Because of the package size, aws-sdk usually doesn't get shipped as part of the deployment. ( ie serverless project)
How often does lambda have its aws-sdk upgraded?

You can actually deploy aws-sdk using serverless framework if you include it as a normal dependency ( not as a dev dependency). Basically you must do npm i aws-sdk@latest (if it i s already as a devDependecy just remove it before running npm i command).

Then when you run serverless deploy you will see something like this:

Serverless: Packing external modules: source-map-support@^0.4.18, aws-sdk@^2.499.0, babel-runtime@^6.26.0, uuid@^3.3.2

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.

Was this page helpful?
0 / 5 - 0 ratings