5

Is it possible to programmatically get/deploy and start an EC2 instance? Essentially pick your instance type, AMI and start it up?

I see the StartInstance method but this only applies to instances already create and stopped in your account.

http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_StartInstances.html

Essentially, what is going on is that I have an automated service that needs multiple EC2 instances for computation. I need to programmatically create a new instance, pick the instance type, pick the AMI, start it up and run some deployment scripts to get things rolling.

I would think there is a way to do this with the AWS SDK but I'm just not seeing it.

On a related note, also need to be able to programmatically destroy a shutdown instance.

3 Answers 3

8

Yes, it's possible.

You use the RunInstances API method.

Launches the specified number of instances using an AMI for which you have permissions.

To completely get rid of an instance, use TerminateInstance.

Shuts down one or more instances. This operation is idempotent; if you terminate an instance more than once, each call succeeds.

The language is a bit confusing because it says "Shuts down one or more instances", but in fact it totally removes them.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, how is this different than RequestSpotInstances ?
RequestSpotInstance allows you to place a bid for an available instance at the current spot market price. This will succeed if your bid is at least equal to the market price. Such instances can be shut down without notice if the current market price later exceeds what you are willing to pay. The spot market is often used for CPU (or GPU) intensive tasks where many instances are needed as cheap as possible, work is divided between all available instances, and the architecture can withstand some instances just going away.
Okay, so RunInstances is intended to fire up On-Demand instances at the current AWS price per hour? If so, that is what I am looking for, thank you.
3

You can write SDK scripts to do the job (create, change and destroy ec2 instances), depend on which language you mastered, such as javascript, java, ruby, python, etc.

But there are easier way to follow up, we call it infrastructure as code, try aws cloudformation or Hashicopy's terraform

With their templates, you can deploy the whole infrastructure (include ec2, rds, vpc, security groups, subnet, etc) as minutes job.

refer:

AWS CloudFormation templates

Terraform AWS PROVIDER

2 Comments

+1 for the CloudFormation service. I never knew that existed. I will need to see if it fits our needs or not, as all we really need to do is boot up an particular AMI and run some scripts. Thanks!
@JakeWilson What did you end up doing? How did it work out?
1

you can try this..

   AmazonEC2Client amazonEc2client = GetAmazonClient(ConfigurationManager.AppSettings["AwsRegionEndPint"]);
     var launchRequest = new RunInstancesRequest()
                    {
                        ImageId = YOUR IMAGE ID,
                        InstanceType = YOUR INSTANCE TYPE,
                        MinCount = 1,
                        MaxCount = 1,
                        KeyName = your keyPairName,
                        SecurityGroupIds =your  groups,
                        SubnetId = your subnet Id,

                    };
                    RunInstancesResponse runInstancesResponse = amazonEc2client.RunInstances(launchRequest);

//code For assign Tag name

var InstanceId = runInstancesResponse.Reservation.Instances[0].InstanceId;
                var trequest = new CreateTagsRequest();
                trequest.Resources=new List<string>(){InstanceId};
                List<Tag> tags=new List<Tag>();
                Tag tag=new Tag("Name","TestCodeFinal");
                tags.Add(tag);
                trequest.Tags = tags;
                amazonEc2client.CreateTags(trequest);
                Reservation reservation = runInstancesResponse.Reservation;

Comments

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.