Yes this is definitely possible.
You can use the AWS APIs in whichever your chosen language is (https://aws.amazon.com/tools/) to communicate with AWS and set up EC2s (machine instances). If you manually set them up in the console first, remote onto them, and then setup all the software etc you require. Them if you save these as AMI (amazon machine images) you can programmatically relaunch as many of these whenever required based on this AMI.
Make sure you are using --instance-initiated-shutdown-behavior terminate to ensure when you shut down these ec2 instances they terminate and stop charging you money.
I would have a go with the AWS Console first, see if you can set up what you want, then look at saving these as AMI's and programmatically launching them