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 in AWS by vijaykumar
• 150 points
1,892 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
• 56,780 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.

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,030 points
12,269 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,640 points
2,758 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer