/// <summary>
        /// Initializes the Amazon EC2 client object and then calls the
        /// DeleteKeyPairAsync method to delete the key pair.
        /// </summary>
        public static async Task Main()
        {
            string keyName = "sdk-example-key-pair";

            // If the key pair was not created in the same AWS Region as
            // the default user on your system, you need to supply
            // the AWS Region as a parameter to the client constructor.
            var client = new AmazonEC2Client();

            var request = new DeleteKeyPairRequest
            {
                KeyName = keyName,
            };

            var response = await client.DeleteKeyPairAsync(request);

            if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine($"Successfully deleted the key pair: {keyName}.");
            }
            else
            {
                Console.WriteLine("Could not delete the key pair.");
            }
        }
예제 #2
0
        public async Task CreateFullStack()
        {
            var ec2Client = new AmazonEC2Client(RegionEndpoint.EUWest1);
            var keyResult = await ec2Client.CreateKeyPairAsync(new CreateKeyPairRequest($"comformation-{Guid.NewGuid()}"));

            try
            {
                // Generate template
                var template = CreateFullTemplate();

                var request = new CreateStackRequest
                {
                    StackName    = $"ComformationTestInfraTemplate-{Guid.NewGuid()}",
                    TemplateBody = template.ToString(),
                    Parameters   = new List <Amazon.CloudFormation.Model.Parameter>
                    {
                        new Amazon.CloudFormation.Model.Parameter
                        {
                            ParameterKey   = "KeyPairName",
                            ParameterValue = keyResult.KeyPair.KeyName
                        }
                    }
                };

                try
                {
                    // Create stack
                    var createResponse = await _cloudformation.CreateStackAsync(request);

                    try
                    {
                        // Get created stack details
                        var stack = await _cloudformation.GetCreatedStack(createResponse.StackId);

                        // Validate created stack
                        AssertCreatedStuck(stack, keyResult.KeyPair);
                    }
                    finally
                    {
                        // cleanup
                        var deleteResponse =
                            await _cloudformation.DeleteStackAsync(new DeleteStackRequest
                        {
                            StackName = createResponse.StackId
                        });
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
            finally
            {
                await ec2Client.DeleteKeyPairAsync(new DeleteKeyPairRequest(keyResult.KeyPair.KeyName));
            }
        }
 public static async Task DeleteKeyPair(AmazonEC2Client ec2Client, string keyName)
 {
     await ec2Client.DeleteKeyPairAsync(new DeleteKeyPairRequest { KeyName = keyName });
 }