你如何从我的Angular应用程序中访问AWS Amplify环境变量?

huangapple go评论54阅读模式
英文:

How can I access AWS Amplify environment variables from my Angular Application?

问题

我对应用程序和系统框架中的环境变量不太熟悉,但我想知道如何从我的代码中访问我的AWS Amplify环境变量?我按照AWS文档上的设置环境变量部分的步骤进行操作(链接:https://docs.aws.amazon.com/amplify/latest/userguide/environment-variables.html),但在尝试从我的TypeScript代码中访问它们时遇到了困难。同样链接中的访问环境变量部分对我来说有些合理,但环境变量仅在构建时可用。我知道这是因为当我在yml文件中的命令中包括'echo "ENV_VAR_NAME=$ENV_VAR_NAME"'时,它会打印出相应的值。我必须在我的amplify.yml构建文件中添加什么内容,以便在部署后尝试运行以下代码时能够成功检索变量:

console.log(process.env.ENV_VAR_NAME)

目前,我的yml文件的构建部分看起来有点像这样(与AWS Amplify文档中的代码片段类似):

build:
  commands:
    - npm run build
    - echo "ENV_VAR_NAME=$ENV_VAR_NAME" >> backend/.env

但当我在我的Angular组件中运行console.log时,它显示为undefined。此外,稍后我想使用AWS提供的系统管理器参数存储存储机密(关于此的详细信息也在上面的链接中提供),我假设访问这些应该有些类似。不确定我可能会误解或不知道的内容,但希望有人可以为我提供一些建设性的反馈。

英文:

Not too experienced with environment variables within app and system frameworks, but I was wondering how I could go about accessing my AWS Amplify environment variable from my code? I followed the steps under Set Environment Variables on AWS's docs at (https://docs.aws.amazon.com/amplify/latest/userguide/environment-variables.html), but am stumped trying to figure out how to access them from my typescript code. The contents under the Access Environment Variables section of the same link provided makes some sense to me, but the environment variable is only available at build time. I know this because when I include 'echo "ENV_VAR_NAME=$ENV_VAR_NAME"' as a command within the yml file it prints out the corresponding value. What must I add to my amplify.yml build file in order for me to be able to successfully retrieve the variable when I try running-

console.log(process.env.ENV_VAR_NAME)

within my typescript code after it has been deployed?

Currently, the build section of my yml file looks something like this (similar to the snippet of code in the aws amplify docs):

build:
  commands:
    - npm run build
    - echo "ENV_VAR_NAME=$ENV_VAR_NAME" >> backend/.env 

But when I run the console.log within my angular component, it shows as undefined. Additionally, later on I would like to store secrets with the systems manager parameter store provided by AWS (details of this also provided in the above link), I assume accessing those should be somewhat similar. Not sure what I may be misunderstanding or unaware of but hopefully someone can provide me with some helpful feedback.

答案1

得分: 1

在进一步研究和试错之后,我能够找到解决方案。对于Angular应用程序,环境变量是通过提供的environment.tsenvironment.prod.ts文件进行配置,而不是使用.env文件。因此,当AWS发现src/.env不是文件或目录时,会导致构建错误。此外,使用'>>'(追加)Linux命令会将该行添加到文件的末尾。由于Angular环境文件包括闭合标签,简单的追加不起作用,因为它们将被破坏并导致语法错误。

为了解决这个问题,你必须在最后一个闭合标签之前追加文本行。在我的YAML文件的构建部分,我添加了以下内容:

sed -i '$i"ENV_VAR_NAME:\"$ENV_VAR_NAME\","' src/environments/environment.prod.ts

通过使用sed命令,我能够在新行(在闭合标签之前)中添加、编辑和保存我的Amplify环境变量,而不会导致任何语法错误。

要从代码中访问这些变量,你只需将环境导入到相应的组件中:

import { environment } from 'src/environments/environment.prod';
// 打印变量
// @ts-ignore
console.log(environment.ENV_VAR_NAME);

但是,只做这个操作会使变量在构建时可用,你的代码仍然会显示“属性'ENV_VAR_NAME'在类型上不存在...”的错误。为了绕过这个问题,我只是通过添加// @ts-ignore来忽略编译器检查这些行。

请记住,当在本地测试时,你仍然需要找到另一种方法来提供这些值,因为这些变量仅在部署后才提供。

英文:

After further research and trial & error, I was able to figure out a solution. When it comes to angular applications, environment variables are configured using the provided environment.ts and environment.prod.ts files instead of a .env file. Because of this, AWS results in a build error when it sees that src/.env is not a file or directory. Also, using the '>>' (append) linux command will add the line to the end of the file. Because the angular environment files include closing tags, a simple append will not work as they will be broken and give you syntax errors.

To fix this, you must append the line of text before the last closing tag. Under the build section of my yaml file I added:

sed -i '$i'"ENV_VAR_NAME:\"$ENV_VAR_NAME\"," src/environments/environment.prod.ts

By using the sed command, I am able to add, edit, and save my amplify environment variable in a new line (before the closing tag) without causing any syntax errors.

To access the variables from your code, you simply import your environment into the corresponding component

import { environment } from 'src/environments/environment.prod'
// prints variable
// @ts-ignore
console.log(environment.ENV_VAR_NAME);

As doing only this makes the variable available at build time, your code will still show a "Property 'ENV_VAR_NAME' does not exist on type..." error. To bypass this, I just ignored the lines from being checked by the compiler by adding "// @ts-ignore"

Keep in mind you will still need to find another way to provide these values when testing locally since the variables are only provided after deploying.

huangapple
  • 本文由 发表于 2023年7月18日 11:27:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定