Search code examples
javaamazon-web-servicesamazon-ec2jsch

How to execute a program with command line arguments on EC2 remotely (java program)?


I want to start a EC2 instance from java code and execute python code on that EC2 instance.

  1. Create a custom AMI with python console application
  2. launch EC2 instance using AMI from java application
  3. launch multiple instances of python program with different command line arguments from java code.

I have done some analysis where I can create AMI with python code and launch EC2 instance, but I am looking for a way to launch multiple instances of python program with command line arguments.

enter image description here

Basically I want to build a scalable system which will start EC2 instance and launch multiple python processes as per requirement and will remove EC2 instance as work is done.

UPDATE: Although accepted answer is correct there is another way to execute scripts remotely using ssh tunneling using JSCH java library , here is the Gist for sample code

This can be used for communication with non AWS machine with SSH service on.


Solution

  • You can use AWS Systems Manager to invoke shell commands on your remote EC2 instance.

    1. Create a document in AWS SSM which invokes the shell commands.
    2. Create an EC2 role which allows SSM to run commands on EC2 instance on your behalf.
    3. Attach that role to that instance and wait for that instance to come under Managed Instance inside SSM.
    4. Run that SSM document on that instance from Java.