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

amplifyPush --simple broken Module not found: Error: Can't resolve './aws-exports' in #13515

Closed
5 tasks done
austinamorusoyardstick opened this issue Dec 22, 2023 · 7 comments
Closed
5 tasks done
Labels
hosting Issues tied to hosting category pending-triage Issue is pending triage platform-push Issues related to `amplify push`

Comments

@austinamorusoyardstick
Copy link

Before opening, please confirm:

  • I have checked to see if my question is addressed in the FAQ.
  • I have searched for duplicate or closed issues.
  • I have read the guide for submitting bug reports.
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • I have removed any sensitive information from my code snippets and submission.

App Id

d2t0beklulssyh

AWS Region

us-east-1

Amplify Hosting feature

Deployments

Frontend framework

React

Next.js version

N/A

Next.js router

App Router

Describe the bug

When deploying with amplifyPush --simple the building of the lambdas fail because they cannot find the aws-exports.js

Expected behavior

Build works aws-exports.js is available for lambda compilation

Reproduction steps

  1. push a new build with ci/cd that has lambdas that compile and use aws-exports.js

Build Settings

version: 0.1
backend:
  phases:
    preBuild:
      commands:
        - nvm use 18
        - yarn install
    build:
      commands:
        - yarn backend-deploy
  cache:
    paths:
      - node_modules/**/*

frontend:
  phases:
    preBuild:
      commands:
        - nvm use 18
        - yarn install
        - yarn schema
        - yarn test-ci
        - yarn nightwatch-ci
    build:
      commands:
        - yarn build
        - yarn feature-flag-deploy
  artifacts:
    baseDirectory: build
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

Log output

2023-12-22T01:07:48.442Z [INFO]: # Cloning repository: [email protected]:yardstickinc/yardstick.git
2023-12-22T01:08:00.381Z [INFO]:
2023-12-22T01:08:00.505Z [INFO]: Cloning into 'yardstick'...
2023-12-22T01:08:00.809Z [INFO]: From https://github.com/yardstickinc/yardstick
                                 * [new ref]           refs/pull/804/head -> pr-804
                                 Switched to branch 'pr-804'
2023-12-22T01:08:00.855Z [INFO]: Successfully cleaned up Git credentials
2023-12-22T01:08:00.855Z [INFO]: # Checking for Git submodules at: /codebuild/output/src2258782366/src/yardstick/.gitmodules
2023-12-22T01:08:00.865Z [INFO]: # Retrieving environment cache...
2023-12-22T01:08:01.444Z [INFO]: # Retrieved environment cache
2023-12-22T01:08:01.444Z [INFO]: ---- Setting Up SSM Secrets ----
2023-12-22T01:08:01.444Z [INFO]: SSM params {"Path":"/amplify/d2t0beklulssyh/sandbox/","WithDecryption":true}
2023-12-22T01:08:02.921Z [INFO]: No live updates for this build run
2023-12-22T01:08:02.925Z [INFO]: # Retrieving cache...
2023-12-22T01:08:02.974Z [INFO]: # Retrieved cache
2023-12-22T01:08:35.612Z [INFO]: ## Starting Backend Build
                                 # Starting phase: preBuild
                                 # Executing command: nvm use 18
2023-12-22T01:08:52.886Z [INFO]: Now using node v18.13.0 (npm v8.19.3)
2023-12-22T01:08:52.887Z [INFO]: # Executing command: yarn install
2023-12-22T01:08:54.702Z [INFO]: ➤ YN0000: ┌ Resolution step
2023-12-22T01:08:55.383Z [INFO]: ➤ YN0002: │ @aws-amplify/api@npm:6.0.8 doesn't provide @aws-amplify/core (p7e654), requested by @aws-amplify/api-rest
...
2023-12-22T01:11:10.895Z [INFO]: ➤ YN0000: └ Completed in 1m 14s
2023-12-22T01:11:11.347Z [INFO]: ➤ YN0000: Done with warnings in 2m 17s
2023-12-22T01:11:11.748Z [INFO]: # Completed phase: preBuild
                                 # Starting phase: build
2023-12-22T01:11:11.748Z [INFO]: # Executing command: yarn backend-deploy
2023-12-22T01:11:14.180Z [INFO]: Booting up the Turbo Encabulator...
2023-12-22T01:11:14.950Z [INFO]: { isEnvProduction: false }
2023-12-22T01:11:16.409Z [INFO]: Synchronizing Cardinal Graham Eaters
2023-12-22T01:11:19.925Z [INFO]: Generating relative motion of conductors and fluxes
2023-12-22T01:11:19.931Z [INFO]: { hash: 'cfdca763aed0928c1378b922b150fc38b2040d47' }
2023-12-22T01:11:19.931Z [INFO]: { userBranch: 'sandbox' }
                                 { base: 'dev' }
