在 aws 上部署时如何使用环境变量 [英] How environment variables are used when deployed on aws

查看:25
本文介绍了在 aws 上部署时如何使用环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个在本地使用环境变量的 Web 应用程序,我想将其投入生产(在线).我试图找出如何在 AWS 上设置环境变量.

I am building a web application that utilises environment variables locally, and I want to put this on production (online). I am trying to find out how to set environment variables on AWS.

这是一个 node.js 应用程序,我在 AWS 上使用弹性 beantalk 进行部署.我浏览了 https://docs.aws.amazon.com/cloud9/latest/user-guide/env-vars.html#env-vars-env-list ,但我不确定哪个选项适用于我.

This is a node.js application and I am using elastic beanstalk on AWS for deployment. I have looked through https://docs.aws.amazon.com/cloud9/latest/user-guide/env-vars.html#env-vars-env-list , but I'm unsure of which option applies to me.

我在本地的 .env 文件包含像

The .env file I have locally contains lines like

密码=我的密码

我正在使用 dotenv 包,带有 require('dotenv').config();在相应的文件中.我正在通过诸如 process.env.PASSWORD 之类的东西访问我代码中的环境变量(使用前面提到的 .env 文件中的一行示例).

and I am using the dotenv package, with require('dotenv').config(); in the appropriate files. I am accessing environment variables in my code through things like process.env.PASSWORD (using the aforementioned example of a line in the .env file).

我尝试搜索了几个地方并看到了各种选项,但我不确定哪一个适用于我的环境变量.

I've tried searching several places and am presented with various options, but I'm not sure which one applies to my environment variables.

推荐答案

您所关注的链接可能会对您管理的 ec2 机器有所帮助,但如果您正在使用 Elasticbeanstalk,我建议您使用 elasticbeanstalk 提供的环境变量配置.

The link you are following may help you in ec2 machine that is mangages by you, but if you are working Elasticbeanstalk I will recommend using Environment variable configuration provided by elasticbeanstalk.

我不赞成 .env 在 Elasticbeanstalk、ECS 和许多其他服务的情况下,AWS 提供简单和开箱即用的功能来在配置和 中设置环境变量.env 将环境写入文件,该文件的安全性低于系统环境变量.

I am not in favour of .env in case of Elasticbeanstalk, ECS and many other services where AWS provide easy and out of the box feature to set environment variable in the configuration and .env write environment to file which is less secure then system environment variable.

Elasticbeanstalk ENV 有趣的部分是,系统环境变量比 .env 环境变量具有更高的外围,但最好不要将 dotenv 放在 elasticbeanstalk 上.

The interesting part of Elasticbeanstalk ENV is, the system environment variable has higher periphery then .env environment variable but better to not place dotenv on elasticbeanstalk.

环境属性和其他软件设置

您可以使用环境属性来传递秘密、端点、调试设置和其他信息到您的应用程序.环境属性可帮助您在多种环境中运行您的应用程序不同的目的,例如开发、测试、分期和生产.

You can use environment properties to pass secrets, endpoints, debug settings, and other information to your application. Environment properties help you run your application in multiple environments for different purposes, such as development, testing, staging, and production.

elasticbeanstalk-deploy_nodejs

Example .ebextensions/options.config

option_settings:
  aws:elasticbeanstalk:application:environment:
    API_ENDPOINT: www.example.com/api

现在你需要的一切

var endpoint = process.env.API_ENDPOINT

环境属性

Environment Properties 部分允许您指定环境正在运行的 Amazon EC2 实例上的配置设置你的申请.这些设置作为键值对传递给应用程序.

The Environment Properties section lets you specify environment configuration settings on the Amazon EC2 instances that are running your application. These settings are passed in as key-value pairs to the application.

在 AWS Elastic Beanstalk 中运行的 Node.js 环境中,您可以使用 process.env.ENV_VARIABLE 访问环境变量类似于下面的例子.

Inside the Node.js environment running in AWS Elastic Beanstalk, you can access the environment variables using process.env.ENV_VARIABLE similar to the following example.

var endpoint = process.env.API_ENDPOINT

这篇关于在 aws 上部署时如何使用环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