Home > Net >  Copy files from ec2 to s3
Copy files from ec2 to s3

Time:02-24

How do copy files from given location from Linux ec2 instances to S3 bucket using Java lambda? Is there any better way to copy the files from ec2 to s3 ?

Regards,

Chamu.

CodePudding user response:

AWS Lambda functions do not have access to disks on Amazon EC2 instances.

It is normally easier to send files from the Amazon EC2 instance to Amazon S3. This can be done with the AWS Command-Line Interface (CLI), or via a Java program that uses the AWS SDK for Java.

CodePudding user response:

You can use the command: "aws s3 cp my_copied_file.ext s3://my_bucket/my_folder/my_file.ext"

Please gothrough the link[1] [1]: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AmazonS3.html

CodePudding user response:

First,you need to narrow down your question to avoid broad answers. 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/testfile_sample.txt","bucket_name","testfile/testing/")
    return {
        'statusCode': 200,
        'body' : json.dumps('Hello AWS World')
    }

Please go through the following AWS documentation guide lines:

  1. Boto3
  2. Use Amazon S3 with Amazon EC2
  3. AWS Lambda
  • Related