2023-12-22T01:11:19.943Z [INFO]: {
                                 files: [
                                 'amplify/backend/api/yardstick/pipelineFunctions/InvokeEditUserMembershipLambdaDataSource.req.vtl',
                                 'amplify/backend/api/yardstick/schema/Mutations.graphql',
                                 'amplify/backend/backend-config.json',
                                 'amplify/backend/function/editUserMembership/amplify.state',
                                 'amplify/backend/function/editUserMembership/editUserMembership-cloudformation-template.json',
                                 'amplify/backend/function/editUserMembership/src/package.json',
                                 'amplify/backend/function/editUserMembership/src/yarn.lock',
                                 'package.json',
                                 'src/function/editUserMembership/config.js',
                                 'src/function/editUserMembership/debug.js',
                                 'src/function/editUserMembership/index.js',
                                 'src/function/editUserMembership/log.js',
                                 'src/function/editUserMembership/process.js',
                                 'src/function/editUserMembership/query/getAccount.graphql',
                                 'src/function/editUserMembership/query/getAccount.js',
                                 'src/function/editUserMembership/query/getTeam.graphql',
                                 'src/function/editUserMembership/query/getTeam.js',
                                 'src/function/editUserMembership/schema/Query.graphql',
                                 '',
                                 [length]: 19
                                 ]
                                 }
2023-12-22T01:11:19.943Z [INFO]: { shouldDeployBackend: true }
                                 Starting backend deployment...
