Amplify-cli: Multienv: get current environment in frontend code

Created on 8 Jan 2019  路  6Comments  路  Source: aws-amplify/amplify-cli

* Which Category is your question related to? *
Category: Function

* What AWS Services are you utilizing? *
Lambda

* Provide additional details e.g. code snippets *
i'm using multienv version of amplify

how can i get current environment name in code?

use case:
i created amplify function, with name "Validation", and i have environment which called "dev".
function name will be "Validation-dev"
now i want invoke this function from code without hardcode.

personally i would prefer this value in "process.env" variables

question

Most helpful comment

@kaustavghosh06 I believe this individual is asking how to get a reference to the resource ID _of_ the Lambda function _from_ the front-end code (see title of ticket)

@DimaKoval unfortunately the environment name isn't currently included directly in aws_exports.js (which is available to front-end code), _but_ it is indirectly included in certain keys, such as the aws_content_delivery_bucket key. You could parse out the environment name like so:

const awsExports = require('./aws-exports')
const REGEX = /.*-(\w+)/
const env = awsExports.aws_content_delivery_bucket.match(REGEX)[1]

All 6 comments

If you look at your Cloudformation template generated by the Amplify CLI for functions (lambda), youll observe this block:

                  "LambdaFunction": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "Handler": "index.handler",
                "FunctionName": {
                    "Fn::If": [
                        "ShouldNotCreateEnvResources",
                        "test968e4b439",
                        {
                            "Fn::Join": [
                                "",
                                [
                                    "test968e4b439",
                                    "-",
                                    {
                                        "Ref": "env"
                                    }
                                ]
                            ]
                        }
                    ]
                },
                "Role": {
                    "Fn::GetAtt": [
                        "LambdaExecutionRole",
                        "Arn"
                    ]
                },
                "Runtime": "nodejs8.10",
                "Timeout": "25",
                "Code": {
                    "S3Bucket": "test9-20190108121715-deployment",
                    "S3Key": "amplify-builds/test968e4b439-1546978705-latest-build.zip"
                },
                "Environment": {
                    "Variables": {
                        "ENV": {
                            "Ref": "env"
                        }
                    }
                }
            }
        }

The environment varibale is actually passed o your lambda function which you can reference it in you r lambda function as process.env.ENV.

@kaustavghosh06 I believe this individual is asking how to get a reference to the resource ID _of_ the Lambda function _from_ the front-end code (see title of ticket)

@DimaKoval unfortunately the environment name isn't currently included directly in aws_exports.js (which is available to front-end code), _but_ it is indirectly included in certain keys, such as the aws_content_delivery_bucket key. You could parse out the environment name like so:

const awsExports = require('./aws-exports')
const REGEX = /.*-(\w+)/
const env = awsExports.aws_content_delivery_bucket.match(REGEX)[1]

@troygoode Thank you for your answers, this is exactly what i was looking for!

For those who think REGEX makes for unreadable code:

const envStart = awsExports.aws_content_delivery_bucket.lastIndexOf('-');
const env = awsExports.aws_content_delivery_bucket.slice(envStart+1);

For those of us not using aws_content_delivery_bucket, can this be achieved another way? There is nothing in my aws-exports.js that mentions the current environment.

Thanks

Hi, I observed that when we execute the command amplify env list the current envionment is shown precedeed by an asterisk(*). So a workaround could be to use this command:
`const exec = require('child_process').exec;

function execute(command, callback) {
exec(command, (error, stdout, stderr) => {
callback(stdout);
});
}

execute('amplify env list', (value) => {
const r = /[\w-'](?:*+[\w-'])+/g;
const match = r.exec(value);
console.log(match[0].replace('*', ''));
});
`

Was this page helpful?
0 / 5 - 0 ratings