Home > Enterprise >  How use web interface to trigger ec2 instance & run a python script?
How use web interface to trigger ec2 instance & run a python script?

Time:04-07

I have a python script that takes an N number of images and converts it to one panorama image.

The workflow is below:

  1. upload n number of images to s3 via the web interface
  2. This should trigger the EC2 instance to start and run the script
  3. Copy images from the S3 to local instance storage
  4. Once the script is executed, then copy the result to the S3 bucket
  5. Script competes it has shown to the web.
  6. Result downloadable from the web

In this process, how can I start the instance once I uploaded the images? How can I know it executed the script?

I need help on the 2 and 5. How to implement it? Should I use any other AWS Services? Note: I don’t need lambda.

CodePudding user response:

If you are frequently converting images, then the traditional architecture would be:

  • Something pushes a message into an Amazon SQS queue (for example, clicking a 'convert' button on a web app, or maybe the upload to S3 triggers an AWS Lambda function that checks that the final image has been uploaded)
  • A worker on an Amazon EC2 instance continuously polls the SQS queue to see if any messages are available
  • If a message is found, it triggers the conversion process

However, if you are only rarely processing images and you need to start/stop the EC2 instance to process the images, then the architecture would be:

  • Images are uploaded to an Amazon S3 bucket
  • Amazon S3 triggers an AWS Lambda function for each upload. The Lambda function determines whether all images have been uploaded. If so, it then calls StartInstances() to turn on an EC2 instance that has already been created and has all necessary software loaded. (Alternatively, if you have a web app that is controlling the process, it can start the EC2 instance and the Lambda function is not required.)
  • Any script on the EC2 instance stored in /var/lib/cloud/scripts/per-boot/ will automatically run when the instance is started (note that this is different to User Data scripts that only run on the first boot of an instance). The script should grab the files from the S3 bucket and process the image
  • When processing is complete, the script can turn off the EC2 instance using sudo shutdown now -h

However, the above is an asynchronous process. That is, the web app (or Lambda function) starts the instance, but no 'response' is returned. You will need some mechanism to 'know' that the instance has finished its job. This could be as simple as looking in the S3 bucket for an output file with a particular name. This name could be derived from the names of the input images, or perhaps your app creates a configuration file in S3 that contains the filenames of the input images and also specifies the filename for the output image.

Therefore, your Step 5 ("Script competes it has shown to the web") will probably need the web page to check for a result every 15 seconds, rather than simply 'showing' a result when it happens.

See also: Auto-Stop EC2 instances when they finish a task - DEV Community

  • Related