Ejemplo n.º 1
0
        /// <summary>
        /// Initializes the Amazon EC2 client object and then calls the
        /// CreateKeyPairAsync method to create the new key pair.
        /// </summary>
        public static async Task Main()
        {
            string keyName = "sdk-example-key-pair";

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

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

            var response = await client.CreateKeyPairAsync(request);

            if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
            {
                var kp = response.KeyPair;
                Console.WriteLine($"{kp.KeyName} with the ID: {kp.KeyPairId}.");
            }
            else
            {
                Console.WriteLine("Could not create key pair.");
            }
        }
Ejemplo n.º 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 <KeyPair> CreateKeyPair(AmazonEC2Client ec2Client, string keyName, string keyOutputDirectory = null)
        {
            var keyPair = (await ec2Client.CreateKeyPairAsync(new CreateKeyPairRequest {
                KeyName = keyName
            }))?.KeyPair;

            if (!string.IsNullOrWhiteSpace(keyOutputDirectory))
            {
                await SaveKeyPairToDisc(keyName, keyOutputDirectory, keyPair);
            }
            return(keyPair);
        }
Ejemplo n.º 4
0
        public string CreateKeyPair()
        {
            string      keyPairName = "Ec2Test";
            KeyPairInfo myKeyPair   = null;

            try
            {
                var dkpRequest  = new DescribeKeyPairsRequest();
                var dkpResponse = amazonEC2Client.DescribeKeyPairsAsync(dkpRequest).Result;
                List <KeyPairInfo> myKeyPairs = dkpResponse.KeyPairs;

                foreach (KeyPairInfo item in myKeyPairs)
                {
                    Console.WriteLine("Existing key pair: " + item.KeyName);
                    if (item.KeyName == keyPairName)
                    {
                        myKeyPair = item;
                    }
                }

                if (myKeyPair == null)
                {
                    var newKeyRequest = new CreateKeyPairRequest()
                    {
                        KeyName = keyPairName
                    };
                    var ckpResponse = amazonEC2Client.CreateKeyPairAsync(newKeyRequest).Result;
                    Console.WriteLine();
                    Console.WriteLine("New key: " + keyPairName);

                    // Save the private key in a .pem file
                    using (FileStream s = new FileStream(keyPairName + ".pem", FileMode.Create))
                        using (StreamWriter writer = new StreamWriter(s))
                        {
                            writer.WriteLine(ckpResponse.KeyPair.KeyMaterial);
                        }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(keyPairName);
        }