28

I am trying to deploy my reactJs app to Amplify. I have my Github connected to Amplify. During deployment it shows the following error at Build step:

2020-01-07T19:35:22.127Z [INFO]: Failed to compile.
2020-01-07T19:35:22.129Z [INFO]: ./src/index.js
                                 Cannot find file './aws-exports' in './src'.
2020-01-07T19:35:22.149Z [WARNING]: error Command failed with exit code 1.
2020-01-07T19:35:22.150Z [INFO]: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
2020-01-07T19:35:22.155Z [ERROR]: !!! Build failed
2020-01-07T19:35:22.239Z [ERROR]: !!! Non-Zero Exit Code detected
2020-01-07T19:35:22.239Z [INFO]: # Starting environment caching...

This happens because .gitignore ignores aws-exports. Can someone please tell me what's the solution to this problem without committing aws-exports?

Telenoobies
  • 938
  • 3
  • 16
  • 33
  • 2
    If you are not able to find aws-exports, try to use ( amplify pull ) https://github.com/aws-amplify/amplify-cli/issues/186 – Cadbury Nov 22 '20 at 02:09

1 Answers1

36

I have experienced the same problem in my first build.

The Amplify documentation is not specific about how you should maintain your builds when using the Amplify Console, but the routine that worked for me was:

You generate your aws-exports file when you run a successful amplify push command.

aws-exports.js file This file is generated only for JavaScript projects. It contains the consolidated outputs from all the categories and is placed under the src directory that the user (the developer) specified during the init process. It is updated after each successful execution of the amplify push command, that has created or updated the cloud resources.

Based on that I updated my configuration in the Amplify console to also deploy my backend. You can learn how to configure your own at https://docs.aws.amazon.com/amplify/latest/userguide/build-settings.html

backend:
  phases:
    build:
      commands:
        - '# Execute Amplify CLI with the helper script'
        - amplifyPush --simple

After the backend build is done the file is generated for my next job which is the frontend build that consumes the aws-exports file.

Note: If you're using eslint you can have a problem with the file output format. You can add a eslint --fix command in your frontend preBuild

Update: As lucdenz mentioned, you also need to setup a service role

Sources I used:

Chris Hayes
  • 11,505
  • 6
  • 33
  • 41
  • I solved the problem by completely removing any code that uses aws-exports, I realized I didn't actually need to import that file. – Telenoobies Jan 20 '20 at 17:15
  • Yeah, if you're not using any backend from amplify this is for sure the best way for doing it. If you come to add any backend dependencies than it will come to this. – Pedro Frattezi Silva Jan 20 '20 at 18:55
  • I am fading thé sale thing. I added Auth to my project. I use a plugin file in which I need to load Cognito user before app mounting. Since then, when I push the code to git. The amplify console run the build which failed because the file is missing. Any idea? – Thzith Apr 15 '20 at 17:01
  • 1
    Excellent answer -- that helped me, thank you. Just to add, if you don't yet have a ServiceRole for the back end, you'll need to add one for this to work. – John Lockwood Apr 28 '20 at 01:23
  • 1
    `amplifyPush --simple` is documented here -> https://docs.aws.amazon.com/amplify/latest/userguide/build-settings.html#frontend-with-backend. – mewc May 01 '20 at 00:21
  • 3
    Needs a service role as well. Doc can be found here: https://docs.aws.amazon.com/amplify/latest/userguide/how-to-service-role-amplify-console.html – lukeharg May 05 '20 at 00:45
  • 3
    tried this but build failed with message "Resource is not in the state stackUpdateComplete" anybody else got this? – Ricky Jun 04 '20 at 17:37
  • @Ricky this error you're getting is probably due to a failed backend deploy. Check your cloudFormation console for any error's related to your project stack. – Pedro Frattezi Silva Jun 05 '20 at 13:38
  • Sources link seems to be invalid now :(. Are there more references on this? – Stvad Jan 24 '21 at 20:08
  • 1
    So following this, I hit a few different issues with backend build: https://github.com/aws-amplify/amplify-cli/issues/6117 (version mismatch locally and remotely), https://github.com/aws-amplify/amplify-cli/issues/5931 (bug with recent version of cli?), now trying to deal with https://github.com/aws-amplify/amplify-cli/issues/5791 and wishing I can just push it manually, but apparently it's not an option https://github.com/aws-amplify/amplify-console/issues/956 – Stvad Jan 24 '21 at 22:13
  • Since this issue has been a lot discussed and seeing @Stvad comments I'll take some time to update the answer, thank you for the collaboration. – Pedro Frattezi Silva Jul 08 '21 at 13:22
  • 1
    Note that this is only if you build with AWS, in my case on Netlify, `amplifyPush --simple` is not available. [According to this GitHub comment](https://github.com/aws-amplify/docs/issues/503#issuecomment-686752095), `aws-exports.js` does NOT contain sensitive data. The reason it's in `.gitignore` is that it'll apply the same config across all your environments. – Chris Hayes Nov 13 '21 at 20:45
  • Renaming aws-exports.js -> aws-exports.ts also worked. – Jorge Garcia Mar 27 '23 at 03:59