Copying a file from S3 into my codebase when using Elastic Beanstalk

孤人 提交于 2021-02-11 13:56:00

问题


I have the following script:

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" }

The issue that I am having is that when this is being deployed, the file aren't present in the /var/app/current/storage directory.

I thought that maybe this script was running too soon and the current directory wasn't ready yet, so I tried the ondeck directory and this also doesn't work.

If I change the path to anywhere other than my codebase directory it works, the file is copied from S3.

Any ideas? Thanks.


回答1:


Directives under the "files" key are processed before your web application is set up. You will need to download the file to a tmp file, and then use a container_command to transfer it to your app in the current directory.

This AWS doc mentions near the top the order in which keys are processed. The files key is processed before commands, and commands are run before the application and web server are set up. However, the container_commands section notes that they are used "to execute commands that affect your application source code".

So you should modify your script to something like this:

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"


来源:https://stackoverflow.com/questions/61194547/copying-a-file-from-s3-into-my-codebase-when-using-elastic-beanstalk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!