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?
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.
An environment variable is a pair of strings that is stored in a function's version-specific configuration.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With