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
3,955 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,240 points
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,150 points
15,867 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,120 points
3,446 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer