Front-End Web & Mobile
Configure environment variables and secrets for your Lambda functions with Amplify CLI
With the recent release, Amplify CLI allows you to set environment variables and secrets for your Lambda functions. This feature is available as an advanced setting in Amplify’s “Function” category and allows you to configure environment variables and secrets interactively. The secrets configuration uses AWS Parameter Store backed by a “SecureString” parameter. In addition, this feature works out-of-the-box with Amplify’s backend environment capabilities to enable you to set different environment variables or secrets for each Amplify backend environment such as staging or prod.
This blog post was written by Daijiro Wachi, Sr. Developer Relations Engineer (Frontend Web & Mobile)
AWS Amplify is the fastest and easiest way to build cloud-powered mobile and web apps on AWS. Amplify comprises a set of tools and services that enables front-end web and mobile developers to leverage the power of AWS services to build innovative and feature-rich applications.
1. Setup a new React app with an Amplify backend
Install and configure the Amplify CLI
You can skip this section, if you have the Amplify CLI installed and configured correctly.
Install the Amplify CLI via NPM and configure the Amplify CLI. For the purposes of this demo, you can select all default options:
Setup React app and Amplify project
Run the following command to create a new React project called “react-amplified” or if you already have an existing Amplify project skip to the next section.
Initialize an Amplify project by running:
If you already have an AWS profile set up on your local machine, choose “Yes” when prompted by the CLI and select the profile you would like to use.
If you do not have an AWS profile set up on your local machine, you will be prompted by the CLI to set up a new AWS profile.
2. Add a new Lambda function
Run the following command to create a new Lambda Function:
amplify add function
Select the default options:
Amplify CLI now shows the new options to configure environment variables and secrets. Select Yes
to configure advanced settings.
Tip: if you mistakenly selected No
, run amplify update function
to reconfigure these options.
Select Yes
to configure environment variables and enter name and value of the new environment variable.
Also select Yes
to configure secrets and enter name and value of the new secret.
3. Use configured environment variable and secret in the new Lambda Function
Edit the new Lambda Function to use configured environment variable and secret.
To access the environment variable: Call process.env.<variable-name>
to retrieve the value.
To access the secrets value: Require aws-sdk
to retrieve the secret value through AWS Systems Manager Parameter Store. A code sample on how to retrieve the secret value is automatically added to the top of your function’s file.
Press enter in the terminal to continue once you have updated the Lambda function’s code.
Now, deploy your function to the cloud by running:
4. Verify in AWS Lambda function’s console
Once the deployment is complete, you can test the new Lambda Function that you created in the AWS Lambda console. Open Lambda Function in the AWS console and select the newly created Lambda Function and click “Test”.
Tip: Quickly the AWS Lambda console by running amplify console function
.
You will see the configured environment variable and secret returned.
5. ? Success
Success! In this blog posts, you learned how to create a Lambda function using Amplify CLI and how to set and access these environment variables and secrets. Review the Amplify documentation for environment variables and secrets value for more information.
We hope you like these new features! Let us know your feedback via: