#Amplify Gen 2 Function with DynamoDb runs in Sandbox but fails to deploy

5 messages · Page 1 of 1 (latest)

hushed viper
#

I have a Gen 2 Amplify function that is working in sandbox but failing when deploying.

My handler.ts starts with ......

import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DynamoDBDocument } from "@aws-sdk/lib-dynamodb"; import { ApiGatewayManagementApiClient, PostToConnectionCommand, } from "@aws-sdk/client-apigatewaymanagementapi"; import { CognitoJwtVerifier } from "aws-jwt-verify";

and have package.json
{ "name": "collaborationhandler", "type": "module", "dependencies": { "@aws-sdk/client-apigatewaymanagementapi": "^3.726.1", "@aws-sdk/client-dynamodb": "^3.726.1", "@aws-sdk/lib-dynamodb": "^3.726.1", "aws-jwt-verify": "^5.0.0" } }

This works in the Sandbox but fails in deploy with the following errors having added --debug to the build step.

[DEBUG] 2025-01-16T05:01:33.344Z: ✘ [ERROR] Could not resolve "@aws-sdk/client-dynamodb" amplify/functions/collaborationHandler/handler.ts:1:31: 1 │ import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; ╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~ You can mark the path "@aws-sdk/client-dynamodb" as external to exclude it from the bundle, which will remove this error and leave the unresolved path in the bundle.

And similar messages for the other imports.

I added exclude "amplify.." in my amplify/tsconfig from this https://docs.amplify.aws/react/build-a-backend/troubleshooting/cannot-find-module-amplify-env/ but that didn't help.

I'd really appreciate any help and apologies if I made any mistakes, this is my first time posting here.

fallen galleon
#

Hey, as a quick check were the dependencies listed in package.json installed under the amplify folder's package.json?
if yes, i would suggest removing these and installing at the project root package.json

hushed viper
#

I’ll give that a go. I’ve put them in the amplify folder and tried adding a package.JSON within the function folder also. Thanks for the suggestion

fallen galleon
#

hey, when deploying amplify will bundle the dependency installed at the root of the project. installing at the root should mitigate the issue

hushed viper
#

Thank you this worked! I thought I’d read somewhere there sdk was installed by default into the image. Guess I was mistaken. Another quick question, how can I ensure I’m running all appropriate linter / other checks before I sync to deploy? Is there something I can add as a git pre commit hook? Thanks again for your help.