Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build Error: Cannot find module '@aws-sdk/client-ses' and 'googleapis' in Lambda function with layers (Amplify Gen 2) #2395

Open
mikedorsey1021 opened this issue Jan 2, 2025 · 7 comments
Labels
function Issue pertaining to Amplify Function pending-response Issue is pending response from author pending-triage Incoming issues that need categorization

Comments

@mikedorsey1021
Copy link

Environment information

System:
  OS: macOS 15.1.1
  CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  Memory: 137.49 MB / 32.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 20.15.0 - ~/.nvm/versions/node/v20.15.0/bin/node
  Yarn: 1.22.17 - /usr/local/bin/yarn
  npm: 10.7.0 - ~/.nvm/versions/node/v20.15.0/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/auth-construct: 1.3.1
  @aws-amplify/backend: 1.5.0
  @aws-amplify/backend-auth: 1.2.0
  @aws-amplify/backend-cli: 1.2.9
  @aws-amplify/backend-data: 1.1.4
  @aws-amplify/backend-deployer: 1.1.5
  @aws-amplify/backend-function: 1.7.0
  @aws-amplify/backend-output-schemas: 1.3.0
  @aws-amplify/backend-output-storage: 1.1.2
  @aws-amplify/backend-secret: 1.1.4
  @aws-amplify/backend-storage: 1.2.1
  @aws-amplify/cli-core: 1.1.3
  @aws-amplify/client-config: 1.4.0
  @aws-amplify/deployed-backend-client: 1.4.2
  @aws-amplify/form-generator: 1.0.3
  @aws-amplify/model-generator: 1.0.8
  @aws-amplify/platform-core: 1.1.0
  @aws-amplify/plugin-types: 1.3.0
  @aws-amplify/sandbox: 1.2.3
  @aws-amplify/schema-generator: 1.2.4
  aws-amplify: 6.6.6
  aws-cdk: 2.163.1
  aws-cdk-lib: 2.163.1
  typescript: 5.6.3
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Describe the bug

I'm encountering a persistent build error in my Amplify Gen 2 project when deploying to production via the Amplify console. The build fails with a TypeScript validation check failed error, indicating that the modules @aws-sdk/client-ses and googleapis cannot be found in my Lambda function (sendEmailReceipt) even though I'm using Lambda layers to provide these dependencies.

Error Message (from Amplify Console build log):

SyntaxError: TypeScript validation check failed. Resolution: Fix the syntax and type errors in your backend definition. Cause: amplify/functions/send-email-receipt/handler.ts(2,45): error TS2307: Cannot find module '@aws-sdk/client-ses' or its corresponding type declarations. amplify/functions/send-email-receipt/handler.ts(3,24): error TS2307: Cannot find module 'googleapis' or its corresponding type declarations.

amplify/functions/send-email-receipt/handler.ts

import type { Handler } from 'aws-lambda';
import { google } from 'googleapis';
import { SESClient, SendEmailCommand } from '@aws-sdk/client-ses';



const ses = new SESClient({ region: 'us-east-2' });
const sheets = google.sheets('v4');

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const handler: Handler = async (event: any) => {
  const { name, email, message } = event.arguments;
  const API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  const SPREADSHEET_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

  try {
    // Send confirmation email
    const emailCommand = new SendEmailCommand({
      Destination: { ToAddresses: [email] },
      Message: {
        Body: { Text: { Data: `Thank you for your prayer request, ${name}` } },
        Subject: { Data: 'Prayer Request Received' }
      },
      Source: '[email protected]'
    });

    await ses.send(emailCommand);

    // Append to Google Sheet
    await sheets.spreadsheets.values.append({
      spreadsheetId: SPREADSHEET_ID,
      range: 'Sheet1!A:D',
      key: API_KEY,
      valueInputOption: 'USER_ENTERED',
      requestBody: {
        values: [[new Date().toISOString(), name, email, message]]
      }
    });

    return `Request received and processed`;
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
};

The error occurs both in the Amplify Sandbox and in the production build on the Amplify console.

Expected behavior:

The Amplify build should succeed, and the sendEmailReceipt Lambda function should be able to access the googleapis and @aws-sdk/client-ses modules from the layers during runtime.

Actual behavior:

The build fails with the TypeScript validation check failed error, indicating that the modules cannot be found.

Reproduction steps

Reproduction steps

  1. Create an Amplify Gen 2 project.
  2. Create a Lambda function named sendEmailReceipt.
  3. Create two Lambda layers:
    • googleapis-layer: Contains the googleapis npm package.
    • ses-layer: Contains the @aws-sdk/client-ses npm package.
  4. Reference these layers in the defineFunction construct for sendEmailReceipt .
  5. Push the changes to trigger a build in the Amplify console.

Code Snippets:

amplify/functions/send-email-receipt/resource.ts:

import { defineFunction} from '@aws-amplify/backend';

export const sendEmailReceipt = defineFunction({
  name: 'sendEmailReceipt',
  entry: './handler.ts',
  layers: {
    "@aws-sdk/client-ses": "arn:aws:lambda:us-east-2:xxxxxxxxxxxx:layer:client_ses:2",
    "googleapis": "arn:aws:lambda:us-east-2:xxxxxxxxxxxx:layer:googleapis:2"
  }
});
});

