Amplify-cli: How to generate aws-exports.js file?

Created on 10 Mar 2020  路  13Comments  路  Source: aws-amplify/amplify-cli

* Please describe which feature you have a question about? *

Is there a way to generate the aws-exports.js file that amplify cli generates without running amplifyPush or doing any kind of change to the existing backend env? There must be a command for that.

* Provide additional details*

Due to https://github.com/aws-amplify/amplify-console/issues/501, my backend build in AWS Amplify is failing. I don't need to make changes to the backend however through AWS Amplify. The only reason I am running amplifyPush is so that aws-exports.js is generated, which is needed by my frontend.

pending-response platform question

Most helpful comment

@hisham You can also just use the command amplify env checkout <env-name> to re-generate the aws_exports.js file.

All 13 comments

There's no such a command that solely generates the aws-exports.js file.
You can run amplify env pull to pull down the latest of your project's backend development (by you or your teammates), it will put the the latest into the #current-cloud-backend folder and also recreate the aws-exports.json file for you.
We don't recommend you to do so, but if you absolutely have to manually construct the aws-exports.js file, the values you need for the fields are all in the amplify/#current-cloud-backend/amplify-meta.json file.

Ok thanks. It's just confusing what amplifyPush does in the AWS Amplify Console CI/CD build. For example, if branch A and branch B are connected to backend env 'prod'. Let's say in branch A I make a change to the backend and push via 'amplify push' on my computer but I don't git push. Then in branch B I make a frontend change and git push, but branch B has old prod environment since it's not merged with branch A. What will amplifyPush in AWS Amplify do? Will it override the backend prod env with an older version or is it smart enough to not revert. It's not clear. It seems to run an 'amplify init' command which may or may not push. I guess I'll do more testing and reading.

@hisham You can also just use the command amplify env checkout <env-name> to re-generate the aws_exports.js file.

@kaustavghosh06 I just tried this by adding it in my prebuild step for frontend in amplify.yml

Snippet from my amplify.yml:

frontend:
  phases:
    preBuild:
      commands:
        - amplify env checkout $BACKEND_ENV
        - npm ci
                                 # Starting phase: preBuild
                                 # Executing command: amplify env checkout prod
2020-03-12T19:01:20.805Z [WARNING]: /root/.nvm/versions/node/v10.16.0/lib/node_modules/@aws-amplify/cli/node_modules/gluegun/build/index.js:13
                                    throw up;
                                    ^
                                    Error: ENOENT: no such file or directory, open '/codebuild/output/src921029018/src/ess-app/amplify/.config/local-env-info.json'
                                    at Object.openSync (fs.js:443:3)
                                    at Object.readFileSync (fs.js:343:35)
                                    at readJsonFile (/root/.nvm/versions/node/v10.16.0/lib/node_modules/@aws-amplify/cli/src/extensions/amplify-helpers/read-json-file.js:11:33)
                                    at Command.run (/root/.nvm/versions/node/v10.16.0/lib/node_modules/@aws-amplify/cli/src/commands/env/checkout.js:23:26)
                                    at Runtime.<anonymous> (/root/.nvm/versions/node/v10.16.0/lib/node_modules/@aws-amplify/cli/node_modules/gluegun/build/runtime/run.js:117:58)
                                    at step (/root/.nvm/versions/node/v10.16.0/lib/node_modules/@aws-amplify/cli/node_modules/gluegun/build/runtime/run.js:43:23)
                                    at Object.next (/root/.nvm/versions/node/v10.16.0/lib/node_modules/@aws-amplify/cli/node_modules/gluegun/build/runtime/run.js:24:53)
                                    at fulfilled (/root/.nvm/versions/node/v10.16.0/lib/node_modules/@aws-amplify/cli/node_modules/gluegun/build/runtime/run.js:15:58)
                                    at process._tickCallback (internal/process/next_tick.js:68:7)
                                    at Function.Module.runMain (internal/modules/cjs/loader.js:832:11)
                                    at startup (internal/bootstrap/node.js:283:19)
                                    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
2020-03-12T19:01:20.815Z [ERROR]: !!! Build failed
2020-03-12T19:01:20.898Z [ERROR]: !!! Non-Zero Exit Code detected
2020-03-12T19:01:20.899Z [INFO]: # Starting environment caching...
2020-03-12T19:01:20.899Z [INFO]: # Environment caching completed
Terminating logging...

I guess my use case is how do I connect a frontend with a backend in the AWS Amplify CI environment without triggering an amplify push? It seems the current amplifyPush.sh script can trigger a push even though it only calls amplify init but amplify init in headless mode apparently can cause pushes. See https://stackoverflow.com/questions/58957834/amplify-init-headless-always-pushes-to-aws

How about amplify pull? Did you check that out?
https://aws-amplify.github.io/docs/cli-toolchain/quickstart#amplify-pull

Also, you can execute this command in a headless manner in your CI scripts - https://aws-amplify.github.io/docs/cli-toolchain/usage#amplify-pull-parameters

Please comment on this thread if you have any further questions

I am trying to achieve this, I need aws-exports to be generated before pushing because I need some values to generate a dotenv file being used by some of my lambda functions, I need to do that on amplify ci/cd. I tried amplify init -y but it produced this:

2020-05-20T01:05:07.837Z [INFO]: # Executing command: amplify init -y
2020-05-20T01:05:08.716Z [INFO]: Note: It is recommended to run this command from the root of your app directory
2020-05-20T01:05:08.718Z [INFO]: Environment name missing
2020-05-20T01:05:08.726Z [ERROR]: !!! Build failed
2020-05-20T01:05:08.795Z [ERROR]: !!! Non-Zero Exit Code detected
2020-05-20T01:05:08.795Z [INFO]: # Starting environment caching...
2020-05-20T01:05:08.796Z [INFO]: # Environment caching completed
Terminating logging...

When I used amplify pull -y it just hanged, seems that the process just stopped with the following output:

2020-05-20T01:22:56.183Z [INFO]: # Executing command: amplify pull -y
2020-05-20T01:23:02.136Z [INFO]: 

Any thoughts on how to do this?

@yuth @kaustavghosh06

-- Edit

Also tried amplify env checkout $ENV and amplify env checkout $BACKEND_ENV but both variables seem to be empty/undefined.

I tried amplify env checkout $AWS_BRANCH but it produced this

2020-05-20T03:00:44.361Z [INFO]: # Executing command: amplify env checkout dev
2020-05-20T03:00:45.253Z [INFO]: Current environment cannot be determined
                                 Use 'amplify init' in the root of your app directory to initialize your project with Amplify

this is my amplify.yml

version: 0.1
backend:
  phases:
    build:
      commands:
        - npm ci
        - amplify env checkout $AWS_BRANCH
        - npm run gen-lambda-vars
        - amplifyPush --simple
frontend:
  phases:
    preBuild:
      commands:
        - npm ci
    build:
      commands:
        - npm run build
  artifacts:
    baseDirectory: build
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*
      - $HOME/.npm

Does anyone have an example using amplify pull in amplify console build yml file?

There's no such a command that solely generates the aws-exports.js file.
You can run amplify env pull to pull down the latest of your project's backend development (by you or your teammates), it will put the the latest into the #current-cloud-backend folder and also recreate the aws-exports.json file for you.
We don't recommend you to do so, but if you absolutely have to manually construct the aws-exports.js file, the values you need for the fields are all in the amplify/#current-cloud-backend/amplify-meta.json file.

Please don't take offence but this response is typical of AWS docs -

note this line: "...and also recreate the aws-exports.json file for you..."

and then this one: "...absolutely have to manually construct the aws-exports.js file..."

So what is it that gets created ? A JSON file or a JS file ?

And what does 'amplify pull' do ?

Either way I don't get any aws-exports.js or aws-exports.json created locally in the /src folder (or anywhere else for that matter).

??

ampli

amplify env checkout demo does not recreate the aws-config.js file nor do any of the other amplify pull commands!!

Also why does my original aws-exports.js file have

const awsmobile = {...}

That's clearly going to fail when the frontend is deployed because the deployment relies on importing config ?

for other people running into similar issues, i fixed this problem by creating my own config file:

export const AMPLIFY_CONFIG = {
  "Auth": {
    "region": process.env.REACT_APP_AMPLIFY_REGION,
    "userPoolId": process.env.REACT_APP_AMPLIFY_USER_POOL_ID,
    "userPoolWebClientId": process.env.REACT_APP_AMPLIFY_APP_CLIENT_ID,
  },
  "aws_appsync_graphqlEndpoint": process.env.REACT_APP_AMPLIFY_APP_GRAPHQL_ENDPOINT,
  "aws_appsync_region": process.env.REACT_APP_AMPLIFY_REGION,
  "aws_appsync_authenticationType": process.env.REACT_APP_AMPLIFY_APP_GRAPHQL_AUTHENTICATION_TYPE,
  "aws_appsync_apiKey": process.env.REACT_APP_AMPLIFY_APP_GRAPHQL_API_KEY,
};
import Amplify from "aws-amplify";
import { AMPLIFY_CONFIG } from "./config";
Amplify.configure(AMPLIFY_CONFIG);



md5-8efdf2f58155f1a996d214e1ad5dbb32



version: 1
backend:
  phases:
    build:
      commands:
frontend:
  phases:
    preBuild:
      commands:
        - npm ci
    build:
      commands:
        - npm run build:$BUILD_ENV
  artifacts:
    baseDirectory: build
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*



md5-0fb7868b7eb03bbb7d59cef79d7f2658



"scripts": {
    "lint": "eslint ./src",
    "start": "react-scripts start",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "build:dev": "dotenv -e .env.development react-scripts build",
    "build:prod": "dotenv -e .env.production react-scripts build",
    "build:staging": "dotenv -e .env.staging react-scripts build",
    "build": "echo \"Please use build:dev or build:prod \" && exit 1"
  },
Was this page helpful?
0 / 5 - 0 ratings