amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Add env name to the "aws-exports.js"

Open ghost opened this issue 5 years ago • 5 comments

Is your feature request related to a problem? Please describe. I need to access the enviornment name of my backend in the frontend to display different features.

Describe the solution you'd like Add the value "env" in the aws-exports.js which is generated by the Amplify CLI (And when possible also to the build spec as a native Env to use it in this deployment as well)

Describe alternatives you've considered Query the backend stack of amplify or replace a config file during the build process

Thx!

ghost avatar May 24 '20 10:05 ghost

+1. But there's an easier workaround available if you have a REST or Graphql endpoint in the aws-exports. These endpoints are always post-fixed with the backend env. So you can do something like this:

import awsmobile from '../../../shared/aws-exports.js';

const restEndpoint = awsmobile.aws_cloud_logic_custom[0].endpoint;
const backendEnv = restEndpoint.split('amazonaws.com/')[1];

hisham avatar May 29 '20 05:05 hisham

Yes this would be great, currently we need to do a custom build through webpack to set the env, which prevents us from using amplify hosting.

Would be much more simple to access via aws-exports.

dylan-westbury avatar Oct 26 '20 06:10 dylan-westbury

Any updates on this? This seems pretty simple to implement and would be very useful. The workaround above does not work for me (I have graphql and my URL doesn't have anything on it that would indicated the environment).

malcomm avatar Nov 09 '20 16:11 malcomm

I also would like to have this environment name in "aws-exports.js" . The workaround above doesn't work for my environment too.

ktoyoda39 avatar Mar 10 '21 08:03 ktoyoda39

// uses the bucket name to determine the environment
function detectEnvironment(config): 'prod' | 'staging' | 'dev' {
  const bucketName = config.aws_user_files_s3_bucket;
  const parts = bucketName.split('-');
  const env = parts[parts.length - 1];

  if (env === 'prod' || env === 'staging' || env === 'dev') {
    return env;
  } else {
    throw new Error(`Unknown environment: ${env}`);
  }
}

Should work for me, having 3 environments (prod, staging and dev). It pulls the suffix of the bucket name, which matches my environment. The only issue I am having right now is the fact that I want it to be in Typescript, and the config does not have a typedef defined within amplify.

Ideally, this 5 year old can be closed with a simple new field soon though..

charlieforward9 avatar May 07 '25 20:05 charlieforward9