User guide

Table Of Contents
3. (Optional) To launch the instance with an IAM role (p. 47), specify an IAM instance profile in the
RunInstancesRequest.
Note that an IAM user can't launch an instance with an IAM role without the permissions granted by
the following policy.
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"iam:PassRole",
"iam:ListInstanceProfiles",
"ec2:*"
],
"Resource": "*"
}]
}
For example, the following snippet instantiates and configures an IamInstanceProfileSpecification
object for an IAM role named winapp-instance-role-1.
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);
}
Version v2.0.0
45
AWS SDK for .NET Developer Guide
Launch an EC2 Instance