Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda run command on EC2 instance and get result

I have an EC2 instance that is running a few processes. I also have a Lambda script that is triggered through various means. I would like this Lambda script to talk to my EC2 instance and get a list of running processes from it (Essentially run ps aux on the EC2 box, and read the output).

Now this is easy enough with just one instance and its instance-id. Just SSH in, run the command, get the output, and be on my way. However, I would like to scale this to multiple EC2 instances, for which only the instance-id is known and SSH keys may not be given.

Is such a configuration possible with Lambda and Boto (or other libraries)? Or do I just have to run a microserver on each of my instances that will reply with the given information (something I'm really trying to avoid)

like image 824
Tanishq dubey Avatar asked May 23 '17 19:05

Tanishq dubey


People also ask

Does Lambda function run on EC2?

AWS Lambda can be considered as a framework of EC2 Container Service (ECS) that uses containers to run a piece of code that represents your application. The life cycle of each container is short. The running Lambda function doesn't save its state.

How do I connect an EC2 instance to a Lambda function?

To connect a Lambda function to a dedicated VPC, first peer the dedicated VPC to a default tenancy VPC that contains the function. The solution requires using an Amazon Elastic Compute Cloud (Amazon EC2) Dedicated Instance. Note that your AWS account incurs charges for this instance.


1 Answers

You can do this easily with AWS Systems Manager - Run Command

AWS Systems Manager provides you safe, secure remote management of your instances at scale without logging into your servers, replacing the need for bastion hosts, SSH, or remote PowerShell.

Specifically:

  • Use the send-command API from Lambda function to get list of all processes on a group of instances. You can do this by providing a list of instances or even a tag query
  • You can also use CloudWatch Events to trigger a Run Command directly
like image 108
tomahawk Avatar answered Nov 02 '22 19:11

tomahawk