public static CreateStackResponse CreateStack(string templateUri, string awsCredentialsAccessKey, string awsCredentialsSecretKey)
        {
            AmazonCloudFormationClient client = new AmazonCloudFormationClient(RegionEndpoint.USEast1);


            CreateStackRequest request = new CreateStackRequest
            {
                DisableRollback = true,
                TemplateURL     = templateUri,
                StackName       = new Uri(templateUri).Segments[new Uri(templateUri).Segments.Length - 1].Replace(".template", string.Empty).Replace('.', '-'),
            };

            request.Capabilities.Add("CAPABILITY_IAM");

            try
            {
                var response = client.CreateStack(request);

                if (response.HttpStatusCode < HttpStatusCode.OK || response.HttpStatusCode >= HttpStatusCode.MultipleChoices)
                {
                    throw new Exception(response.ToString());
                }
                return(response);
            }
            catch (Amazon.Runtime.Internal.HttpErrorResponseException ex)
            {
                throw new Exception(ex.Message);
            }
        }
Example #2
0
        static void TestParentChildTemplates()
        {
            string bucket_Name  = QSS3BucketName;
            string templateName = QSS3KeyPrefix + TdwUtils.cfClassPathParentSubnet.Replace("tdw_cf_template\\", "");
            string stack_name   = templateName.Replace("-", "");

            stack_name = stack_name.Replace(".template", "");
            string dataPath = null;

            byte[]            dataBytes = null;
            PutObjectRequest  request   = null;
            PutObjectResponse response  = null;
            AmazonS3Client    s3Client  = new AmazonS3Client();

            bucket_Name = TdwUtils.CreateBucket(s3Client, QSS3BucketName);

            GetObjectRequest getObjectRequest = new GetObjectRequest
            {
                BucketName = bucket_Name,
                Key        = templateName,
            };

            string data = null;

            using (GetObjectResponse getObjectResponse = s3Client.GetObject(getObjectRequest))
            {
                using (var stream = getObjectResponse.ResponseStream)
                    using (var reader = new StreamReader(stream))
                    {
                        data = reader.ReadToEnd();
                    }
            }

            Amazon.CloudFormation.AmazonCloudFormationClient cfClient = new AmazonCloudFormationClient();

            try
            {
                DeleteStackRequest deleteRequest = new DeleteStackRequest()
                {
                    StackName = stack_name
                };
                cfClient.DeleteStack(deleteRequest);
            }
            catch (Exception ex)
            {
                ex = null;
            }

            List <string> CfCapabilities = new List <string>();

            CfCapabilities.Add("CAPABILITY_IAM");
            CreateStackRequest stackRequest = new CreateStackRequest()
            {
                StackName = stack_name, TemplateBody = data, Capabilities = CfCapabilities
            };
            CreateStackResponse stackResponse = cfClient.CreateStack(stackRequest);
        }
