aws-sdk-v3 failing

0

For the past one week, I have been trying to deploy an amplify app using cognito services and the aws-sdk for IoT, but no head-way.

I have previously deployed same using the version 2.

Recently, I cannot anymore because the cloud stack formation enforces the use of version 3.

I have taken the initiative to update my repo to version 3, updated my Runtime environment: to node 18x in my yml file, yet it still failing. Below is where I usually get stuck during stack formation:

UserPoolClientInputs:
    Type: Custom::LambdaCallout
    Properties:
      ServiceToken:
        Fn::GetAtt:
          - UserPoolClientLambda
          - Arn
      clientId:
        Ref: UserPoolClient
      userpoolId:
        Ref: UserPool
    DependsOn: UserPoolClientLogPolicy

and then I receive the following log error from the cloudWatch:

> 2024-05-09T11:14:46.811Z undefined ERROR Uncaught Exception {
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'aws-sdk'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/index.mjs",
"stack": [
"Runtime.ImportModuleError: Error: Cannot find module 'aws-sdk'",
"Require stack:",
"- /var/task/index.js",
"- /var/runtime/index.mjs",
" at _loadUserApp (file:///var/runtime/index.mjs:1087:17)",
" at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)",
" at async start (file:///var/runtime/index.mjs:1282:23)",
" at async file:///var/runtime/index.mjs:1288:1"
]}

Looking at my yml file that request the aws-sdk is as below:

    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile:
          Fn::Join:
            - ''
            - - const response = require('cfn-response');
              - const aws = require('aws-sdk');
              - const identity = new aws.CognitoIdentityServiceProvider();
              - exports.handler = (event, context, callback) => {
              - ' if (event.RequestType == ''Delete'') { '
              - '   response.send(event, context, response.SUCCESS, {})'
              - ' }'
              - ' if (event.RequestType == ''Update'' || event.RequestType == ''Create'') {'
              - '   const params = {'
              - '     ClientId: event.ResourceProperties.clientId,'
              - '     UserPoolId: event.ResourceProperties.userpoolId'
              - '   };'
              - '   identity.describeUserPoolClient(params).promise()'
              - '     .then((res) => {'
              - '       response.send(event, context, response.SUCCESS, {''appSecret'': res.UserPoolClient.ClientSecret});'
              - '     })'
              - '     .catch((err) => {'
              - '       response.send(event, context, response.FAILURE, {err});'
              - '     });'
              - ' }'
              - '};'
      Handler: index.handler
      Runtime: nodejs18.x
      Timeout: '300'
      Role:
        Fn::GetAtt:
          - UserPoolClientRole
          - Arn
    DependsOn: UserPoolClientRole```

I need a guide on the best approach to make it work. I already have installed aws-sdk v3 as a dependency below:

dependencies: {
    "@aws-sdk/client-cloudformation": "^3.569.0",
    "@aws-sdk/client-s3": "^3.569.0",
    "@testing-library/jest-dom": "^5.17.0",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "aws-amplify": "^1.3.3",
    "aws-amplify-react": "^2.6.3",
    "aws-iot-device-sdk": "^2.2.13"
}

your help is highly anticipated.

Thanks

  • Can you try updating the function in the yaml file to use v3 equivalent instead of v2?

1 Answer
0
Accepted Answer

You are using the NodeJS 18.x runtime which has AWS SDK for JS version 3 installed by default [1]. Version 3 is a major rewrite of the SDK which will require changes to your Lambda function code. To help with this, I would recommend reviewing the documentation on migrating from V2 to V3 of the SDK [2]. It describe the major differences and also walks you through how to use aws-sdk-js-codemod [3] to help with the migration.

[1] https://aws.amazon.com/blogs/compute/node-js-18-x-runtime-now-available-in-aws-lambda/

[2] https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/migrating.html

[3] https://www.npmjs.com/package/aws-sdk-js-codemod

profile pictureAWS
answered 14 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions