VOOZH about

URL: https://repost.aws/questions/QUAECsXArNQv2DcHMt0OOWTw/how-to-upload-file-from-local-machine-to-an-ec2-instance-planning-to-use-aws-sdk-via-python

⇱ How to upload file from local machine to an EC2 instance? (Planning to use AWS SDK via Python) | AWS re:Post


Skip to content

How to upload file from local machine to an EC2 instance? (Planning to use AWS SDK via Python)

0

As the subject says, I would like to upload a file (over 300 MB) to an EC2 instance using AWS SDK in Python. Any help would be appreciated. If there is a better way to do this porgrammatically then I'm open to recommendations.

Topics
Compute
Language
English

asked 3 years ago1.8K views

1 Answer
  • Newest
  • Most votes
  • Most comments
Are these answers helpful? Upvote the correct answer to help the community benefit from your knowledge.
0

Hello.

You cannot directly upload files to EC2 with the AWS SDK.
One way is to use an S3 event trigger to run Lambda and have it execute a Systems Manager RunCommand.
The Lambda code looks like this:

from datetime import datetime
import boto3
import os

ssm = boto3.client('ssm')

instance_id = os.environ['instance_id']
now = datetime.now()
file_name = 'file_name' + now.strftime('%Y%m%d%H%M%S')

def lambda_handler(event, context):
 bucket = event['Records'][0]['s3']['bucket']['name']
 key = event['Records'][0]['s3']['object']['key']
 response = ssm.send_command(
 InstanceIds=[instance_id],
 DocumentName='AWS-RunShellScript',
 Parameters={
 'commands': [
 f'aws s3 cp s3://{bucket}/{key} /home/ec2-user/{file_name}'
 ],
 'executionTimeout': ['3600'],
 }
 )
EXPERT

answered 3 years ago

  • Gableo
    3 years ago

    Okay, i see. As a follow up question, what would be the best way to upload a 20 GB file to an EC2 instance without using AWS SDK and S3?

  • Riku_Kobayashi EXPERT
    3 years ago

    How about uploading with SCP command? If you really want to upload to EC2 with Python, you can use a module called "paramiko" to operate SFTP from Python code. https://www.paramiko.org/