How to upload file from ec2 linux to s3 bucket using lambda function

+1 vote
I want to transfer a file that exists in ec2 Linux at location : /home/test/sample.txt  to S3 bucket location(testbk1122/testing/)  using lambda function .

can anyone tell me how to do this with example
Mar 11, 2020 in AWS by vijaykumar
• 150 points
10,722 views
Thank you for sharing, great example!!
Just one question, what about in case of Windows instance?.
Hi@Rodrigo,

In the case of the Windows system, you need to use the SCP protocol. It will help to connect windows instance.

1 answer to this question.

0 votes

Hi@vijaykumar,

You can transfer file from ec2 instance to s3 bucket using lambda function.

Follow the below steps:

  • Create an IAM role with s3FullAccess and Ec2FullAccess

  • Create a s3 bucket

  • create a lambda function and try to run the below code.

import json
import boto3
from pprint import pprint
def lambda_handler(event, context):
    client = boto3.client("ec2")
    s3 = boto3.client("s3")
    status = client.describe_instance_status(IncludeAllInstances = True)
    s3.upload_file("/home/test/sample.txt","bucket_name","testbk1122/testing/")
    return {
        'statusCode': 200,
        'body' : json.dumps('Hello from lambda')
    }

Make sure your EC2 instance is  in running status.

Hope it will help.

Thank You

answered Mar 11, 2020 by MD
• 95,460 points
Is there any vice versa for this? like from s3 to EC2 or how to upload file to EC2 instance via python code?

if you have aws cli in your system than you can simply do that by using the below command.

$ ssh ec2-user@1.2.3.4:8080 "aws s3 cp s3://bucket/folder/  /destination in ec2 instance"

You can also use this concept to create python script.

Ok thanks. I have one another question like ..is there any python code or python way to upload file to EC2 instance folder?
is any code available? please guide me I am new at AWS

Ok. As you know for authentication you have to add your aws ACCESS keys and all.

First install aws cli in your local system.

https://www.edureka.co/community/62798/how-to-install-aws-cli?show=62798#q62798

Then create one python file and inside this python file write your code.

import os
import credential file of aws account
os.system("aws s3 cp s3://my_bucket/my_folder/my_file.ext my_copied_file.ext")

I didn't understand below line:-

import credential file of aws account

and above code is related to copy file from s3 to EC2 instance? right?

you have to write your aws credential in some file right. So in above line I just told you to import that file name.
so you mean to say .aws folder with credentials file? right?
Yes, try once.
my file is in ec2 under below path

/home/ubuntu/sk/amitt.txt  and while i am passing this path, getting error like path not found for this path.

Hi,

If it is showing path not found means you are sharing the wrong path. Check the path properly using the PWD command. If possible share your code here.

This path I have provided by checking pwd path only. if the same path I am passing in aws cli then it is working perfectly.

Hi,

You are able to access your files from AWS CLI. It means the files are present in your local system not in EC2 instance. I suggest you run your commands in the EC2 instance and paste the screenshot here.

Related Questions In AWS

+3 votes
6 answers

How to move files from amazon ec2 to s3 bucket using command line

Hey, 3 ways you can do this: To ...READ MORE

answered Oct 9, 2018 in AWS by Omkar
• 69,220 points
20,002 views
0 votes
1 answer

How to upload a file from S3 in Lambda?

Yes there is an option where you ...READ MORE

answered Jan 24, 2019 in AWS by Priyaj
• 58,020 points
5,132 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP