amazon-web-servicesamazon-s3amazon-ec2user-data

Copying File From S3 To EC2 by User Data Approach


I have been searching solution for this task, all I find CLI approaches which I don't want.

I simply want:

I have an S3 Bucket, which has one private file, file can be an image/zip file anything.

And I want when I launch any EC2 instance it should have taken that file from S3 bucket to EC2 instance directory.

And for this, I want to use only EC2 User Data Approach.


Solution

  • The User Data field in Amazon EC2 passes information to the instance that is accessible to applications running on the instance.

    Amazon EC2 instances launched with Amazon-provided AMIs (eg Amazon Linux 2) include a program called Cloud-Init that looks at the User Data and, if a script is provided, runs that script the first time that the instance is booted.

    Therefore, you can configure a script (passed via User Data) that will run when the instance is first launched. The script will run as the root user. Your script could copy a file from Amazon S3 by using the AWS Command-Line Interface (CLI), like this:

    #!
    aws s3 cp s3://my-bucket/foo.txt /home/ec2-user/foo.txt
    chown ec2-user foo.txt
    

    Please note that you will need to assign an IAM Role to the instance that has permission to access the bucket. The AWS CLI will use these permissions for the file copy.

    You mention that you do not wish to use the AWS CLI. You could, instead, write a program that calls the Amazon S3 API using a preferred programming language (eg Python), but using the CLI is much simpler.