简体   繁体   English

如何轻松地将敏感配置数据部署到Flask Web服务器的Amazon Elastic Beanstalk

[英]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应用程序,但存在以下问题:

  1. Our configuration contains a lot of secret API keys, so we don't have it checked into Git. 我们的配置包含许多秘密API密钥,因此我们没有将其检入Git。
  2. Elastic Beanstalk only deploys the files that are committed to Git, so our config doesn't get uploaded as part of the deployment process. Elastic Beanstalk仅部署提交给Git的文件,因此在部署过程中不会上载我们的配置。
  3. Elastic Beanstalk offers a way to set and read environment variables - but Flask typically expects environment variables to be set in Python files that are imported at runtime. Elastic Beanstalk提供了一种设置和读取环境变量的方法-但Flask通常希望在运行时导入的Python文件中设置环境变量。

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.

相关问题 在AWS Elastic Beanstalk上部署python Web服务器 - Deploy python web server on AWS Elastic Beanstalk 如何使用 Amazon AWS Elastic Beanstalk 部署私有 python pip 依赖项? - How to deploy private python pip dependency with Amazon AWS Elastic Beanstalk? 如何使用应用程序工厂将Flask应用程序部署到AWS Elastic beantalk - How to deploy Flask app to AWS Elastic beanstalk using an application factory 如何将 Visual Studio Flask 应用程序部署到 Elastic Beanstalk - How to deploy Visual Studio Flask app to Elastic Beanstalk 如何在AWS弹性beanstalk上部署结构化Flask应用程序 - How to deploy structured Flask app on AWS elastic beanstalk 如何在 Elastic Beanstalk 中部署使用 .csv 文件作为数据库的 web 应用程序? - How to deploy web app that used .csv file as database in Elastic Beanstalk? 如何从Jenkins将Python应用程序部署到Amazon Elastic Beanstalk? - How do I deploy a Python application to Amazon Elastic Beanstalk from Jenkins? Amazon Elastic Beanstalk:如何设置 wsgi 路径? - Amazon Elastic Beanstalk : how to set the wsgi path? 如何在Amazon Elastic Beanstalk上安装PythonMagick - How to install PythonMagick on Amazon Elastic Beanstalk 尝试将Flask App部署到AWS Elastic Beanstalk时出错 - Error when Trying to Deploy Flask App to AWS Elastic Beanstalk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM