User guide
var runInstancesRequest = new RunInstancesRequest()
{
ImageId = "ami-62c44d52",
InstanceType = "m1.small",
MinCount = 1,
MaxCount = 1,
KeyName = keyName
};
runInstancesRequest.SecurityGroups.Add(secGroupName);
To configure the request object:
• Specify which Amazon Machine Image (AMI) your instances will use by setting the ImageId property
to an appropriate public or privately-provided image ID. For a list of public AMIs provided by
Amazon, go to Amazon Machine Images.
• Specify the instance type by assigning it to the InstanceType property. It must be compatible with
the specified AMI. For more information about instance types, see Instance Families and Types.
• Set MinCount to the minimum number of EC2 instances to be launched. If MinCount is larger
than your authorized maximum, AWS does not launch any instances. The default maximum is 20.
• Set MaxCount to the number of EC2 instances that you want to launch, which cannot be larger
than your authorized maximum. If MaxCount exceeds the number of available instances, AWS
launches as many as possible.
• Set KeyName to the EC2 key name.
• Add one or more security group names to the list of authorized security groups.
Important
Make sure that the specified AMI, key name, and security group names exist in the region
that you specified when you created the client object.
To launch the Amazon EC2 instance with an IAM role
To launch the Amazon EC2 instance with an IAM role, you need to specify an IAM instance profile
in the RunInstancesRequest. The following snippet shows how to instantiate and configure an
IamInstanceProfileSpecification object for an IAM role named winapp-instance-role-1. This
example assumes that you have already created this role using the AWS Management Console or
some other means.
IamInstanceProfile 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
Note
To launch an instance with an IAM role, the profile that you used to create the client object
must grant permissions to use the IAM service in addition to Amazon EC2 permissions. For
more information, see Tutorial: Creating Amazon EC2 Instances (p. 43).
2. Launch the instances by passing the request object to the RunInstances method, as follows:
Version v2.0.0
49
AWS SDK for .NET Developer Guide
Launch Amazon EC2 Instances