0

I'm familiar with running the AWS CLI command to copy from a folder to S3 or from one S3 bucket to another S3 bucket:

aws s3 cp ./someFile.txt s3://bucket/someFile.txt

aws s3 cp s3://bucketSource/someFile.txt s3://bucketDestination/someFile.txt

But is it possible to copy files from S3 to an EC2-Instance when you're not on the EC2-Instance? Something like:

aws s3 cp s3://bucket/folder/ ec2-user@1.2.3.4:8080/some/folder/

I'm trying to run this from Jenkins which is why I can't simply run the command on the EC2 like this:

aws s3 cp s3://bucket/folder/ ./my/destination/folder/on/the/ec2

Update:

I don't think this is possible so I'm going to look into using https://docs.aws.amazon.com/cli/latest/reference/ssm/send-command.html

Kyle Bridenstine
  • 6,055
  • 11
  • 62
  • 100

3 Answers3

1

No.

The AWS CLI calls the AWS API. The APIs for Amazon S3 do not have the ability to interact with the operating system on an Amazon EC2 instance.

Your idea of using AWS Systems Manager is a good idea. You can send the command to the instance itself, and the instance can then upload/download objects to Amazon S3.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
0

Since you have SSH access, you could also just run

ssh ec2-user@1.2.3.4:8080 "aws s3 cp s3://bucket/folder/ ./my/destination/folder/on/the/ec2"

... to run the command on the EC2 instance directly.

It's not as efficient as using send-command (because ssh will necessarily pipe the output of that command to your local terminal) but, if you're not transferring millions of files, the tradeoff in simplicity may be acceptable for you.

ephemer
  • 1,239
  • 8
  • 21
0
Using AWS System Manager send command :
#Copying file from S3 bucket to EC2 instance :
$Instance_Id='i-0123456xxx'
aws ssm send-command --document-name "AWS-RunShellScript" --document-version "\$DEFAULT" --targets "Key=instanceids,Values='$Instance_Id'" --parameters '{"commands":["aws s3 cp s3://s3-bucket/output-path/file-name /dirName/ "]}' --timeout-seconds 600 --max-concurrency "50" --max-errors "0" --region REGION_NAME
  • 1
    Hi and welcome to stackoverflow, and thank you for your answer. Rather than just posting a block of code, can you give a short explanation to what the issue is you solved and how you solved it? This will help people who find this question in the future to better understand the issue and how to deal with it. – Plutian Jan 17 '20 at 10:14