aws-amplify / amplify-backend

Home to all tools related to Amplify's code-first DX (Gen 2) for building fullstack apps on AWS
Apache License 2.0
185 stars 62 forks source link

aws ampligy gen2 build error #1961

Closed gambithunt closed 2 months ago

gambithunt commented 2 months ago

Environment information

Hi, I've build exactly two functions and a basic auth, all works fine in the sandbox.

when I try deply to the app for the first time to amplify I get a build error related to the backend. See below.

When checking the backend code there are no typescript errors and it works fine in the sandbox, the error also does not stipulate where there error is caused, it simply refers to the " backend definition"

Please assist me in trouble shooting this.

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

56 2024-09-05T07:19:12.132Z [INFO]: SyntaxError: TypeScript validation check failed.
57 Resolution: Fix the syntax and type errors in your backend definition.

58 2024-09-05T07:19:12.132Z [INFO]: Cause:
59 2024-09-05T07:19:12.244Z [ERROR]: !!! Build failed
60 2024-09-05T07:19:12.244Z [ERROR]: !!! Error: Command failed with exit code 1
61 2024-09-05T07:19:12.245Z [INFO]: # Starting environment caching...
62 2024-09-05T07:19:12.245Z [INFO]: # Environment caching completed

Describe the bug

There are no typescript errors in the IDE and in the sandbox but the backend seems to be detecting one and does not specify where it is.

Reproduction steps

It's a first time deploy of the very beginnings of a vue app.

ykethan commented 2 months ago

Hey @gambithunt, thank you for reaching out. In the hosting -> build setting amplify.yml, could you add --debug to the deploy command and provide us the output?

npx ampx pipeline-deploy --branch $AWS_BRANCH --app-id $AWS_APP_ID --debug
gambithunt commented 2 months ago

Hi, thanks for getting back to me, please see below the output

2024-09-05T13:57:18.772Z [INFO]: ## Starting Backend Build

12

# Starting phase: build

13

# Executing command: npm ci --cache .npm --prefer-offline

14

2024-09-05T13:57:53.307Z [WARNING]: npm WARN deprecated rimraf@3.0.2: Rimraf versions prior to v4 are no longer supported

15

2024-09-05T13:57:54.264Z [WARNING]: npm WARN deprecated inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.

16

2024-09-05T13:57:56.347Z [WARNING]: npm WARN deprecated

17

2024-09-05T13:57:56.348Z [WARNING]: @babel/plugin-proposal-object-rest-spread@7.20.7: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-object-rest-spread instead.

18

2024-09-05T13:57:56.348Z [WARNING]: npm WARN

19

2024-09-05T13:57:56.349Z [WARNING]: deprecated @babel/plugin-proposal-class-properties@7.18.6: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-class-properties instead.

20

2024-09-05T13:57:58.329Z [WARNING]: npm WARN

21

2024-09-05T13:57:58.334Z [WARNING]: deprecated glob@7.2.3: Glob versions prior to v9 are no longer supported

22

2024-09-05T13:58:02.282Z [WARNING]: npm WARN deprecated glob@7.2.3: Glob versions prior to v9 are no longer supported

23

2024-09-05T13:58:10.225Z [WARNING]: npm WARN deprecated core-js@2.6.12: core-js@<3.23.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Some versions have web compatibility issues. Please, upgrade your dependencies to the actual version of core-js.

24

2024-09-05T13:58:12.902Z [INFO]: added 1452 packages, and audited 1569 packages in 41s

25

2024-09-05T13:58:12.903Z [INFO]: 187 packages are looking for funding

26

run `npm fund` for details

27

2024-09-05T13:58:12.934Z [INFO]: 8 vulnerabilities (1 moderate, 7 high)

28

To address all issues, run:

29

npm audit fix

30

Run `npm audit` for details.

31

2024-09-05T13:58:12.969Z [INFO]: # Executing command: npx ampx pipeline-deploy --branch $AWS_BRANCH --app-id $AWS_APP_ID --debug

32

2024-09-05T13:58:20.499Z [INFO]: [DEBUG] 2024-09-05T13:58:20.499Z: Bundling asset amplify-d3hdj15s3omqag-main-branch-cb4fc39d52/AmplifyBranchLinker/CustomResourceLambda/Code/Stage...

33

2024-09-05T13:58:20.507Z [INFO]:

34

2024-09-05T13:58:21.008Z [INFO]: [DEBUG] 2024-09-05T13:58:21.007Z:

35

...4b4cbf8c9b8eeddb568701c18403a6ce3863e10b9d12fb8effe7fb2/index.js 1.0mb ⚠️

36

⚡ Done in 75ms

37

2024-09-05T13:58:21.008Z [INFO]:

38

2024-09-05T13:58:22.779Z [INFO]: [DEBUG] 2024-09-05T13:58:22.779Z: Bundling asset amplify-d3hdj15s3omqag-main-branch-cb4fc39d52/auth/SecretFetcherResourceProviderLambda/Code/Stage...

39

2024-09-05T13:58:22.779Z [INFO]:

40

2024-09-05T13:58:23.232Z [INFO]: [DEBUG] 2024-09-05T13:58:23.232Z:

41

...0f9b6bf0db25fa48b90c4f7e7b34e14cc2e18bcfb93243f1faa8b6f/index.js 240.3kb

42

⚡ Done in 21ms

43

2024-09-05T13:58:23.232Z [INFO]:

44

2024-09-05T13:58:23.293Z [INFO]: [DEBUG] 2024-09-05T13:58:23.293Z: Bundling asset amplify-d3hdj15s3omqag-main-branch-cb4fc39d52/function/strava-refresh-lambda/Code/Stage...

45

2024-09-05T13:58:23.294Z [INFO]:

46

2024-09-05T13:58:23.739Z [INFO]: [DEBUG] 2024-09-05T13:58:23.739Z: ✘ [ERROR] Could not resolve "axios"

47

amplify/functions/strava-refresh/handler.ts:3:18:

48

3 │ import axios from "axios";

49

╵ ~~~~~~~

50

You can mark the path "axios" as external to exclude it from the bundle, which will remove this error and leave the unresolved path in the bundle.

51

2024-09-05T13:58:23.746Z [INFO]:

52

2024-09-05T13:58:23.870Z [INFO]: [DEBUG] 2024-09-05T13:58:23.869Z: 1 error

53

2024-09-05T13:58:23.893Z [INFO]: [DEBUG] 2024-09-05T13:58:23.893Z: /codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-function/src/factory.ts:365

54

