User guide
var instanceProfile = new IamInstanceProfile();
instanceProfile.Id = "winapp-instance-role-1";
instanceProfile.Arn = "arn:aws:iam::4444-5555-6666:instance-profile/winapp-
instance-role-1";
To specify this instance profile in the RunInstancesRequest object, add the following line.
InstanceProfile = instanceProfile
4. Launch the instances by passing the request object to the RunInstances method. Save the IDs of
the instances, as you need them to manage the instances.
Use the returned RunInstancesResponse object to get a list of instance IDs for the new instances.
The Reservation.Instances property contains a list of Instance objects, one for each EC2 instance
that you successfully launched.You can retrieve the ID for each instance from the Instance object's
InstanceId property.
var launchResponse = ec2Client.RunInstances(launchRequest);
List<Instance> instances = launchResponse.Reservation.Instances;
List<String> instanceIds = new List<string>();
foreach (Instance item in instances)
{
instanceIds.Add(item.InstanceId);
Console.WriteLine();
Console.WriteLine("New instance: " + item.InstanceId);
Console.WriteLine("Instance state: " + item.State.Name);
}
Checking the State of Your Instance
Use the following procedure to get the current state of your instance. Initially, your instance is in the
pending state.You can connect to your instance after it enters the running state.
To check the state of your instance
1. Create and configure a DescribeInstancesRequest object. Assign a list of instance IDs to the InstanceId
property and use the Filter property to limit the request to certain instances, such as instances with
a particular user-specified tag.
var instancesRequest = new DescribeInstancesRequest();
instancesRequest.InstanceId = instanceIDs;
2. Call the EC2 client's DescribeInstances method, and pass it the request object from step 1. The
method returns a DescribeInstancesResponse object with the descriptions.
var statusResponse = ec2Client.DescribeInstances(instancesRequest);
3. Enumerate the running instances and determine their status. The
DescribeInstancesResult.Reservations property contains a list of reservations. In this case, there is
only one. Each reservation contains a list of Instance objects.You can get the instance's status from
the InstanceState.Name property.
Version v2.0.0
78
AWS SDK for .NET Developer Guide
Tutorial: Creating Amazon EC2 Instances