Skip to content

Using Environment Variables in an Amplify/VUE/TS/Vite setup

0

I'd like to get some advice on the best method to access environment variables (EV) in Amplify Gen 2

I'd like to store a value as an EV - it doesn't need to be secret. I have read a number of pieces of documentation but can't find a definitive answer. The official documentation is here. The documentation, even when selecting the VUE framework/language selection at the top of the page doesn't change and still shows details such as REACT_APP_TEST_VARIABLE. Other documentation I've read is that VUE needs EVs to begin with VUE_APP and further still that Vite needs them to begin VITE_APP_

I'd like to be able to read the EVs from wherever Amplify stores them, display them on a VUE page and then allow the value to be changed. I'm not after advice on how to build the page to do that, I just need to be able to read and write the EVs.

Things I have tried so far:

  • Using amplify.yml to have commands such as this: export VITE_APP_startDate=$(aws ssm get-parameter --name "/amplify/shared/<AppID>/VITE_APP_startDate" --with-decryption --query "Parameter.Value" --output text) and also using echo to pipe the value to .env: echo "VITE_APP_startDate=$VITE_APP_startDate" >> .env Trying to access these variables through process.env returns nothing

  • I have written a lambda function through Amplify using { SSMClient, GetParametersCommand } from '@aws-sdk/client-ssm' and this doesn't return any values

Anyone who could provide advice or example code that works would be gratefully received!

asked 2 years ago1.1K views

1 Answer
0

For using environment variables in an Amplify Gen 2 setup with Vue, TypeScript, and Vite, there are a few key points to consider:

  1. Naming Convention: For Vite projects, environment variables should indeed start with "VITE_". This is the correct prefix for Vite to expose these variables to your client-side code.

  2. Setting Environment Variables: In Amplify Gen 2, you can set environment variables using the Amplify CLI or the AWS Management Console. For non-sensitive data, you can use:

    npx ampx sandbox secret set MY_VARIABLE
    

    This will prompt you to enter the value for the variable.

  3. Accessing Environment Variables: In a Vue component or script, you can access these variables using:

    import.meta.env.VITE_MY_VARIABLE
  4. Reading and Writing: To read the variables, simply use the method above. For writing or updating variables, you'll need to use AWS APIs or CLI commands, as client-side code cannot directly modify environment variables for security reasons.

  5. Lambda Function Approach: Your approach of using a Lambda function to interact with SSM Parameter Store is a good one for more dynamic management of these values. You can create API endpoints that your Vue application can call to read or write these values.

  6. Amplify.yml Configuration: Your approach in the amplify.yml file to export variables is on the right track. However, make sure these variables are being properly passed to your Vite build process.

To display and potentially update these variables:

  1. Create a Vue component that reads the environment variable:

    <template>
      <div>Start Date: {{ startDate }}</div>
    </template>
    
    <script setup lang="ts">
    import { ref } from 'vue'
    
    const startDate = ref(import.meta.env.VITE_APP_startDate)
    </script>
  2. For updating, create an API (using Amplify's API category or a custom Lambda function) that can update the SSM parameter. Then, call this API from your Vue component when you want to update the value.

Remember, any changes to environment variables typically require a rebuild and redeployment of your application to take effect on the client side. For more dynamic behavior, consider using SSM Parameter Store or a similar service and fetching the values at runtime through an API.

Sources
New in AWS Amplify: Expanded Fullstack Deployment Capabilities for Teams of All Sizes | Front-End Web & Mobile
Using environment variables in AWS AppSync - AWS AppSync

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.