AWS Elastic Beanstalk权限的最佳实践

时间:2015-12-03 16:24:18

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

我是AWS Elastic Beanstalk的新手并且仍然在学习它。请分享您认识的最佳实践。

" eb deploy"将Django应用程序上传到AWS。但是,' ./manage makemigrations'将会有权限被拒绝错误,例如

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/core/management/commands/makemigrations.py", line 143, in handle
    self.write_migration_files(changes)
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/core/management/commands/makemigrations.py", line 172, in write_migration_files
    with open(writer.path, "wb") as fh:
PermissionError: [Errno 13] Permission denied: '/opt/python/bundle/3/app/myproj/myapp/migrations/0001_initial.py'

注意用户是ec2用户,他只拥有/opt/python/bundle/3/app/myproj/myapp/migrations/&#39;的读取权限。但是,使用root用户可以成功执行具有安全性问题的迁移。

另一个类似的问题是, ec2-user没有WRITE访问&#39; /var/log/&#39;,哪里会存储日志文件?存储在/home/ec2-user内?如果环境终止,主目录将被清除,对吗?

1 个答案:

答案 0 :(得分:2)

您不应该在EB实例上调用makemigrations。 makemigrations是您将其称为开发的一部分,然后将所有生成的文件(如“0001_initial.py”)置于版本控制之下,并作为eb deploy的一部分进行部署。

在你的.ebextensions上,你只需要调用只能写入数据库的“migrate”(在进行数据的实际迁移时):

container_commands:
   01_migrate:
     command: "django-admin.py migrate --noinput"
     leader_only: true

其中一些示例还建议在.ebextensions上调用collectstatics,但我建议不要这样做。假设您正在使用django-storage这样的东西并将所有静态存储在S3(或更好的CloudFront)上,那么最好在您自己的本地计算机上调用collectstatics作为开发的一部分。 e.g。

python manage.py collecstatics
eb deploy

我对日志问题没有真正的问题。我只是使用eb日志,所以我只是将我想要看到的内容打印到控制台,EB将负责这些日志。但我知道您有权写入/ tmp,因此可以作为日志的位置。