Trying to use environment variables

1

I am trying to follow the directions here https://docs.aws.amazon.com/amplify/latest/userguide/environment-variables.html, and the instructions say to add :$BUILD_ENV as follows:

build:
commands:
- npm run build:$BUILD_ENV

But this generates a syntax error saying "command build: not found". What I want to do is pass my Cognito API keys as environment variables, so I don't have to put them in my code.

Thanks for any help with this,
Lee

已提問 4 年前檢視次數 472 次
2 個答案
0

Hello, we have provided a general way for you to access environment variables. In your case, you might need to do something like:

build:
   commands:
      - ENV_NAME=$ENV_NAME
      - npm run build
已回答 4 年前
0

To use environment variables during a build in AWS Amplify, follow these steps.

  1. Go to the AWS Amplify Console in the AWS Management Console and select the app that you want to configure.

  2. Inside your app's dashboard, click on "Backend environments" on the left menu, and then choose "Edit" for the environment you want to work with (e.g., staging or production).

  3. Configure Environment Variables: In the environment settings, you'll find an option to add environment variables. You can add key-value pairs representing your environment-specific variables.

    For example:

    MY_API_URL=https://api.example.com
    DEBUG_MODE=true
    

    These environment variables will be accessible during the build process.

  4. In your build configuration file (e.g., amplify.yml or buildspec.yml), you can access the environment variables using the syntax for the respective build platform.

    • For AWS Amplify Console's built-in build platform, you can access environment variables using the following syntax:

      version: 1
      frontend:
        phases:
          build:
            commands:
              - echo "My API URL: $MY_API_URL"
    • If you're using AWS Amplify with a custom build environment, you might use different methods to access the environment variables based on the specific build tools and platform you are using.

  5. When you trigger a new build or deploy your application, AWS Amplify will automatically use the environment variables you defined during the build process. This allows your application to adapt to different environments based on the specified variables.

AWS
已回答 9 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南