User guide

InstanceProfile = instanceProfile
3. 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);
}
To launch an EC2 instance in a VPC
1. Create and initialize a network interface.
string subnetID = "subnet-cb663da2";
List<string> groups = new List<string>() { mySG.GroupId };
var eni = new InstanceNetworkInterfaceSpecification()
{
DeviceIndex = 0,
SubnetId = subnetID,
Groups = groups,
AssociatePublicIpAddress = true
};
List<InstanceNetworkInterfaceSpecification> enis = new List<InstanceNetwork
InterfaceSpecification>() {eni};
DeviceIndex
The index of the device on the instance for the network interface attachment.
SubnetId
The ID of the subnet to launch the instance into.
GroupIds
One or more security groups. For more information, see Create a Security Group (p. 69).
AssociatePublicIpAddress
Indicates whether to auto-assign a public IP address to an instance in a VPC.
2. Create and initialize a RunInstancesRequest object. Make sure that the AMI, key pair, and security
group that you specify exist in the region that you specified when you created the client object.
Version v2.0.0
76
AWS SDK for .NET Developer Guide
Tutorial: Creating Amazon EC2 Instances