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달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