Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating process.env variables using AWS Amplify?

With serverless we can add process.env variables by creating a configuration file entry like this:

environment:
    STRIPE_SECRET_KEY: ${self:custom.secrets.stripeSecretKey} # Stripe secret API key

And we can access it in our lambda function like this:

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);

How do we do this with AWS Amplify?

like image 311
Ole Avatar asked Aug 11 '19 00:08

Ole


People also ask

What is process ENV variable?

The process. env global variable is injected by the Node at runtime for your application to use and it represents the state of the system environment your application is in when it starts. For example, if the system has a PATH variable set, this will be made accessible to you through process.

Where are AWS environment variables stored?

An environment variable is a pair of strings that is stored in a function's version-specific configuration.

What is the use of .ENV file?

The . env file contains the individual user environment variables that override the variables set in the /etc/environment file. You can customize your environment variables as desired by modifying your . env file.

How do I set environment variables in AWS amplify console?

Sign in to the AWS Management Console and open the Amplify Console . In the Amplify Console, choose App Settings, and then choose Environment variables . In the Environment variables section, choose Manage variables . In the Manage variables section, under Variable, enter your key. For Value , enter your value.

Is the amplify_Amazon_client_ID environment variable an AWS access key or secret key?

The AMPLIFY_AMAZON_CLIENT_ID and AMPLIFY_AMAZON_CLIENT_SECRET environment variables are OAuth tokens, not an AWS access key and secret key. Environment secrets are similar to environment variables, but they are AWS Systems Manager (SSM) Parameter Store key value pairs that can be encrypted.

Are my app's environment variables the same as the amplify console environment variables?

If you are developing your app with a frontend framework that supports its own environment variables, it is important to understand that these are not the same as the environment variables you configure in the Amplify Console.

What is the--CA-bundle environment variable in AWS CLI?

If defined, this environment variable overrides the value for the profile setting ca_bundle . You can override this environment variable by using the --ca-bundle command line parameter. This feature is available only with AWS CLI version 2.


1 Answers

After a year+ of development using amplify framework I figured that you can only specify ENV VARIABLE form from your front-end build process. for lambdas it's a bit tricky. You can add a condition "IsProductionEnv" which is going to place value to ENV Variables for that function depending on amplify env. for production I use "prod" you can use whatever you want. go to your amplify/backend/function/{functionName} folder. there should be {functionName}-cloudformation-template.json file. you need to add one more item to "Conditions" object:

"Conditions":{
  ...,
  "IsProductionEnv": {
      "Fn::Equals": [
        {
          "Ref": "env"
        },
        "prod"
      ]
    }
}

then you need to use that condition at "Resources.Properties.Environment.Variables" :

       "Environment": {
          "Variables": {
            ...,
            "STRIPE_PK": {
              "Fn::If": [
                "IsProductionEnv",
                "pk_live_...",
                "pk_test_..."
              ]
            }
          }
        }

I have "dev" and "prod" amplify env names. it will handle your deployments and manage your env variables based on env for that function.

like image 126
v.nikopolidi Avatar answered Oct 11 '22 04:10

v.nikopolidi