English 中文(简体)
生态环境特性在埃尔斯特贝兰普拉克不适用。
原标题:Environment properties are not passed to application in Elastic Beanstalk

在部署我的Django项目时,由于<代码>,没有配置数据库环境。 RDS_HOSTNAME in os.environ Return false。 事实上,在部署时没有环境财产。 所有这些财产都是在部署后提供的。

滚动环境:<条码>/opt/elgalbeanstalk/bin/get-config environment>。

{"DJANGO_SETTINGS_MODULE":"myApp.settings","PYTHONPATH":"/var/app/venv/staging-LQM1lest/bin:$PYTHONPATH","RDS_DB_NAME":"ebdb","RDS_HOSTNAME":"xxxx.amazonaws.com","RDS_PASSWORD":"xxxx","RDS_PORT":"xxxx","RDS_USERNAME":"xxxx"}

所有RDS预先确定的财产都是固定的,但是仍然有一些<代码>os.environ无法阅读。

<代码>制定>py文档:

# [...]

if  RDS_HOSTNAME  in os.environ:
    DATABASES = {
         default : {
             ENGINE :  django.db.backends.mysql ,
             NAME : os.environ[ RDS_DB_NAME ],
             USER : os.environ[ RDS_USERNAME ],
             PASSWORD : os.environ[ RDS_PASSWORD ],
             HOST : os.environ[ RDS_HOSTNAME ],
             PORT : os.environ[ RDS_PORT ],
        }
    }

# [...]

我是否必须作任何改动,以便在部署时提供这些财产?

最佳回答

象这样的种子是一种严重的诱杀,美国妇女联盟对此毫不犹豫。 我几乎没有办法完成这项工作,但所有这些努力都需要进入EB环境,并做一些手工工作。

Solution 1

https://stackoverflow.com/users/3167238/hephalump”

  1. A. 创建协会秘密管理人

  2. www.un.org/chinese/ga/president

  3. 然后去到宇航科学院用户群,去到Roles。 从那以后,你可以将政策附在秘密管理人员的上司上。

  4. 一旦完成,就部署该项目。

  5. 然后,对环境的标识(eb ssh environment_name)。

  6. http://www.un.org/Depts/DGACM/index_chinese.htm

  7. 最后,<代码>python3管理.pymigration。

Solution 2

  1. Edit .bash_profile and add export these variables at the end of the file:

     export RDS_DB_NAME=your_dbname
     export RDS_USERNAME=user
     export RDS_PASSWORD=pass
     export RDS_HOSTNAME=host_endpoint
     export RDS_PORT=3306
    
  2. 页: 1

  3. 现在你可以部署项目。

Solution 3

  1. Set all environment properties in EB environment s configuration. (Go to Configuration->Software->Edit->Environment properties and add the key and values). enter image description here

2. 结 论 在<代码>制定时添加这一氮。

    from pathlib import Path
    import os
    import subprocess
    import ast


    def get_environ_vars():
        completed_process = subprocess.run(
            [ /opt/elasticbeanstalk/bin/get-config ,  environment ],
            stdout=subprocess.PIPE,
            text=True,
            check=True
        )

        return ast.literal_eval(completed_process.stdout)
  1. Go to Database section and replace it with this snippet

     if  RDS_HOSTNAME  in os.environ:
         DATABASES = {
              default : {
                  ENGINE :  django.db.backends.mysql ,
                   NAME : os.environ[ RDS_DB_NAME ],
                   USER : os.environ[ RDS_USERNAME ],
                   PASSWORD : os.environ[ RDS_PASSWORD ],
                   HOST : os.environ[ RDS_HOSTNAME ],
                   PORT : os.environ[ RDS_PORT ],
         }
     }
     else:
         env_vars = get_environ_vars()
         DATABASES = {
              default : {
              ENGINE :  django.db.backends.mysql ,
              NAME : env_vars[ RDS_DB_NAME ],
              USER : env_vars[ RDS_USERNAME ],
              PASSWORD : env_vars[ RDS_PASSWORD ],
              HOST : env_vars[ RDS_HOSTNAME ],
              PORT : env_vars[ RDS_PORT ],
         }
     }
    
  2. 项目的实施。

  3. 流向环境(eb ssh environment_name)。

  4. http://www.un.org/Depts/DGACM/index_chinese.htm

  5. 最后,<代码>python3管理.pymigration。

Conclusion:

Solution 1 is little complex and secret manager is not free (30 days trial only).
Solution 2 is simplest one but I do not recommend tempering any file manually on EB.
Solution 3 is a clean solution which I will use. This solution also takes care of this bug fix in future.

问题回答

为了在该系统中使用环境特性,例如,管理传承(使用遗体)的传承,你可以简单地管理:

/opt/elasticbeanstalk/bin/get-config environment | jq -r "to_entries|map("export (.key)= (.value|tostring) ")|.[]" >> /home/ec2-user/.bash_profile 

在你通过SSH标出时,将添加以下所有推进剂: 当然,最好在指挥下:科室。

在欧共体2级中提供环境变量的官方亚太观测所解决方案也确定了这一问题:

https://aws.amazon.com/premiumsupport/knowledge-center/elgal-beanstalk-env-variables-shell/

我得以通过:

  1. Encoding my secrets with base64 encoder
  2. Setting the environment variables with encoded values
  3. Decoding the encoded variables while reading inside Django settings

例-假设我们想读环境变量RDS_PASSWORD=@#%^&

<>Step 1: 将我们的密码与基64统一起来。 能够使用以下简称:

In [1]: import base64

In [2]: password = b @#$%^& 

In [3]: base64.b64encode(password)
Out[3]: b QCMkJV4m 

Step 2: Setting the environment variable in Elastic Beanstalk environment configuration (Go to your EB environment Configuration->Updates, monitoring, and logging->Edit->Environment properties) enter image description here

<>3>标准: 在你的Django服务环境中添加逻辑,将这一变量加以编码。 y

DATABASES = {
     default : {
         ENGINE :  django.db.backends.postgresql ,
         NAME : os.environ[ RDS_DB_NAME ],
         USER : os.environ[ RDS_USERNAME ],
         PASSWORD : base64.b64decode(os.environ[ RDS_PASSWORD ]).decode( utf-8 ),
         HOST : os.environ[ RDS_HOSTNAME ],
         PORT : os.environ[ RDS_PORT ],
    }
}

图64所示特征

  • [A-Z]
  • [a-z]
  • [0-9]
  • [+, /, = (For Padding)]

由EB环境变量值支持的特性

  • Values can contain any alphanumeric characters, white space, and the following symbols: _ . : / = + - @ "

When compared to other solutions in this post, I find this solution much cleaner

你们可以做以下工作,把环境价值扩大到目前的致命环境:

eval $(/opt/elasticbeanstalk/bin/get-config environment | jq -r "to_entries|map("export (.key)= (.value|tostring) ")|.[]")

这将使埃尔伯兰群岛的变量与你出口这些变量相类似:

export Xmx= 12888m 
export HEAP_DUMP_PATH= /heapdumps 
export ENVIRONMENT= qa 




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签