Example #3
0
        static void TestCfStack(EncryptionMaterials encryptionMaterials)
        {
            string bucket_Name  = QSS3BucketName;
            string templateName = QSS3KeyPrefix + TdwUtils.cfClassPathBastion.Replace("tdw_cf_template\\", "");
            string stack_name   = templateName.Replace("-", "");

            stack_name = stack_name.Replace(".template", "");
            //AmazonS3EncryptionClient s3Client = new AmazonS3EncryptionClient(encryptionMaterials);
            AmazonS3Client s3Client = new AmazonS3Client();

            GetObjectRequest getObjectRequest = new GetObjectRequest
            {
                BucketName = bucket_Name,
                Key        = templateName,
            };

            string data = null;

            using (GetObjectResponse getObjectResponse = s3Client.GetObject(getObjectRequest))
            {
                using (var stream = getObjectResponse.ResponseStream)
                    using (var reader = new StreamReader(stream))
                    {
                        data = reader.ReadToEnd();
                    }
            }

            Amazon.CloudFormation.AmazonCloudFormationClient cfClient = new AmazonCloudFormationClient();
            ValidateTemplateResponse templateResponse = cfClient.ValidateTemplate(new ValidateTemplateRequest()
            {
                TemplateBody = data
            });

            List <string>            capabilities       = templateResponse.Capabilities;
            string                   capabilitiesReason = templateResponse.CapabilitiesReason;
            string                   description        = templateResponse.Description;
            List <TemplateParameter> parameters         = templateResponse.Parameters;

            if (parameters.Any())
            {
                Console.WriteLine("  Parameters:");

                foreach (var p in parameters)
                {
                    Console.WriteLine("    {0} = {1}",
                                      p.ParameterKey, p.Description);
                }
            }

            //try
            //{
            //    DeleteStackRequest deleteRequest = new DeleteStackRequest() { StackName = stack_name };
            //    cfClient.DeleteStack(deleteRequest);
            //}
            //catch (Exception ex)
            //{
            //    ex = null;
            //}

            DescribeStacksResponse testForStackDescResp = new DescribeStacksResponse();

            try
            {
                testForStackDescResp = cfClient.DescribeStacks(new DescribeStacksRequest()
                {
                    StackName = stack_name
                });
            }
            catch (Exception ex)
            {
                testForStackDescResp = null;
            }

            if (testForStackDescResp == null)
            {
                List <string> CfCapabilities = new List <string>();
                CfCapabilities.Add("CAPABILITY_IAM");
                CreateStackRequest stackRequest = new CreateStackRequest()
                {
                    StackName = stack_name, TemplateBody = data, Capabilities = CfCapabilities
                };

                //stackRequest.Parameters.Add(new Parameter() { ParameterKey = "pDBPassword", ParameterValue = "LiverpoolFC" } );
                //stackRequest.Parameters.Add(new Parameter() { ParameterKey = "pNotifyEmail", ParameterValue = "*****@*****.**" });
                //stackRequest.Parameters.Add(new Parameter() { ParameterKey = "pEC2KeyPairBastion", ParameterValue = "BastionSshKvp" });
                //stackRequest.Parameters.Add(new Parameter() { ParameterKey = "pEC2KeyPair", ParameterValue = "Ec2SshKvp" });
                //stackRequest.Parameters.Add(new Parameter() { ParameterKey = "pSupportsConfig", ParameterValue = "Yes" });
                //stackRequest.Parameters.Add(new Parameter() { ParameterKey = "pAvailabilityZoneA", ParameterValue = "eu-west-1a" });
                //stackRequest.Parameters.Add(new Parameter() { ParameterKey = "pAvailabilityZoneB", ParameterValue = "eu-west-1b" });
                //stackRequest.Parameters.Add(new Parameter() { ParameterKey = "pVPCTenancy", ParameterValue = "default" });
                //stackRequest.Parameters.Add(new Parameter() { ParameterKey = "QSS3BucketName", ParameterValue = QSS3BucketName });
                //stackRequest.Parameters.Add(new Parameter() { ParameterKey = "QSS3KeyPrefix", ParameterValue = QSS3KeyPrefix });

                templateResponse = cfClient.ValidateTemplate(new ValidateTemplateRequest()
                {
                    TemplateBody = data
                });
                CreateStackResponse stackResponse = cfClient.CreateStack(stackRequest);
            }

            testForStackDescResp = cfClient.DescribeStacks(new DescribeStacksRequest());

            foreach (var stack in testForStackDescResp.Stacks)
            {
                Console.WriteLine("stack: {0}", stack.StackName);
                Console.WriteLine("  status: {0}", stack.StackStatus);
                Console.WriteLine("  created: {0}", stack.CreationTime);

                var ps = stack.Parameters;

                if (ps.Any())
                {
                    Console.WriteLine("  parameters:");

                    foreach (var p in ps)
                    {
                        Console.WriteLine("    {0} = {1}",
                                          p.ParameterKey, p.ParameterValue);
                    }
                }
            }
        }