【问题标题】:Copying a file from S3 into my codebase when using Elastic Beanstalk使用 Elastic Beanstalk 时将文件从 S3 复制到我的代码库中
【发布时间】:2020-07-26 09:43:18
【问题描述】:

我有以下脚本:

Parameters:
  bucket:
    Type: CommaDelimitedList
    Description: "Name of the Amazon S3 bucket that contains your file"
    Default: "my-bucket"
  fileuri:
    Type: String
    Description: "Path to the file in S3"
    Default: "https://my-bucket.s3.eu-west-2.amazonaws.com/oauth-private.key"
  authrole:
    Type: String
    Description: "Role with permissions to download the file from Amazon S3"
    Default: "aws-elasticbeanstalk-ec2-role"

files:
  /var/app/current/storage/oauth-private.key:
    mode: "000600"
    owner: webapp
    group: webapp
    source: { "Ref" : "fileuri" }
    authentication: S3AccessCred

Resources:
  AWSEBAutoScalingGroup:
    Type: "AWS::AutoScaling::AutoScalingGroup"
    Metadata:
      AWS::CloudFormation::Authentication:
        S3AccessCred:
          type: "S3"
          roleName: { "Ref" : "authrole" }
          buckets: { "Ref" : "bucket" }

我遇到的问题是,在部署它时,/var/app/current/storage 目录中不存在该文件。

我认为这个脚本可能运行得太快了,current 目录还没有准备好,所以我尝试了ondeck 目录,这也不起作用。

如果我将路径更改为我的代码库目录以外的任何其他位置,则该文件将从 S3 复制。

有什么想法吗?谢谢。

【问题讨论】:

    标签: amazon-web-services amazon-s3 amazon-elastic-beanstalk


    【解决方案1】:

    “文件”键下的指令在您的网络应用程序设置之前被处理。您需要将文件下载到 tmp 文件,然后使用 container_command 将其传输到当前目录中的应用程序。

    AWS doc 在顶部附近提到了处理密钥的顺序。 files 密钥在 commands 之前处理,commands 在应用程序和 Web 服务器设置之前运行。但是,container_commands 部分指出它们用于“执行影响应用程序源代码的命令”。

    所以你应该把你的脚本修改成这样:

    Parameters: ...
    Resources: ...
    
    files:
      "/tmp/oauth-private.key":
        mode: "000600"
        owner: webapp
        group: webapp
        source: { "Ref" : "fileuri" }
        authentication: S3AccessCred
    
    container_commands:
      file_transfer_1:
        command: "mkdir -p storage"
      file_transfer_2:
        command: "mv /tmp/oauth-private.key storage"
    

    【讨论】:

    • 这是完美的。成功了。我很确定您需要成为机器人才能理解和浏览 AWS 文档。这么多自相矛盾的文章。虽然,只是一项修正案。 file_transfer_3 不是必需的,因为 mv 已经将其删除。
    • 啊,是的,我会解决的。我想我是从我出于某种原因使用cp 的示例中复制的。很高兴这有帮助!
    • 看准了!再次感谢!
    猜你喜欢
    • 2017-03-11
    • 2013-08-20
    • 2014-10-30
    • 2021-03-26
    • 2021-12-01
    • 2019-05-14
    • 2018-12-08
    • 2014-03-11
    • 2020-10-26
    相关资源
    最近更新 更多