如何通过 eb cli (django postgres) 迁移 AWS RDS 数据库?

How can I migrate AWS RDS database through eb cli (django postgres)?

我有一个使用 elastic beanstalk CLI 部署的 django 项目。 我添加了一个postgres数据库(AWS RDS),连接是运行nning.

我的数据库还是空的。我如何 运行 迁移命令或 eb shell 中的“python manage.py createsuperuser”?

当我打开 eb ssh <env> 并尝试 ls 时,没有任何显示。使用 cd .. 后退一个目录并再次 ls 显示 ec2-userhealthd网络应用。我没有进入这些文件夹的权限。在 eb ssh 中使用 运行 python 命令是不可能的吗?

我也试过容器命令,.ebextensions/db-migrate.config:

container_commands:
  01_makemigrations:
    command: "python manage.py makemigrations"
    leader_only: true
  02_migrate:
    command: "python manage.py migrate --first main initial && python manage.py migrate"
    leader_only: true
option_settings:
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: <app>.settings

使用 eb deploy 再次提交和部署后,出现此错误:

eb deploy Creating application version archive "app-xxxx". Uploading: [##################################################] 100% Done...

2021-02-22 12:57:12 INFO Environment update is starting.

2021-02-22 12:58:05 INFO Deploying new version to instance(s).

2021-02-22 12:58:21 INFO Instance deployment successfully generated a 'Procfile'.

2021-02-22 12:58:23 ERROR Instance deployment failed. For details, see 'eb-engine.log'.

2021-02-22 12:58:24 ERROR [Instance: i-xxxx] Command failed on instance. Return code: 1 Output: Engine execution has encountered an error..

2021-02-22 12:58:24 INFO Command execution completed on all instances. Summary: [Successful: 0, Failed: 1].

2021-02-22 12:58:24 ERROR Unsuccessful command execution on instance id(s) 'i-xxxx'. Aborting the operation.

2021-02-22 12:58:24 ERROR Failed to deploy application.

ERROR: ServiceError - Failed to deploy application.

为什么命令失败?任何建议表示赞赏。顺便说一句,我用本地 sql db.

部署没有问题

更新:

eb-engine.log 让我参考 cfn-init.log。这是输出:

2021-02-22 16:49:43,624 [ERROR] Command 01_makemigrations (python manage.py makemigrations) failed

2021-02-22 16:49:43,624 [ERROR] Error encountered during build of postbuild_0_django_test: Command 01_makemigrations failed

Traceback (most recent call last): File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 542, in run_config CloudFormationCarpenter(config, self._auth_config).build(worklog)

File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 260, in build changes['commands'] = CommandTool().apply(self._config.commands)

File "/usr/lib/python2.7/site-packages/cfnbootstrap/command_tool.py", line 117, in apply raise ToolError(u"Command %s failed" % name) ToolError: Command 01_makemigrations failed

在路径中我看到 python 2.7 (?) 但实际上 Python 3.7 在 64 位亚马逊上 运行ning Linux 2. 我也尝试执行命令与“python3”。

问题是此时没有加载 django。

解决方案:

container_commands:
  01_migrate:
    command: |
      source $PYTHONPATH/activate
      pipenv run python ./manage.py migrate

将您引向 eb-engine.logcfn-init.log 的日志,但我在 cfn-init-cmd.log 中找到了最后的提示:

2021-02-23 11:08:34,019 P5022 [INFO] ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

我也尝试过“createsuperuser”。完全没有错误,但也没有创建新用户。没有活动的命令行输入 username/password。如果我找到了 createsuperuser 的解决方案,我会尝试更新我的答案。

我可以提出一个替代方法:

与其从 eb shell 中使用 运行ning Django 管理命令,不如将本地 Django 项目的 DATABASE settings 调整到远程数据库,并从本地终端执行管理命令。

这对于 运行 临时命令很有用,例如 createsuperuser,但对于日常维护命令,例如 migrate,您需要坚持使用container_commands 作为部署过程一部分的方法。

最后,如果您决定 运行 createsuperuser,特别是在 eb shell 中,那么您可以使用 container_commands 以非交互方式执行此操作, 配置了一些额外的设置。来自 Django Docs:

When run interactively, this command will prompt for a password for the new superuser account. When run non-interactively, you can provide a password by setting the DJANGO_SUPERUSER_PASSWORD environment variable. Otherwise, no password will be set, and the superuser account will not be able to log in until a password has been manually set for it.

In non-interactive mode, the USERNAME_FIELD and required fields (listed in REQUIRED_FIELDS) fall back to DJANGO_SUPERUSER_<uppercase_field_name> environment variables, unless they are overridden by a command line argument. For example, to provide an email field, you can use DJANGO_SUPERUSER_EMAIL environment variable.