Additional Notes:

  • I have tried it without the layers and adding the node_modules to the build and get the same error.
  • I have tried both npm ci and npm install in the amplify.yml file.
  • I have tried clearing the Amplify console build cache and triggering clean builds.
  • The issue occurs both in the sandbox and in production builds.

I suspect there might be a bug or a specific configuration issue related to how Amplify Gen 2 handles Lambda layers during the production build process. Any help or guidance would be greatly appreciated. It seems it doesn't play nicely with outside dependencies regardless of how you import them and try and use it.

@mikedorsey1021 mikedorsey1021 added the pending-triage Incoming issues that need categorization label Jan 2, 2025
@ykethan
Copy link
Member

ykethan commented Jan 3, 2025

Hey @mikedorsey1021, thank you for reaching out. This appears to be typescript error on a deploy.
Are the dependencies @aws-sdk/client-ses and googleapis installed locally in the project root package.json?

The key (module name) is used to externalize the locally installed module dependency so it doesn't get bundled with your lambda function. Once deployed to the cloud the Lambda function will refer to the Lambda layer for your dependencies.

@ykethan ykethan added pending-response Issue is pending response from author function Issue pertaining to Amplify Function labels Jan 3, 2025
@mikedorsey1021
Copy link
Author

Hey @mikedorsey1021, thank you for reaching out. This appears to be typescript error on a deploy. Are the dependencies @aws-sdk/client-ses and googleapis installed locally in the project root package.json?

The key (module name) is used to externalize the locally installed module dependency so it doesn't get bundled with your lambda function. Once deployed to the cloud the Lambda function will refer to the Lambda layer for your dependencies.

I did install it locally as you suggested, thank you for that. After doing so, unfortunately it lands me back to an original error which is

2025-01-03T17:28:06.898Z [INFO]: # Executing command: npx ampx pipeline-deploy --branch $AWS_BRANCH --app-id $AWS_APP_ID

53

2025-01-03T17:28:24.324Z [INFO]:

54

2025-01-03T17:28:24.328Z [INFO]: SyntaxError: Unable to build the Amplify backend definition.

55

Resolution: Check your backend definition in the `amplify` folder for syntax and type errors.

56

Cause: SyntaxError: The requested module '../functions/send-email-receipt/resource' does not provide an export named 'sendEmailReceipt'

57

at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)

58

2025-01-03T17:28:24.328Z [WARNING]: ampx pipeline-deploy

59

Command to deploy backends in a custom CI/CD pipeline. This command is not inten

60

ded to be used locally.

61

Options:

62

--debug Print debug logs to the console [boolean] [default: false]

63

--help Show help [boolean]

64

--branch Name of the git branch being deployed [string] [required]

65

--app-id The app id of the target Amplify app [string] [required]

66

--outputs-out-dir A path to directory where amplify_outputs is written. If no

67

t provided defaults to current process working directory.

68

[string]

69

--outputs-version Version of the configuration. Version 0 represents classic

70

amplify-cli config file amplify-configuration and 1 represe

71

nts newer config file amplify_outputs

72

[string] [choices: "0", "1", "1.1", "1.2"] [default: "1.2"]

73

--outputs-format amplify_outputs file format

74

[string] [choices: "mjs", "json", "json-mobile", "ts", "dart"]

75

2025-01-03T17:28:24.528Z [ERROR]: !!! Build failed

76

2025-01-03T17:28:24.528Z [ERROR]: !!! Error: Command failed with exit code 1

77

2025-01-03T17:28:24.528Z [INFO]: # Starting environment caching...

78

2025-01-03T17:28:24.529Z [INFO]: # Uploading environment cache artifact...

79

2025-01-03T17:28:24.604Z [INFO]: # Uploaded environment cache artifact

80

2025-01-03T17:28:24.605Z [INFO]: # Environment caching completed

@github-actions github-actions bot removed the pending-response Issue is pending response from author label Jan 3, 2025
@ykethan
Copy link
Member

ykethan commented Jan 3, 2025

Hey @mikedorsey1021, the deploy appears to have failed due The requested module '../functions/send-email-receipt/resource' does not provide an export named 'sendEmailReceipt'.

Are you experiencing this error on a sandbox deploy as well? are there any changes on amplify/functions/send-email-receipt/resource.ts previously provided?

@ykethan ykethan added the pending-response Issue is pending response from author label Jan 3, 2025
@mikedorsey1021
Copy link
Author

Hey @mikedorsey1021, the deploy appears to have failed due The requested module '../functions/send-email-receipt/resource' does not provide an export named 'sendEmailReceipt'.

Are you experiencing this error on a sandbox deploy as well? are there any changes on amplify/functions/send-email-receipt/resource.ts previously provided?

No changes on the resource.ts previously provided. I am not experiencing this in sandbox. This is only in the Amplify console production build.

@github-actions github-actions bot removed the pending-response Issue is pending response from author label Jan 3, 2025
@ykethan
Copy link
Member

ykethan commented Jan 3, 2025

@mikedorsey1021 noticed @aws-amplify/backend and @aws-amplify/backend-cli does appear to be out of date. Could you upgrade to the latest versions which should @aws-amplify/backend: 1.12.0 and @aws-amplify/backend-cli: 1.4.6, then pushing the updated package-lock.json to start a new build.

additionally, tried reproducing the deploy issue using the following but did not observe an error message

// handler.ts

import { SESClient, SendEmailCommand } from "@aws-sdk/client-ses";
import type { Handler } from "aws-lambda";
import { google } from "googleapis";

const ses = new SESClient({ region: "us-east-2" });
const sheets = google.sheets("v4");

export const handler: Handler = async (event) => {
  const { name, email, message } = event.arguments;
  const API_KEY =
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  const SPREADSHEET_ID =
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

  try {
    // Send confirmation email
    const emailCommand = new SendEmailCommand({
      Destination: { ToAddresses: [email] },
      Message: {
        Body: { Text: { Data: `Thank you for your prayer request, ${name}` } },
        Subject: { Data: "Prayer Request Received" },
      },
      Source: "[email protected]",
    });

    await ses.send(emailCommand);

    // Append to Google Sheet
    await sheets.spreadsheets.values.append({
      spreadsheetId: SPREADSHEET_ID,
      range: "Sheet1!A:D",
      key: API_KEY,
      valueInputOption: "USER_ENTERED",
      requestBody: {
        values: [[new Date().toISOString(), name, email, message]],
      },
    });

    return `Request received and processed`;
  } catch (error) {
    console.error("Error:", error);
    throw error;
  }
};

```

```
//resource.ts

import { defineFunction} from '@aws-amplify/backend';

export const sendEmailReceipt = defineFunction({
  name: 'sendEmailReceipt',
  entry: './handler.ts',
  layers: {
    "@aws-sdk/client-ses": "<arn>",
    "googleapis": "<arn>"
  }
});
```

```
// amplify/auth/resource.ts
import { defineAuth } from "@aws-amplify/backend";
import { sendEmailReceipt } from "../functions/send-email-receipt/resource";
import { postConfirmation } from "./post-confirmation/resource";

export const auth = defineAuth({
  loginWith: {
    email: true,
  },
  triggers: {
    postConfirmation,
  },

  senders: {
    email: {
      handler: sendEmailReceipt,
    },
  },
});

```

@ykethan ykethan added the pending-response Issue is pending response from author label Jan 3, 2025
@mikedorsey1021
Copy link
Author

I did try updating the packages but was not able to get a different result. Is there anyway I can share the the repo with you? Its private so I'll have to add you.

@github-actions github-actions bot removed the pending-response Issue is pending response from author label Jan 3, 2025
@ykethan
Copy link
Member

ykethan commented Jan 4, 2025

@mikedorsey1021 you could provide us a link the repository with a minimal reproduction.
or you could try zipping the amplify folder and sending this over a discord DM(my handle is ykethan). temporary access to the repository should also be fine.

@ykethan ykethan added the pending-response Issue is pending response from author label Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
function Issue pertaining to Amplify Function pending-response Issue is pending response from author pending-triage Incoming issues that need categorization
Projects
None yet
Development

No branches or pull requests

2 participants