User guide

4. Pass the request object to the AuthorizeSecurityGroupIngress method, which returns an
AuthorizeSecurityGroupIngressResponse object.
var ingressResponse = ec2Client.AuthorizeSecurityGroupIngress(ingress
Request);
Console.WriteLine("New RDP rule for: " + ipRange);
Create a Key Pair Using the SDK for .NET
You must specify a key pair when you launch an EC2 instance and specify the private key of the key pair
when you connect to the instance.You can create a key pair or use an existing key pair that you've used
when launching other instances. For more information, see Amazon EC2 Key Pairs in the Amazon EC2
User Guide for Microsoft Windows Instances.
Enumerating Your Key Pairs
You can enumerate your key pairs and check whether a particular key pair exists.
To enumerate your key pairs
Get the complete list of your key pairs using DescribeKeyPairs with no parameters.The following example
checks each key pair to see whether its name is my-sample-key.
string keyPairName = "my-sample-key";
KeyPairInfo myKeyPair = null;
var dkpRequest = new DescribeKeyPairsRequest();
var dkpResponse = ec2Client.DescribeKeyPairs(dkpRequest);
List<KeyPairInfo> myKeyPairs = dkpResponse.KeyPairs;
foreach (KeyPairInfo item in myKeyPairs)
{
Console.WriteLine("Existing key pair: " + item.KeyName);
if (item.KeyName == keyPairName)
{
myKeyPair = item;
}
}
Creating a Key Pair and Saving the Private Key
The example in this section follows from the example in the previous section. If the key pair doesn't already
exist, create it. Be sure to save the private key now, because you can't retrieve it later.
To create a key pair and save the private key
Create and initialize a CreateKeyPairRequest object. Set the KeyName property to the name of the key
pair.
Pass the request object to the CreateKeyPair method, which returns a CreateKeyPairResponse object.
The response object includes a CreateKeyPairResult property that contains the new key's KeyPair object.
The KeyPair object's KeyMaterial property contains the unencrypted private key for the key pair. Save
the private key as a .pem file in a safe location.You'll need this file when you connect to your instance.
Version v2.0.0
73
AWS SDK for .NET Developer Guide
Tutorial: Creating Amazon EC2 Instances