[英]How to easily deploy sensitive config data to Amazon Elastic Beanstalk for Flask web server
I have a Flask application that I want to deploy to Amazon Elastic Beanstalk, but have the following problem: 我有一个要部署到Amazon Elastic Beanstalk的Flask应用程序,但存在以下问题:
Is there a way we can get our configuration file automatically uploaded to AWS EB alongside the source code when we deploy it? 部署时,有什么方法可以将我们的配置文件与源代码一起自动上传到AWS EB? I considered having it in a file that gets created via configuration in .ebextensions/... but that would just need to be in Git anyway, defeating the object. 我考虑过将其保存在通过.ebextensions / ...中的配置创建的文件中,但是无论如何它只需要放在Git中,就可以击败该对象。
Or do we have to (a) convert all our code to read configuration from environment variables, and (b) create some sort of pre-startup script that injects the values in the current config scripts into the environment? 还是我们必须(a)转换所有代码以从环境变量读取配置,并且(b)创建某种将启动当前配置脚本中的值注入环境的预启动脚本? It wouldn't be ideal to have to maintain 2 totally different ways of reading configuration data into our app depending on whether it's running on AWS or not. 必须维护两种完全不同的方式来读取配置数据到我们的应用程序中,这取决于它是否在AWS上运行。
I have seen this question/answer - How to specify sensitive environment variables at deploy time with Elastic Beanstalk - and I don't think that adequately addresses the issue because it is not very scalable to large numbers of config options nor deals with the fact that Flask typically expects its config in a different format. 我已经看到了这个问题/答案- 如何在部署时使用Elastic Beanstalk指定敏感的环境变量 -我认为这不能充分解决该问题,因为它不能很好地扩展到大量的配置选项,也不能解决以下事实: Flask通常期望其配置采用其他格式。
If you are doing a 12 factor application, the simplest way to handle this kind of configuration is to lookup environment variables by default, and fall back on your hard-coded configurations: 如果您要执行12因子应用程序,则处理这种配置的最简单方法是默认情况下查找环境变量,然后使用硬编码配置:
from os import environ
from flask.config import Config
class EnvConfiguration(Config):
"""Configuration sub-class that checks its environment
If no environment variable is available,
it falls back to the hard-coded value provided"""
def __getitem__(self, key):
environ.get(key, super(EnvConfig, self).__getitem__(key))
You can then override either Flask.config_class
(v1+) or Flask.make_config
to ensure that the configuration used is the one you want: 然后,您可以覆盖Flask.config_class
(v1 +)或Flask.make_config
以确保使用的配置是您想要的配置:
class CustomApp(Flask):
# 1.0+ way
config_class = EnvConfiguration
# 0.8+ way (you only need one of these)
def make_config(self, instance_relative=False):
config = super(CustomApp, self).make_config(instance_relative=instance_relative)
config = EnvConfig(**config)
return config
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.