- Newest
- Most votes
- Most comments
For using environment variables in an Amplify Gen 2 setup with Vue, TypeScript, and Vite, there are a few key points to consider:
-
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.
-
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_VARIABLEThis will prompt you to enter the value for the variable.
-
Accessing Environment Variables: In a Vue component or script, you can access these variables using:
import.meta.env.VITE_MY_VARIABLE -
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.
-
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.
-
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:
-
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> -
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
Relevant content
asked 2 years ago
asked 2 years ago
- AWS OFFICIALUpdated 7 months ago
