コード例 #1
0
 public void CreateCloudFormationOnAWS()
 {
     try
     {
         Log.Info(Program.LogPath, "Creating Cloud Information");
         var describeStacksRequest = new DescribeStacksRequest();
         var changeSetName         = "changeset" + Program.PostfixExpression;
         var changeSetType         = ChangeSetType.CREATE;
         if (CheckStackIsExist(CloudFormationClient, StackName))
         {
             changeSetType = ChangeSetType.UPDATE;
         }
         var createChangeSetRequest = new CreateChangeSetRequest
         {
             ChangeSetName = changeSetName,
             StackName     = StackName,
             //TemplateBody = ServerlessTemplateBody,
             TemplateURL   = TemplateUrl,
             ChangeSetType = changeSetType,
             Capabilities  = new List <string> {
                 "CAPABILITY_IAM"
             },
         };
         var createChangeSetResponse = CloudFormationClient.CreateChangeSet(createChangeSetRequest);
         WaitForChangeSet(CloudFormationClient, StackName, changeSetName);
         var executeChangeSetResponse = CloudFormationClient.ExecuteChangeSet(new ExecuteChangeSetRequest
         {
             ChangeSetName = changeSetName,
             StackName     = StackName,
         });
         WaitForStack(CloudFormationClient, StackName);
         var generatedStack = GetStack(CloudFormationClient, StackName);
         Log.Info(Program.LogPath, "Output URL is : " + generatedStack.Outputs.Find(x => x.OutputKey == "ApiURL").OutputValue);
         Log.Info(Program.LogPath, "Creating Cloud Information Finished");
     }
     catch (Exception ex)
     {
         Log.Error(Program.LogPath.FullName, "Creating Cloud Information  Error :   " + ex.Message);
     }
 }
コード例 #2
0
        static void ApplyCloudFormationChangeSetExample()
        {
            string bucket_Name  = QSS3BucketName;
            string templateName = QSS3KeyPrefix + TdwUtils.cfClassPathBastionChangeSet.Replace("tdw_cf_template\\", "");
            string stack_name   = QSS3KeyPrefix + TdwUtils.cfClassPathBastion.Replace("tdw_cf_template\\", "");

            stack_name = stack_name.Replace("-", "");
            stack_name = stack_name.Replace(".template", "");
            AmazonS3Client s3Client = new AmazonS3Client();

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

            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();
                    }
            }

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

            CfCapabilities.Add("CAPABILITY_IAM");

            List <Amazon.CloudFormation.Model.Parameter> parameters = new List <Amazon.CloudFormation.Model.Parameter>();

            parameters.Add(new Parameter
            {
                ParameterKey   = "pEnvTag",
                ParameterValue = "development"
            });

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

            notificationArns.Add("aws:sns:eu-west-1:009837347446:tdwcftdevmainbastion-LoggingTemplate-1E3KD8XDHOSTY-rSecurityAlarmTopic-1TNN0GI7819UM");

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

            resourceTypes.Add("AWS::*");

            List <Amazon.CloudFormation.Model.Tag> tagList = new List <Amazon.CloudFormation.Model.Tag>();

            tagList.Add(new Amazon.CloudFormation.Model.Tag()
            {
                Key = "environment", Value = "development"
            });

            CreateChangeSetRequest cfReq = new CreateChangeSetRequest()
            {
                Capabilities  = CfCapabilities,
                ChangeSetName = "tdwv010001",
                ChangeSetType = ChangeSetType.UPDATE,
                ClientToken   = "fromappsettingsv010001",
                Description   = "Adding kinesis template to tdw stack and parameterizing env parameter",
                //NotificationARNs = notificationArns,
                Parameters = parameters,
                //ResourceTypes = resourceTypes,
                //RoleARN
                StackName    = stack_name,
                Tags         = tagList,
                TemplateBody = data
                               //UsePreviousTemplate = true
            };

            CreateChangeSetResponse cfResp = cfClient.CreateChangeSet(cfReq);
        }