How to create ami instance programmatically?
This is a link to the Amazon documentation for creating an AMI. Amazon provide a set of command line tools so you should be able to call these from your script.
you may also find these 3rd party scripts useful for creating a bootable EBS from a running instance.
Or it may be more useful to invoke the AWS EC2 APIs more directly from your application.