【问题标题】:django project deployed on AWS using postgres - how to create a database使用 postgres 在 AWS 上部署的 django 项目 - 如何创建数据库
【发布时间】:2021-11-05 02:46:56
【问题描述】:

我有一个使用 awsebcli 在 AWS 上部署的 django 项目。

我需要提供一个数据库(postgres),一旦部署了环境,我就从控制台创建一个数据库 我想自动化这个过程,我尝试了一个配置文件

所以我尝试将其添加到项目的 .ebextensions 文件夹中的 django.config 文件中:

  aws:rds:dbinstance:
    DBAllocatedStorage: '5'
    DBDeletionPolicy: Delete
    DBEngine: postgres
    DBEngineVersion: 10.17
    DBInstanceClass: db.t2.micro
    DBPassword: PASSWORD_HERE
    DBUser: USERNAME_HERE
    MultiAZDatabase: false

但是这种方式并没有创建数据库。我必须从控制台创建它,然后使用终端(eb setenv)将数据库的端点和其他参数作为环境变量传递。

有没有办法解决这个问题并使部署自动化?

-----编辑----- 这是我完整的 django.config 文件

 option_settings:
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: "personal_portfolio.settings"
    PYTHONPATH: "/var/app/current:$PYTHONPATH"
    AWS_STORAGE_BUCKET_NAME: "insert-bucketname"
    AWS_S3_REGION_NAME: "us-east-1"
    RDS_USERNAME: "xxxx"
    RDS_PASSWORD: "xxxx"
  aws:elasticbeanstalk:container:python:
    WSGIPath: "personal_portfolio.wsgi:application"
    NumProcesses: 3
    NumThreads: 20
  aws:elasticbeanstalk:environment:process:default:
    HealthCheckPath: "/about"
    MatcherHTTPCode: "200-499"
  aws:elasticbeanstalk:environment:proxy:staticfiles:
    /static: static
    /media: media
  aws:rds:dbinstance:
    DBAllocatedStorage: 5
    DBDeletionPolicy: Delete
    DBEngine: postgres
    DBEngineVersion: 10.17
    DBInstanceClass: db.t2.micro
    DBPassword: lorokpopen
    DBUser: postgres
    MultiAZDatabase: false
container_commands:
  01_makemigrations:
    command: "source /var/app/venv/*/bin/activate && python3 manage.py makemigrations --noinput"
    leader_only: true
  02_migrate:
    command: "source /var/app/venv/*/bin/activate && python3 manage.py migrate --noinput"
    leader_only: true
  03_createsu:
    command: "source /var/app/venv/*/bin/activate && python3 manage.py createsu"
  04_collectstatic:
    command: "source /var/app/venv/*/bin/activate && python3 manage.py collectstatic --noinput"
    leader_only: true

【问题讨论】:

    标签: django postgresql amazon-web-services amazon-elastic-beanstalk


    【解决方案1】:

    你是先使用option_settings命名空间吗?

    .ebextensions/db-instance-options.config

    option_settings:
      aws:rds:dbinstance:
        DBAllocatedStorage: 100
    

    如上所述: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.managing.db.html#using-features.managing.db.namespace

    此页面列出了aws:rds:dbinstance 的所有值: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options-general.html#command-options-general-rdsdbinstance

    现在,从一般的最佳实践来看,这非常适合开发和测试环境。但是,它并不适合生产环境,因为它将数据库实例的生命周期与应用程序环境的生命周期联系在一起。

    您应该将您的数据库实例与您的环境分离,您可以通过 CloudFormation 或 CLI 在 Amazon RDS 中创建数据库实例,并配置您的应用程序以在启动时连接到它,就像您现在所做的那样

    在这个页面中,aws 说了几件事: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/AWSHowTo.RDS.html

    查看 RDS Instance CloudFormation 定义: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-rds-database-instance.html

    【讨论】:

    • 我编辑了插入完整配置文件的问题
    猜你喜欢
    • 2017-05-14
    • 2018-02-24
    • 2012-08-24
    • 2021-12-24
    • 2010-11-04
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 2011-06-14
    相关资源
    最近更新 更多