JBS
JBS

Reputation: 686

AWS Beanstalk - On Deploy, Copy file with env variables from S3 bucket?

I'm attempting to copy a file from an S3 bucket to an NodeJS app on Beanstalk when I deploy a new version.

What I've done so far:

  1. Created a S3 bucket and uploaded the files.
  2. Created IAM Policy that allows the Beanstalk instance (aws-beanstalk-ec2-role) to read the bucket files.
  3. Set Bucket policy to allow aws-beanstalk-ec2-role to access the files.
  4. Created a config file in my app at .ebextensions/app.config with this:
  Resources:
    AWSEBAutoScalingGroup:
        Metadata:
            AWS::CloudFormation::Authentication:
                S3Auth:
                    type: "s3"
                    buckets: ["bucket-name"]
                    roleName: aws-elasticbeanstalk-ec2-role

  files:
    "/tmp/deployment/application/file_needed.json" :
      mode: "000644"
      owner: root
      group: root
      authentication: "S3Auth"
      source: https://[bucket-name].s3.[region].amazonaws.com/origin_file.json

The app deploys with no errors, but the file, file_needed.json, does not get copied?

Update: After trying a different folder destination (/tmp), I've found that files are copied. What I know is that Beanstalk first extracts the files to /tmp/deployment/application/, and then moves them to /var/app/current. How can I get the files to copy to the root directory of the app?

Upvotes: 1

Views: 1372

Answers (2)

SujithaW
SujithaW

Reputation: 510

I followed this Download S3 and Add to App Directory

I used the default bucket created by EB and added a new role with permissions to read from S3

Also to copy the files you should use container_commands https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#linux-container-commands

Whatever files you want to copy need to be added to the staging folder, this is mentioned in the above link

this is what I used to copy the file to my deployed code

    command: "mv /home/ec2-user/.env.production /var/app/staging/.env.production"

Also if you want your file to readable change the owner and group to webapp

this command shows you all available users less /etc/passwd

To find your apps user group you can also just check the permission of the files in app/current

Upvotes: 0

Dennis Traub
Dennis Traub

Reputation: 51654

The reason might be a case-sensitivity issue. In AWS::CloudFormation::Authentication the type is set to "s3" instead of "S3".

Also try removing “region” from the bucket URL:

source: https://bucket-name.s3.amazonaws.com/origin_file.json

Upvotes: 0

Related Questions