Running ajax calls in Django

I have a sample code from here to provision an EC2 instance using boto3 in python.

I need to integrate this in my Django project, but if I inject the sample code in my view, the template will show only after the EC2 instance is provisioned.

How do I run this as and ajax call ? As a subprocess calling this like as a bash command line ?

Celery is the way to go here.

What if I do it as mentioned here ?
And call it via fetch from the template ?

Then you run the risk of the user switching off the page before the process has completed.

If this is a time-consuming operation that may exceed the timeout value for a request, you need to use Celery. If not, then yes, setting this up as a separate view would work fine.

The ajax task itself would take around 40-60 seconds and I would use a progress bar for updates (using websockets).

In that case, I would think about setting it up as a Channels worker task. You could even use a websocket message to initiate the process.