How to copy folder from S3 to elastic beanstalk instance on instance creation

时间:2016-11-12 06:03:25

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

I'm running a Django web-app on AWS Elastic Beanstalk that needs specific files available to it to run (in fact, an nltk corpus of stopwords). Since instances come and go, I copied the needed folder to the S3 bucket that was created by my elastic beanstalk and planned to add a copy command using awscli to my elastic beanstalk configuration file. But I can't get it to work.

Instances launched by my beanstalk should have read access to the S3 bucket because this is the bucket created automatically by beanstalk. So beanstalk also created an IAM role aws-elasticbeanstalk-ec2-role which is an instance profile that is attached to every instance it launches. This role includes the AWSElasticBeanstalkWebTier policy which seems to grant both read and write access to the S3 bucket:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "BucketAccess",
      "Action": [
        "s3:Get*",
        "s3:List*",
        "s3:PutObject"
      ],
      "Effect": "Allow",
      "Resource": [
        "arn:aws:s3:::elasticbeanstalk-*",
        "arn:aws:s3:::elasticbeanstalk-*/*"
      ]
    }
  ]
} 

I tried adding the following command to .ebextensions/my_app.config:

commands: 
  01_copy_nltk_data:
    command: aws s3 cp s3://<my_bucket>/nltk_data /usr/local/share/

But I get the following error when I try to deploy even though I can see the folder in my S3 console

Command failed on instance. Return code: 1 Output: An error occurred (404) when calling the HeadObject operation: Key "nltk_data" does not exist

Any ideas?

Thanks!

1 个答案:

答案 0 :(得分:5)

AWS Support有答案:我的nltk_data文件夹中包含子文件夹和文件,因此aws s3 cp命令需要--recursive选项。