Kamal R
Kamal R

Reputation: 51

Accessing files in EC2 from Lambda

I have few EC2 servers in AWS. Whenever the disk space exceeds a limit, i want to delete some files (may be logs folder) in EC2 instance automatically. I am planning to use Lambda and cloudwatch for this. Can i use Lambda to interact with EC2. If not possible, what is the alternate approach to achieve this functionality.

Upvotes: 3

Views: 5356

Answers (3)

John Rotenstein
John Rotenstein

Reputation: 269282

This is not an appropriate use-case for an AWS Lambda function.

AWS Lambda is suitable for tasks where compute is required in response to an event. Your use-case, however, is to manipulate information on an EC2 instance, which does not need cloud compute.

You could run a script on each each computer, triggered by a Scheduled Task.

Alternatively, you could use the Systems Manager Run Command (also known as the EC2 Run Command), which allows you to run commands on multiple Amazon EC2 instances and view the results. This could be used to trigger a local script, or it could pass the whole command to run (including the script). It is purpose-built for the type of task you describe.

Upvotes: 2

Gustavo Tavares
Gustavo Tavares

Reputation: 2805

AWS Lambda has access to your instances if they are available in the internet. If they are not available in the internet, it is possible to give access to AWS lambda using a NAT or instance Gateway in your VPC.

The problem is: access to your instance does not means access to the instances filesystems. To delete the files from Lambda you can use two alternatives:

  1. Configure a network filesystem service in your instances an connect to this services in your lambda function. Using windows you would just "share" your disks, but in that case you would use some SMB library in your lambda code, that "I think" did not have native SMB support. Just keep in mind that your security guy will scream out loud when you propose this alternative.
  2. Create a "agent" in your EC2 instances and keep it running as a Windows Service and call this agent from your lambda function. In that case, the lambda will start the execution of the agent that will be responsible for the file deletion.

Another option, is to follow Ramesh's suggestion and create a Powershell script and configure a cron job. To be easy, you can create a Image with this Powershell script and use the image to initialize each instance. The same solution would be applicable to "the agent" solution in the lambda alternantives.

I think that, in any case, you will need to change something in your 150 servers. Using a customized image can help you to simplify this a little bit, but you will not get a solution without some changes.

Upvotes: 0

According to the following thread, you cannot access files inside a EC2 VM unless you are exposing files to the public using different methodology.

AWS Forum

Quoting from the forum

If you are talking about the underlying EC2 instance, answer is No, you cannot access those files.

However as a solution for your problem, you can used scheduled job to cleanup your files depending your usage. You can use a service or cron job.

Upvotes: -1

Related Questions