aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.82k stars 819 forks source link

How to generate aws-exports.js file? #3643

Closed hisham closed 4 years ago

hisham commented 4 years ago

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.

UnleashedMind commented 4 years ago

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.

hisham commented 4 years ago

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.

kaustavghosh06 commented 4 years ago

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

hisham commented 4 years ago

@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...
hisham commented 4 years ago

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

kaustavghosh06 commented 4 years ago

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

kaustavghosh06 commented 4 years ago

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

yuth commented 4 years ago

Please comment on this thread if you have any further questions

aprilmintacpineda commented 4 years ago

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
kldeb commented 4 years ago

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

duncangroenewald commented 4 years ago

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).

??

duncangroenewald commented 4 years ago

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 ?

simpian commented 3 years ago

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);

i have .env file for different environments. e.g.

  1. .env.development
  2. .env.staging
  3. .env.production

and i also set the environment on amplify build settings for each environment individually using BUILD_ENV, here is my amplify.yml file

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/**/*

package.json

"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"
  },
sdmunozsierra commented 3 years ago

If anyone is stuck with nuxt and amplify when trying to deploy to aws.

version: 1
backend:
  phases:
    build:
      commands:
        - '# Execute Amplify CLI with the helper script'
        - amplifyPush --simple'
frontend:
  phases:
    preBuild:
      commands:
        - yarn install
    build:
      commands:
        - yarn run generate
  artifacts:
    # IMPORTANT - Please verify your build output directory
    baseDirectory: dist
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*
diyism commented 3 years ago

https://console.aws.amazon.com/iam/home?region=us-west-1#/roles

enter AWSAmplifyExecutionRole-xxxxx
click "Attach policies" button
search "AWSCloudFormationFullAccess" and add it into AWSAmplifyExecutionRole-xxxxx
search " AmazonS3FullAccess" and add it into AWSAmplifyExecutionRole-xxxxx
edit AWSAmplifyExecutionPolicy-xxxx,  add these:
        {
            "Effect": "Allow",
            "Action": [
                "iam:*"
            ],
            "Resource": "*"
        }

https://us-west-1.console.aws.amazon.com/amplify/home?region=us-west-1#/ ... /settings/build

backend:
  phases:
    build:
      commands:
        - '# Execute Amplify CLI with the helper script'
        - envCache --set stackInfo ""
        - amplifyPush --simple
github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

david-mcafee commented 2 years ago

As a follow-up on this, to generate the aws-exports.js file in an existing project (or checkout a backend environment that exists in the console but does not show up locally), follow these steps:

  1. Go to the AWS console
  2. Choose AWS Amplify
  3. Find the app
  4. Go to Backend environments
  5. Find the backend environment
  6. Click Edit backend
  7. Run the command that is listed there, something like:
    • amplify pull --appId YOUR_APP_ID --envName YOUR_ENV_NAME
swaminator commented 2 years ago

Since then we have launched capabilities for you to generated the aws-exports.js in the build container as well: https://docs.aws.amazon.com/amplify/latest/userguide/amplify-config-autogeneration.html (for anyone who lands here)