By using AWS re:Post, you agree to the AWS re:Post Terms of Use

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

asked 5 years ago666 views
2 Answers
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
answered 5 years ago
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
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions