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

0 votes
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 in AWS by vijaykumar
• 140 points
150 views

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 by MD
• 8,870 points

Related Questions In AWS

+3 votes
5 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,000 points
7,406 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
• 57,530 points
1,916 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer