0

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.

3
  • May I ask how do you start your python program? Is it running over Django or you are using plain python scripts? Commented Nov 26, 2019 at 10:56
  • Its plain script, I am looking for a way to start it from java server which will is going to start AMI. Commented Nov 26, 2019 at 13:02
  • AWS has SDK aws.amazon.com/sdk-for-java Commented Nov 26, 2019 at 20:58

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

5 Comments

This need to be executed on EC2 instance and from java program which is running on different server. so this approach wont work
is this accessible using java API
I got the fair idea about SSM documents, but having hard time finding the java sdk samples/examples, can you provide any?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.