throw new AmplifyUserError(

55

^

56

AmplifyError [NodeJSFunctionConstructInitializationError]: Failed to instantiate nodejs function construct

57

at AmplifyFunction (/codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-function/src/factory.ts:365:13)

58

at FunctionGenerator.generateContainerEntry (/codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-function/src/factory.ts:282:12)

59

at SingletonConstructContainer.getOrCompute (/codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend/src/engine/singleton_construct_container.ts:51:19)

60

at FunctionFactory.getInstance (/codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-function/src/factory.ts:153:31)

61

at <anonymous> (/codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-data/src/convert_functions.ts:18:21)

62

at Array.map (<anonymous>)

63

at convertFunctionNameMapToCDK (/codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-data/src/convert_functions.ts:16:31)

64

at DataGenerator.generateContainerEntry (/codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-data/src/factory.ts:229:29)

65

at SingletonConstructContainer.getOrCompute (/codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend/src/engine/singleton_construct_container.ts:51:19)

66

at DataFactory.getInstance (/codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-data/src/factory.ts:109:31) {

67

cause: Error: Failed to bundle asset amplify-d3hdj15s3omqag-main-branch-cb4fc39d52/function/strava-refresh-lambda/Code/Stage, bundle output is located at /codebuild/output/src2174721483/src/staza/.amplify/artifacts/cdk.out/bundling-temp-a967ca1b8f29527dd9c4cbf9ee4d5e5fb3e97e0112872a5c72a8b7cf41793efa-error: Error: bash -c npx --no-install esbuild --bundle "/codebuild/output/src2174721483/src/staza/amplify/functions/strava-refresh/handler.ts" --target=node18 --platform=node --format=esm --outfile="/codebuild/output/src2174721483/src/staza/.amplify/artifacts/cdk.out/bundling-temp-a967ca1b8f29527dd9c4cbf9ee4d5e5fb3e97e0112872a5c72a8b7cf41793efa/index.mjs" --minify --sourcemap --loader:.node=file --banner:js="/** * Reads SSM environment context from a known Amplify environment variable, * fetches values from SSM and places those values in the corresponding environment variables */export const internalAmplifyFunctionResolveSsmParams = async (client) => { const envPathObject = JSON.parse(process.env.AMPLIFY_SSM_ENV_CONFIG ?? '{}'); const paths = Object.keys(envPathObject); if (paths.length === 0) { return; } let actualSsmClient; if (client) { actualSsmClient = client; } else { const ssmSdk = await import('@aws-sdk/client-ssm'); actualSsmClient = new ssmSdk.SSM(); } const resolveSecrets = async (paths) => { const response = await actualSsmClient.getParameters({ Names: paths, WithDecryption: true, }); if (response.Parameters && response.Parameters.length > 0) { for (const parameter of response.Parameters) { if (parameter.Name) { const envKey = Object.keys(envPathObject).find((key) => envPathObject[key].sharedPath === parameter.Name); const envName = envKey ? envPathObject[envKey].name : envPathObject[parameter.Name]?.name; process.env[envName] = parameter.Value; } } } return response; }; const response = await resolveSecrets(paths); const sharedPaths = (response?.InvalidParameters || []) .map((invalidParam) => envPathObject[invalidParam].sharedPath) .filter((sharedParam) => !!sharedParam); if (sharedPaths.length > 0) { await resolveSecrets(sharedPaths); }};await internalAmplifyFunctionResolveSsmParams();const SSM_PARAMETER_REFRESH_MS = 1000 * 60;setInterval(() => { void internalAmplifyFunctionResolveSsmParams();}, SSM_PARAMETER_REFRESH_MS);export {};" --inject:"/codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-function/lib/lambda-shims/cjs_shim.js" run in directory /codebuild/output/src2174721483/src/staza exited with status 1

68

at AssetStaging.bundle (/codebuild/output/src2174721483/src/staza/node_modules/aws-cdk-lib/core/lib/asset-staging.js:2:619)

69

at AssetStaging.stageByBundling (/codebuild/output/src2174721483/src/staza/node_modules/aws-cdk-lib/core/lib/asset-staging.js:1:5297)

70

at stageThisAsset (/codebuild/output/src2174721483/src/staza/node_modules/aws-cdk-lib/core/lib/asset-staging.js:1:2728)

71

at Cache.obtain (/codebuild/output/src2174721483/src/staza/node_modules/aws-cdk-lib/core/lib/private/cache.js:1:242)

72

at new AssetStaging (/codebuild/output/src2174721483/src/staza/node_modules/aws-cdk-lib/core/lib/asset-staging.js:1:3125)

73

at new Asset (/codebuild/output/src2174721483/src/staza/node_modules/aws-cdk-lib/aws-s3-assets/lib/asset.js:1:1141)

74

at AssetCode.bind (/codebuild/output/src2174721483/src/staza/node_modules/aws-cdk-lib/aws-lambda/lib/code.js:5:3487)

75

at new Function (/codebuild/output/src2174721483/src/staza/node_modules/aws-cdk-lib/aws-lambda/lib/function.js:1:9422)

76

at new NodejsFunction (/codebuild/output/src2174721483/src/staza/node_modules/aws-cdk-lib/aws-lambda-nodejs/lib/function.js:1:2111)

77

at AmplifyFunction (/codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-function/src/factory.ts:347:24),

78

classification: 'ERROR',

79

options: {

80

message: 'Failed to instantiate nodejs function construct',

81

resolution: 'See the underlying error message for more details.'

82

},

83

serializedError: `{"name":"NodeJSFunctionConstructInitializationError","classification":"ERROR","options":{"message":"Failed to instantiate nodejs function construct","resolution":"See the underlying error message for more details."},"cause":{"name":"Error","message":"Failed to bundle asset amplify-d3hdj15s3omqag-main-branch-cb4fc39d52/function/strava-refresh-lambda/Code/Stage, bundle output is located at /codebuild/output/src2174721483/src/staza/.amplify/artifacts/cdk.out/bundling-temp-a967ca1b8f29527dd9c4cbf9ee4d5e5fb3e97e0112872a5c72a8b7cf41793efa-error: Error: bash -c npx --no-install esbuild --bundle \\"/codebuild/output/src2174721483/src/staza/amplify/functions/strava-refresh/handler.ts\\" --target=node18 --platform=node --format=esm --outfile=\\"/codebuild/output/src2174721483/src/staza/.amplify/artifacts/cdk.out/bundling-temp-a967ca1b8f29527dd9c4cbf9ee4d5e5fb3e97e0112872a5c72a8b7cf41793efa/index.mjs\\" --minify --sourcemap --loader:.node=file --banner:js=\\"/** * Reads SSM environment context from a known Amplify environment variable, * fetches values from SSM and places those values in the corresponding environment variables */export const internalAmplifyFunctionResolveSsmParams = async (client) => { const envPathObject = JSON.parse(process.env.AMPLIFY_SSM_ENV_CONFIG ?? '{}'); const paths = Object.keys(envPathObject); if (paths.length === 0) { return; } let actualSsmClient; if (client) { actualSsmClient = client; } else { const ssmSdk = await import('@aws-sdk/client-ssm'); actualSsmClient = new ssmSdk.SSM(); } const resolveSecrets = async (paths) => { const response = await actualSsmClient.getParameters({ Names: paths, WithDecryption: true, }); if (response.Parameters && response.Parameters.length > 0) { for (const parameter of response.Parameters) { if (parameter.Name) { const envKey = Object.keys(envPathObject).find((key) => envPathObject[key].sharedPath === parameter.Name); const envName = envKey ? envPathObject[envKey].name : envPathObject[parameter.Name]?.name; process.env[envName] = parameter.Value; } } } return response; }; const response = await resolveSecrets(paths); const sharedPaths = (response?.InvalidParameters || []) .map((invalidParam) => envPathObject[invalidParam].sharedPath) .filter((sharedParam) => !!sharedParam); if (sharedPaths.length > 0) { await resolveSecrets(sharedPaths); }};await internalAmplifyFunctionResolveSsmParams();const SSM_PARAMETER_REFRESH_MS = 1000 * 60;setInterval(() => { void internalAmplifyFunctionResolveSsmParams();}, SSM_PARAMETER_REFRESH_MS);export {};\\" --inject:\\"/codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-function/lib/lambda-shims/cjs_shim.js\\" run in directory /codebuild/output/src2174721483/src/staza exited with status 1"}}`,

84

resolution: 'See the underlying error message for more details.',

85

details: undefined,

86

link: undefined,

87

code: undefined

88

}

89

Node.js v18.18.2

90

2024-09-05T13:58:23.893Z [INFO]:

91

2024-09-05T13:58:23.936Z [INFO]: [DEBUG] 2024-09-05T13:58:23.936Z:

92

2024-09-05T13:58:23.937Z [INFO]:

93

2024-09-05T13:58:23.937Z [INFO]: [DEBUG] 2024-09-05T13:58:23.937Z: Subprocess exited with error 1

94

2024-09-05T13:58:23.938Z [INFO]:

95

2024-09-05T13:58:24.547Z [INFO]: [DEBUG] 2024-09-05T13:58:24.547Z: {

96

"compilerOptions": {

97

"sourceMap": true,

98

"target": "es2022",

99

"module": "es2022",

100

"moduleResolution": "bundler",

101

"resolveJsonModule": true,

102

"esModuleInterop": true,

103

"forceConsistentCasingInFileNames": true,

104

"strict": true,

105

"skipLibCheck": true,

106

"paths": {

107

"$amplify/*": [

108

"../.amplify/generated/*"

109

]

110

},

111

"allowSyntheticDefaultImports": true,

112

"resolvePackageJsonExports": true,

113

"resolvePackageJsonImports": true,

114

"useDefineForClassFields": true,

115

"noImplicitAny": true,

116

"noImplicitThis": true,

117

"strictNullChecks": true,

118

"strictFunctionTypes": true,

119

"strictBindCallApply": true,

120

"strictPropertyInitialization": true,

121

"alwaysStrict": true,

122

"useUnknownInCatchVariables": true

123

},

124

"files": [

125

"./backend.ts",

126

"./auth/resource.ts",

127

"./data/resource.ts",

128

"./functions/strava-refresh/handler.ts",

129

"./functions/strava-refresh/resource.ts",

130

"./functions/strava-refresh/graphql/API.ts",

131

"./functions/strava-refresh/graphql/mutations.ts",

132

"./functions/strava-refresh/graphql/queries.ts",

133

"./functions/strava-refresh/graphql/subscriptions.ts"

134

]

135

}

136

2024-09-05T13:58:24.548Z [INFO]:

137

2024-09-05T13:58:29.692Z [INFO]: [DEBUG] 2024-09-05T13:58:29.692Z: amplify/functions/strava-refresh/handler.ts(3,19): error TS2307: Cannot find module 'axios' or its corresponding type declarations.

138

2024-09-05T13:58:29.698Z [INFO]: amplify/functions/strava-refresh/handler.ts(3,19): error TS2307: Cannot find module 'axios' or its corresponding type declarations.

139

2024-09-05T13:58:29.733Z [INFO]:

140

2024-09-05T13:58:29.734Z [WARNING]: ampx pipeline-deploy

141

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

142

ded to be used locally.

143

Options:

144

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

145

--help Show help [boolean]

146

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

147

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

148

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

149

t provided defaults to current process working directory.

150

[string]

151

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

152

amplify-cli config file amplify-configuration and 1 represe

153

nts newer config file amplify_outputs

154

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

155

--outputs-format amplify_outputs file format

156

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

157

2024-09-05T13:58:29.734Z [INFO]: SyntaxError: TypeScript validation check failed.

158

Resolution: Fix the syntax and type errors in your backend definition.

159

Cause:

160

2024-09-05T13:58:29.734Z [INFO]: [DEBUG] 2024-09-05T13:58:29.734Z: SyntaxError: TypeScript validation check failed.

161

at CDKDeployer.invokeTsc (file:///codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-deployer/lib/cdk_deployer.js:152:19)

162

at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

163

at async CDKDeployer.deploy (file:///codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-deployer/lib/cdk_deployer.js:57:9)

164

at async Object.handler (file:///codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-cli/lib/commands/pipeline-deploy/pipeline_deploy_command.js:41:9)

165

2024-09-05T13:58:29.734Z [INFO]: [DEBUG] 2024-09-05T13:58:29.734Z: Error

166

at CDKDeployer.executeCommand (file:///codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-deployer/lib/cdk_deployer.js:118:19)

167

at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

168

at async CDKDeployer.invokeTsc (file:///codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-deployer/lib/cdk_deployer.js:142:13)

169

at async CDKDeployer.deploy (file:///codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-deployer/lib/cdk_deployer.js:57:9)

170

at async Object.handler (file:///codebuild/output/src2174721483/src/staza/node_modules/@aws-amplify/backend-cli/lib/commands/pipeline-deploy/pipeline_deploy_command.js:41:9)

171

2024-09-05T13:58:29.851Z [ERROR]: !!! Build failed

172

2024-09-05T13:58:29.852Z [ERROR]: !!! Error: Command failed with exit code 1

173

2024-09-05T13:58:29.852Z [INFO]: # Starting environment caching...

174

2024-09-05T13:58:29.852Z [INFO]: # Environment caching completed
awsluja commented 2 months ago

It looks like you are trying to use axios in your handler but it is not being included (see https://docs.amplify.aws/react/build-a-backend/functions/examples/add-user-to-group/ for an example where an external package is being used inside of a handler)

amplify/functions/strava-refresh/handler.ts(3,19): error TS2307: Cannot find module 'axios' or its corresponding type declarations.

gambithunt commented 2 months ago

Thank you, I thought that it would be bundled with the function, I did try find info in the docs but its a little unclear. Amplify Gen 1 clearly had example of how to create and include layers,

Let me have a look at the link.

ykethan commented 2 months ago

Hey @gambithunt, the function should bundle the dependency do ensure axios is installed in your projects package.json. Additionally, could you check the esbuild version locally installed? It appears, there was a change that modified the default behavior for bundling dependencies in 0.22, but this was reverted in the latest 0.23 release evanw/esbuild@v0.23.0 (release) related issue: https://github.com/aws-amplify/amplify-backend/issues/1704

gambithunt commented 2 months ago

Hi @ykethan thanks helping.

Looks like I have esbuild 23.0 for the project.

Also added axios to the project package.json

Off topic- Do you have an example of calling a amplify gen 2 function from an amplify gen 2 function?

ykethan commented 2 months ago

@gambithunt this looks similar to this thread on discord. but chaining Lambda functions is generally not a recommended practise as it adds additional cold start and invocation time to your process, increasing cost and delay. If you do need to invoke a function you would need to use the AWS SDK, function URL or attach the function to an API to invoke it.

Did try reproducing the to see if Axios was not being bundled but i did not observe a missing module error. Could you also verify if the package-lock.json has axios in it? Additionally, Lambda layers are currently not supported on Amplify Gen 2 and is being tracked on https://github.com/aws-amplify/amplify-backend/issues/1549

gambithunt commented 2 months ago

Thanks for the help @ykethan, I managed to get it working, needed to install axios in the main package.json file, I previously created one in the amplify directory.

Thanks for the advice on changing lambda functions, perhaps a better system would be to import the specific function into each lambda as it was needed. That way it's only codebase that could be shared and maintained, problem is I dont see a way to import a function into multiple amplify gen2 functions. Something to think about.

ykethan commented 2 months ago

Hey @gambithunt, refer to this comment discord providing this information: https://discord.com/channels/705853757799399426/1280541448495960225/1282711148885446788

gambithunt commented 2 months ago

@ykethan thanks so much for all the help, appreciate it.