User guide
DescribeKeyPairsRequest keyPairDescriptionRequest =
new DescribeKeyPairsRequest();
DescribeKeyPairsResponse keyPairDescriptionResponse =
ec2Client.DescribeKeyPairs(keyPairDescriptionRequest);
List<KeyPairInfo> keyPairs = keyPairDescriptionResponse.KeyPairs;
foreach (KeyPairInfo item in keyPairs)
{
if (item.KeyName == keyName)
{
keyPair = item;
break;
}
}
You can generate a new key pair, as follows:
To create a key pair and obtain the private key
1. Create and initialize a CreateKeyPairRequest object.
var newKeyRequest = new CreateKeyPairRequest();
newKeyRequest.KeyName = "Key Name";
Set the KeyName property to the key pair name.
2. Pass the request object to the Amazon EC2 client object's CreateKeyPair method, which returns a
CreateKeyPairResponse object.
CreateKeyPairResponse newKeyResponse =
ec2Client.CreateKeyPair(newKeyRequest);
privateKey = newKeyResponse.KeyPair.KeyMaterial;
The response object includes a CreateKeyPairResult property that contains the new key's KeyPair
object. The KeyPair object'sKeyMaterial property contains the unencrypted PEM-encoded private
key.You should store this value for later use, because this is the only time that you can retrieve the
private key from KeyMaterial.You can obtain an existing key pair's KeyPair object, as described
earlier in this section, but KeyMaterial will be empty.
Launch Amazon EC2 Instances
You can now launch a set of one or more identically configured Amazon EC2 instances, as follows:
To launch EC2 instances
1. Create and initialize a RunInstancesRequest object.
Version v2.0.0
48
AWS SDK for .NET Developer Guide
Launch Amazon EC2 Instances