2023-12-22T01:11:19.943Z [INFO]: { command: 'amplifyPush --simple' }
2023-12-22T01:11:19.957Z [INFO]: amplifyPush # Getting Amplify CLI Cloud-Formation stack info from environment cache
2023-12-22T01:11:19.965Z [INFO]: amplifyPush # Start initializing Amplify environment: sandbox
2023-12-22T01:11:19.965Z [INFO]: amplifyPush # Initializing new Amplify environment: sandbox (amplify init)
2023-12-22T01:11:23.938Z [INFO]: amplifyPush �[33mNote: It is recommended to run this command from the root of your app directory�[39m
2023-12-22T01:11:25.364Z [WARNING]: amplifyPush - Initializing your environment: sandbox
2023-12-22T01:11:34.126Z [WARNING]: amplifyPush - Building resource api/yardstick
2023-12-22T01:11:34.127Z [WARNING]: amplifyPush - Building resource auth/userPoolGroups
2023-12-22T01:11:34.582Z [WARNING]: amplifyPush - Building resource auth/yardsticke924e399
2023-12-22T01:11:34.583Z [WARNING]: amplifyPush - Building resource custom/KMSKeyIDArn
2023-12-22T01:11:34.583Z [WARNING]: amplifyPush - Building custom resources
2023-12-22T01:11:34.993Z [WARNING]: amplifyPush ✔ Initialized provider successfully.
2023-12-22T01:11:38.694Z [INFO]: amplifyPush �[33m�[39m
                                 �[33mThe following types do not have '@auth' enabled. Consider using @auth with @model�[39m
2023-12-22T01:11:38.697Z [INFO]: amplifyPush �[33m	 - AccountUser�[39m
                                 �[33m	 - InterviewLayoutInterviewComponent�[39m
                                 �[33m	 - Legal�[39m
                                 �[33m	 - TermsPolicy�[39m
                                 �[33m	 - PrivacyPolicy�[39m
                                 �[33m	 - MetricTypeValue�[39m
                                 �[33m	 - MetricType�[39m
                                 �[33m	 - TeamUser�[39m
                                 �[0mLearn more about @auth here: https://docs.amplify.aws/cli-legacy/graphql-transformer/auth�[0m
                                 �[0m�[0m
2023-12-22T01:11:40.649Z [WARNING]: amplifyPush Be careful when using @auth directives on a field in a root type. @auth directives on field definitions use the source object to perform authorization logic and the source will be an empty object for fields on root types. Static group authorization should perform as expected.
2023-12-22T01:11:42.638Z [WARNING]: amplifyPush Be careful when using @auth directives on a field in a root type. @auth directives on field definitions use the source object to perform authorization logic and the source will be an empty object for fields on root types. Static group authorization should perform as expected.
2023-12-22T01:11:44.774Z [INFO]: amplifyPush �[32mGraphQL schema compiled successfully.�[39m
                                 �[32mEdit your schema at /codebuild/output/src2258782366/src/yardstick/amplify/backend/api/yardstick/schema.graphql or place .graphql files in a directory at /codebuild/output/src2258782366/src/yardstick/amplify/backend/api/yardstick/schema�[39m
2023-12-22T01:28:22.418Z [WARNING]: amplifyPush ✖ There was an error initializing your environment.
2023-12-22T01:28:22.440Z [INFO]: amplifyPush 🛑 Packaging lambda function failed with the error
                                 Command failed with exit code 1: yarn amplify:pdfCandidateStatus
                                 <s> [webpack.Progress] 0%
...
                                 <s> [webpack.Progress] 100%
                                 { isEnvProduction: false }
                                 asset index.js 47.4 MiB [emitted] [big] (name: main)
                                 orphan modules 390 KiB [orphan] 235 modules
                                 runtime modules 2.15 KiB 6 modules
                                 modules by path ./node_modules/ 13.4 MiB
                                 javascript modules 12.6 MiB 3926 modules
                                 json modules 785 KiB
                                 modules by path ./node_modules/react-html-parser/ 62.9 KiB 9 modules
                                 modules by path ./node_modules/@aws-sdk/ 19.8 KiB 6 modules
                                 modules by path ./node_modules/css-tree/ 273 KiB 5 modules
                                 modules by path ./node_modules/css-select/ 30 KiB 4 modules
                                 modules by path ./node_modules/escodegen/ 1.99 KiB 2 modules
                                 + 3 modules
                                 modules by path ./src/ 3 MiB 657 modules
                                 + 25 modules
                                 ERROR in ./src/awsExportsWrapper.js 11:59-83
                                 Module not found: Error: Can't resolve './aws-exports' in '/codebuild/output/src2258782366/src/yardstick/src'
                                 resolve './aws-exports' in '/codebuild/output/src2258782366/src/yardstick/src'
                                 using description file: /codebuild/output/src2258782366/src/yardstick/package.json (relative path: ./src)
                                 using description file: /codebuild/output/src2258782366/src/yardstick/package.json (relative path: ./src/aws-exports)
                                 no extension
                                 /codebuild/output/src2258782366/src/yardstick/src/aws-exports doesn't exist
                                 .js
                                 /codebuild/output/src2258782366/src/yardstick/src/aws-exports.js doesn't exist
                                 .json
                                 /codebuild/output/src2258782366/src/yardstick/src/aws-exports.json doesn't exist
                                 .wasm
                                 /codebuild/output/src2258782366/src/yardstick/src/aws-exports.wasm doesn't exist
                                 as directory
                                 /codebuild/output/src2258782366/src/yardstick/src/aws-exports doesn't exist
                                 @ ./src/lib/apollo-link-appsync/index.js 22:59-80
                                 @ ./src/lib/client/links.js 12:27-57
                                 @ ./src/lib/client/config.js 13:15-33
                                 @ ./src/lib/client/index.js 38:29-48
                                 @ ./src/lib/function/helpers/pdf/index.js 24:16-33
                                 @ ./src/function/pdfCandidateStatus/process.js 16:13-44
                                 @ ./src/function/pdfCandidateStatus/index.js 10:17-37
                                 ERROR in [eslint]
                                 /codebuild/output/src2258782366/src/yardstick/src/awsExportsWrapper.js
                                 1:34  error  Unable to resolve path to module './aws-exports'  import/no-unresolved
                                 ✖ 1 problem (1 error, 0 warnings)
                                 webpack 5.89.0 compiled with 2 errors in 21935 ms
2023-12-22T01:28:22.441Z [INFO]: amplifyPush
                                 Learn more at: https://docs.amplify.aws/cli/project/troubleshooting/
2023-12-22T01:28:22.452Z [INFO]: amplifyPush
2023-12-22T01:28:22.453Z [INFO]: amplifyPush Session Identifier: e60a6533-aea6-4eec-bdbf-ab4cc9c79095

Additional information

amplifyPush Session Identifier: 3b5b370e-b5d8-48be-b961-8123d718c701

backend-deploy node script

import { exec, execSync } from 'child_process';

export default async function amplifyBackend() {
	if (process.env.FORCE_DEPLOY_BACKEND === 'true') {
		console.log('Starting backend deployment (FORCED)...');
		await deployBackend();
		return;
	}
	const hash = process.env.AWS_COMMIT_ID;
	console.log({ hash });
	const userBranch = process.env.USER_BRANCH;
	console.log({ userBranch });
	const base = userBranch !== 'prod' ? 'dev' : 'prod';
	console.log({ base });
	const files = gitDiffFiles(base, hash);
	console.log({ files });
	const shouldDeployBackend = hasBackendChanges(files);
	console.log({ shouldDeployBackend });
	if (shouldDeployBackend) {
		console.log('Starting backend deployment...');
		await deployBackend();
		return;
	}
	await getAWSExports(userBranch);
	console.log('SKIPPING BACKEND DEPLOYMENT');
	return;
}

function hasBackendChanges(files) {
	return files?.some((file) => isBackendDirectory(file));
}

function isBackendDirectory(file) {
	return file.startsWith('amplify/') || file.startsWith('src/function');
}

function cleanText(text) {
	const cleaned = text.replace(/\s/g, ',')?.split(',');
	return cleaned;
}
function command(script) {
	const result = execSync(script).toString();
	const cleaned = cleanText(result);
	return cleaned;
}

function gitDiffFiles(base, hash) {
	const script = `git --no-pager diff --name-only ${base} ${hash}`;
	return command(script);
}

function execute(command, env, tag = '') {
	return new Promise(function (resolve, reject) {
		console.log({ command });
		const child = exec(command, {
			env,
			maxBuffer: 1024 * 1024 * 1004 * 100,
			stdio: 'inherit',
		});
		child.stdout.on('data', function (data) {
			console.log(tag, data);
		});
		child.stderr.on('data', function (data) {
			console.error(tag, data);
		});
		child.on('close', function (code) {
			console.warn(tag, code);
		});
		child.addListener('error', function (err) {
			console.error(tag, err);
		});
		child.addListener('exit', function (code) {
			console.warn(tag, code);
			if (code > 0) {
				reject(
					new Error(`${tag}: FAILED - EXITCODE: ${code}: ${command}`)
				);
			}
			resolve(code);
		});
	});
}

function deployBackend() {
	return execute('amplifyPush --simple', process.env, 'amplifyPush');
}

function getAWSExports(env) {
	const script = `amplify init --amplify "{\\"envName\\":\\"${env}\\"}" -y`;
	return execute(script, process.env, 'getAWSExports');
}

@austinamorusoyardstick austinamorusoyardstick added the pending-triage Issue is pending triage label Dec 22, 2023
@swaminator swaminator transferred this issue from aws-amplify/amplify-hosting Jan 4, 2024
@ykethan
Copy link
Member

ykethan commented Jan 5, 2024

Hey @austinamorusoyardstick, thank you for reaching out. The amplifyPush --simple will need to be explicitly provided in the yml file as the during the build the command is parsed from the file then generates the aws-exports.js file.

@ykethan ykethan added hosting Issues tied to hosting category pending-response Issue is pending response from the issue author platform-push Issues related to `amplify push` labels Jan 5, 2024
@austinamorusoyardstick
Copy link
Author

How then do I make it optional to run?

@github-actions github-actions bot removed the pending-response Issue is pending response from the issue author label Jan 5, 2024
@austinamorusoyardstick
Copy link
Author

I also was able to run amplify init prior to push simple in my script and it worked

@ykethan
Copy link
Member

ykethan commented Jan 8, 2024

@austinamorusoyardstick interesting, let me reach out to the console team for some additional information on this behavior. But when utilizing a custom script, you should be able to run amplify cli headless commands to run init and push.
additionally, observed Amplify console does provide a environment variable that runs a diff in the amplify folder: https://docs.aws.amazon.com/amplify/latest/userguide/build-settings.html#enable-diff-backend

@ykethan ykethan added the pending-response Issue is pending response from the issue author label Jan 8, 2024
@austinamorusoyardstick
Copy link
Author

I find the diff backend doesn't tend to work for us correctly. It often skips builds that it shouldn't.

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

ykethan commented Jan 23, 2024

@austinamorusoyardstick I was able to get some information, the recommended approach for generating the aws-exports.js file is using the amplifyPush --simple script at build time. If the amplifyPush --simple is nested under a custom script that utilizes Amplify CLI headless commands, the backend build will fail as we expect this command in the buildSpec to be parsed at build time to generate the aws-exports.js file for full-stack deployments.
additionally, as the diff backend issue seems to be related to Amplify hosting do open a case with AWS technical support as the hosting repository only accepts new feature requests.
Closing the issue.

@ykethan ykethan closed this as not planned Won't fix, can't repro, duplicate, stale Jan 23, 2024
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hosting Issues tied to hosting category pending-triage Issue is pending triage platform-push Issues related to `amplify push`
Projects
None yet
Development

No branches or pull requests

2 participants