コード例 #1
0
        /// <summary>
        /// <para> Creates a new Auto Scaling group with the specified name. Once the creation request is completed, the AutoScalingGroup is ready to be
        /// used in other calls. </para> <para><b>NOTE:</b> The Auto Scaling group name must be unique within the scope of your AWS account, and under
        /// the quota of Auto Scaling groups allowed for your account. </para>
        /// </summary>
        ///
        /// <param name="createAutoScalingGroupRequest">Container for the necessary parameters to execute the CreateAutoScalingGroup service method on
        ///           AmazonAutoScaling.</param>
        ///
        /// <exception cref="LimitExceededException"/>
        /// <exception cref="AlreadyExistsException"/>
        public CreateAutoScalingGroupResponse CreateAutoScalingGroup(CreateAutoScalingGroupRequest createAutoScalingGroupRequest)
        {
            IRequest <CreateAutoScalingGroupRequest> request  = new CreateAutoScalingGroupRequestMarshaller().Marshall(createAutoScalingGroupRequest);
            CreateAutoScalingGroupResponse           response = Invoke <CreateAutoScalingGroupRequest, CreateAutoScalingGroupResponse> (request, this.signer, CreateAutoScalingGroupResponseUnmarshaller.GetInstance());

            return(response);
        }
コード例 #2
0
        /// <summary>
        /// 本接口(CreateAutoScalingGroup)用于创建伸缩组
        /// </summary>
        /// <param name="req">参考<see cref="CreateAutoScalingGroupRequest"/></param>
        /// <returns>参考<see cref="CreateAutoScalingGroupResponse"/>实例</returns>
        public async Task <CreateAutoScalingGroupResponse> CreateAutoScalingGroup(CreateAutoScalingGroupRequest req)
        {
            JsonResponseModel <CreateAutoScalingGroupResponse> rsp = null;

            try
            {
                var strResp = await this.InternalRequest(req, "CreateAutoScalingGroup");

                rsp = JsonConvert.DeserializeObject <JsonResponseModel <CreateAutoScalingGroupResponse> >(strResp);
            }
            catch (JsonSerializationException e)
            {
                throw new TencentCloudSDKException(e.Message);
            }
            return(rsp.Response);
        }
コード例 #3
0
        /**
         * Convert CreateAutoScalingGroupRequest to name value pairs
         */
        private static IDictionary <string, string> ConvertCreateAutoScalingGroup(CreateAutoScalingGroupRequest request)
        {
            IDictionary <string, string> parameters = new Dictionary <string, string>();

            parameters["Action"] = "CreateAutoScalingGroup";
            if (request.IsSetAutoScalingGroupName())
            {
                parameters["AutoScalingGroupName"] = request.AutoScalingGroupName;
            }
            if (request.IsSetLaunchConfigurationName())
            {
                parameters["LaunchConfigurationName"] = request.LaunchConfigurationName;
            }
            if (request.IsSetMinSize())
            {
                parameters["MinSize"] = request.MinSize.ToString();
            }
            if (request.IsSetMaxSize())
            {
                parameters["MaxSize"] = request.MaxSize.ToString();
            }
            if (request.IsSetCooldown())
            {
                parameters["Cooldown"] = request.Cooldown.ToString();
            }
            List <string> createAutoScalingGroupRequestAvailabilityZonesList      = request.AvailabilityZones;
            int           createAutoScalingGroupRequestAvailabilityZonesListIndex = 1;

            foreach (string createAutoScalingGroupRequestAvailabilityZones in createAutoScalingGroupRequestAvailabilityZonesList)
            {
                parameters[String.Concat("AvailabilityZones", ".member.", createAutoScalingGroupRequestAvailabilityZonesListIndex)] = createAutoScalingGroupRequestAvailabilityZones;
                createAutoScalingGroupRequestAvailabilityZonesListIndex++;
            }
            List <string> createAutoScalingGroupRequestLoadBalancerNamesList      = request.LoadBalancerNames;
            int           createAutoScalingGroupRequestLoadBalancerNamesListIndex = 1;

            foreach (string createAutoScalingGroupRequestLoadBalancerNames in createAutoScalingGroupRequestLoadBalancerNamesList)
            {
                parameters[String.Concat("LoadBalancerNames", ".member.", createAutoScalingGroupRequestLoadBalancerNamesListIndex)] = createAutoScalingGroupRequestLoadBalancerNames;
                createAutoScalingGroupRequestLoadBalancerNamesListIndex++;
            }

            return(parameters);
        }
コード例 #4
0
        private static IDictionary <string, string> ConvertCreateAutoScalingGroup(CreateAutoScalingGroupRequest request)
        {
            IDictionary <string, string> dictionary = new Dictionary <string, string>();

            dictionary["Action"] = "CreateAutoScalingGroup";
            if (request.IsSetAutoScalingGroupName())
            {
                dictionary["AutoScalingGroupName"] = request.AutoScalingGroupName;
            }
            if (request.IsSetLaunchConfigurationName())
            {
                dictionary["LaunchConfigurationName"] = request.LaunchConfigurationName;
            }
            if (request.IsSetMinSize())
            {
                dictionary["MinSize"] = request.MinSize.ToString();
            }
            if (request.IsSetMaxSize())
            {
                dictionary["MaxSize"] = request.MaxSize.ToString();
            }
            if (request.IsSetCooldown())
            {
                dictionary["Cooldown"] = request.Cooldown.ToString();
            }
            List <string> availabilityZones = request.AvailabilityZones;
            int           num = 1;

            foreach (string str in availabilityZones)
            {
                dictionary["AvailabilityZones" + ".member." + num] = str;
                num++;
            }
            List <string> loadBalancerNames = request.LoadBalancerNames;
            int           num2 = 1;

            foreach (string str2 in loadBalancerNames)
            {
                dictionary["LoadBalancerNames" + ".member." + num2] = str2;
                num2++;
            }
            return(dictionary);
        }
コード例 #5
0
 /// <summary>
 /// Create Auto Scaling Group
 /// </summary>
 /// <param name="request">Create Auto Scaling Group  request</param>
 /// <returns>Create Auto Scaling Group  Response from the service</returns>
 public CreateAutoScalingGroupResponse CreateAutoScalingGroup(CreateAutoScalingGroupRequest request)
 {
     return(Invoke <CreateAutoScalingGroupResponse>(ConvertCreateAutoScalingGroup(request)));
 }
コード例 #6
0
ファイル: AutoScaling.cs プロジェクト: wjpeters/aws-sdk-net
        // Test disabled, $ and time constraints
        //[TestMethod]
        //[TestCategory("AutoScaling")]
        public void AutoScalingGroupCRUDTest()
        {
            var groupName        = string.Concat(UtilityMethods.SDK_TEST_PREFIX, "TestAutoScalingGroup", DateTime.UtcNow.Ticks);
            var launchConfigName = string.Concat(UtilityMethods.SDK_TEST_PREFIX, "TestLaunchConfiguration", DateTime.UtcNow.Ticks);

            try
            {
                string imageId;
                using (var ec2Client = new AmazonEC2Client())
                {
                    imageId = Amazon.EC2.Util.ImageUtilities.FindImage(ec2Client, Amazon.EC2.Util.ImageUtilities.WINDOWS_2012_BASE).ImageId;
                }
                // create the initial launch configuration and group
                var createLaunchConfigRequest = new CreateLaunchConfigurationRequest
                {
                    ImageId                 = imageId,
                    InstanceType            = "m1.small",
                    LaunchConfigurationName = launchConfigName
                };
                var createLaunchConfigResponse = Client.CreateLaunchConfiguration(createLaunchConfigRequest);
                Assert.IsNotNull(createLaunchConfigResponse);

                var createGroupRequest = new CreateAutoScalingGroupRequest
                {
                    AutoScalingGroupName = groupName,
                    MinSize           = 1,
                    MaxSize           = 5,
                    AvailabilityZones = new List <string> {
                        "us-east-1b"
                    },
                    LaunchConfigurationName = launchConfigName
                };
                var createGroupresponse = Client.CreateAutoScalingGroup(createGroupRequest);
                Assert.IsNotNull(createGroupresponse);

                // make a simple update to the group
                var updateGroupRequest = new UpdateAutoScalingGroupRequest
                {
                    AutoScalingGroupName = groupName,
                    MinSize = 1,
                    MaxSize = 2,
                    LaunchConfigurationName = launchConfigName
                };
                Client.UpdateAutoScalingGroup(updateGroupRequest);

                // poll the group to check activity
                var describeGroupsRequest = new DescribeAutoScalingGroupsRequest
                {
                    AutoScalingGroupNames = new List <string> {
                        groupName
                    }
                };
                var describeGroupsResponse = Client.DescribeAutoScalingGroups(describeGroupsRequest);
                while (describeGroupsResponse.AutoScalingGroups[0].Instances.Count == 0)
                {
                    Console.WriteLine("Sleeping 30s while some instance starts");
                    Thread.Sleep(TimeSpan.FromSeconds(30));
                    describeGroupsResponse = Client.DescribeAutoScalingGroups(describeGroupsRequest);
                }
                var instances = describeGroupsResponse.AutoScalingGroups[0].Instances;
                Assert.IsNotNull(instances);
                Assert.IsTrue(instances.Count > 0);

                var describeActivitiesRequest = new DescribeScalingActivitiesRequest
                {
                    AutoScalingGroupName = groupName
                };
                var describeActivitiesResponse = Client.DescribeScalingActivities(describeActivitiesRequest);
                while (ActivitiesPending(describeActivitiesResponse.Activities))
                {
                    Console.WriteLine("Sleeping 15s waiting for no activities to be pending/in-progres");
                    Thread.Sleep(TimeSpan.FromSeconds(15));
                    describeActivitiesResponse = Client.DescribeScalingActivities(describeActivitiesRequest);
                }

                // make another configuration change to the group, then manually terminate
                // any remaining instances it contains
                updateGroupRequest = new UpdateAutoScalingGroupRequest
                {
                    AutoScalingGroupName = groupName,
                    MinSize = 0,
                    MaxSize = 2,
                    LaunchConfigurationName = launchConfigName
                };

                Client.UpdateAutoScalingGroup(updateGroupRequest);

                foreach (var instance in instances)
                {
                    var terminateRequest = new TerminateInstanceInAutoScalingGroupRequest
                    {
                        InstanceId = instance.InstanceId,
                        ShouldDecrementDesiredCapacity = true
                    };

                    var terminateResult = Client.TerminateInstanceInAutoScalingGroup(terminateRequest);
                    var activity        = terminateResult.Activity;
                    Assert.IsNotNull(activity);
                    Assert.IsNotNull(activity.ActivityId);
                    Assert.IsNotNull(activity.Description);
                    Assert.IsNotNull(activity.Progress);
                }

                // wait for all instances to terminate, then cleanup
                describeGroupsResponse = Client.DescribeAutoScalingGroups(describeGroupsRequest);
                while (describeGroupsResponse.AutoScalingGroups[0].Instances.Count != 0)
                {
                    Console.WriteLine("Waiting 30s for instance(s) to terminate.");
                    Thread.Sleep(TimeSpan.FromSeconds(30));
                    describeGroupsResponse = Client.DescribeAutoScalingGroups(describeGroupsRequest);
                }

                Assert.IsTrue(describeGroupsResponse.AutoScalingGroups[0].Instances.Count == 0);
            }
            finally
            {
                Client.DeleteAutoScalingGroup(new DeleteAutoScalingGroupRequest
                {
                    AutoScalingGroupName = groupName,
                    ForceDelete          = true
                });
                Client.DeleteLaunchConfiguration(new DeleteLaunchConfigurationRequest
                {
                    LaunchConfigurationName = launchConfigName
                });
            }
        }
コード例 #7
0
        public static void Configure()
        {
            //Amazon Credentials
            string accessKey = ConfigurationManager.AppSettings["AWSAccessKeyId"];
            string secretKey = ConfigurationManager.AppSettings["AWSSecretKey"];

            string launchConfigurationName = "ScheduledServicesLaunchConfiguration";

            string autoScalingGroupName = "ScheduledServiceASG";
            string scaleOutPolicyName   = "ScheduledServicesScaleOutSQSPolicy";
            string scaleInPolicyName    = "ScheduledServicesScaleInSQSPolicy";

            string scaleOutARN = "";
            string scaleInARN  = "";

            string queueName = "ScheduledServicesSQS";
            string queueURL  = "";

            string amiID        = "AMI_ID";
            string instanceType = "INSTANCE_TYPE";

            AmazonAutoScalingClient autoScaleClient = new AmazonAutoScalingClient(accessKey, secretKey, RegionEndpoint.USWest2);

            AmazonSQSClient sqsClient = new AmazonSQSClient(accessKey, secretKey, RegionEndpoint.USWest2);

            Console.WriteLine("¡¡¡CONFIGURATION INITIALIZED!!!");
            Console.WriteLine("");

            Console.WriteLine("--------- SQS ---------");
            Console.WriteLine("");

            Console.WriteLine("Creating the simple queue service item");
            Console.WriteLine("");

            //Get or create the sqs instance
            CreateQueueRequest  createQueueRequest  = new CreateQueueRequest(queueName);
            CreateQueueResponse createQueueResponse = sqsClient.CreateQueue(createQueueRequest);

            queueURL = createQueueResponse.QueueUrl;

            Console.WriteLine("Created the simple queue service item with name: " + queueName + " and  url: " + queueURL);
            Console.WriteLine("");

            Console.WriteLine("--------- EC2 ---------");
            Console.WriteLine("");

            //If not exists any launch configuration with this name, creates
            DescribeLaunchConfigurationsRequest describeLaunchConfigurationsRequest = new DescribeLaunchConfigurationsRequest();

            describeLaunchConfigurationsRequest.LaunchConfigurationNames = new List <string>()
            {
                launchConfigurationName
            };
            DescribeLaunchConfigurationsResponse describeLaunchConfigurationsResponse = autoScaleClient.DescribeLaunchConfigurations(describeLaunchConfigurationsRequest);

            Console.WriteLine("Creating the launch configuration");
            Console.WriteLine("");
            if (describeLaunchConfigurationsResponse.LaunchConfigurations.Count == 0)
            {
                //Create Launch Configuration Request
                CreateLaunchConfigurationRequest launchConfigurationRequest = new CreateLaunchConfigurationRequest();
                launchConfigurationRequest.LaunchConfigurationName = launchConfigurationName;
                launchConfigurationRequest.ImageId      = amiID;
                launchConfigurationRequest.InstanceType = instanceType;

                //Create Launch Configuration Response
                CreateLaunchConfigurationResponse launchConfigurationResponse = autoScaleClient.CreateLaunchConfiguration(launchConfigurationRequest);

                Console.WriteLine("Created the launch configuration with name: " + launchConfigurationName);
                Console.WriteLine("");
            }
            else
            {
                Console.WriteLine("Founded the launch configuration with name: " + launchConfigurationName);
                Console.WriteLine("");
            }

            Console.WriteLine("Creating the autoscaling group");
            Console.WriteLine("");


            DescribeAutoScalingGroupsRequest describeAutoScalingGroupsRequest = new DescribeAutoScalingGroupsRequest();

            describeAutoScalingGroupsRequest.AutoScalingGroupNames = new List <string>()
            {
                autoScalingGroupName
            };
            DescribeAutoScalingGroupsResponse describeAutoScalingGroupsResponse = autoScaleClient.DescribeAutoScalingGroups(describeAutoScalingGroupsRequest);

            if (describeAutoScalingGroupsResponse.AutoScalingGroups.Count == 0)
            {
                //Create Auto Scaling Group Request
                CreateAutoScalingGroupRequest autoScalingGroupRequest = new CreateAutoScalingGroupRequest();
                autoScalingGroupRequest.AutoScalingGroupName = autoScalingGroupName;
                autoScalingGroupRequest.MinSize           = 1;
                autoScalingGroupRequest.MaxSize           = 3;
                autoScalingGroupRequest.DesiredCapacity   = 1;
                autoScalingGroupRequest.AvailabilityZones = new List <string>()
                {
                    "us-west-2a", "us-west-2b", "us-west-2c"
                };
                autoScalingGroupRequest.LaunchConfigurationName = launchConfigurationName;

                //Create Auto Scaling Group Response
                autoScaleClient.CreateAutoScalingGroup(autoScalingGroupRequest);

                Console.WriteLine("Created the autoscaling group with name: " + autoScalingGroupName);
                Console.WriteLine("");
            }
            else
            {
                Console.WriteLine("Founded the autoscaling group with name: " + autoScalingGroupName);
                Console.WriteLine("");
            }

            Console.WriteLine("Creating the scale out policy");
            Console.WriteLine("");

            //Policies
            //Creating scaling out policy for the SQS
            PutScalingPolicyRequest scalingOutPolicyRequest = new PutScalingPolicyRequest();

            scalingOutPolicyRequest.PolicyName           = scaleOutPolicyName;
            scalingOutPolicyRequest.AutoScalingGroupName = autoScalingGroupName;
            scalingOutPolicyRequest.ScalingAdjustment    = -1;
            scalingOutPolicyRequest.AdjustmentType       = "ChangeInCapacity";

            PutScalingPolicyResponse scalingOutPolicyResponse = autoScaleClient.PutScalingPolicy(scalingOutPolicyRequest);

            scaleOutARN = scalingOutPolicyResponse.PolicyARN;

            Console.WriteLine("Created the scale out policy with arn: " + scaleOutARN);
            Console.WriteLine("");

            Console.WriteLine("Creating the scale in policy");
            Console.WriteLine("");

            //Creating scaling in policy for the SQS
            PutScalingPolicyRequest scalingInPolicyRequest = new PutScalingPolicyRequest();

            scalingInPolicyRequest.PolicyName           = scaleInPolicyName;
            scalingInPolicyRequest.AutoScalingGroupName = autoScalingGroupName;
            scalingInPolicyRequest.ScalingAdjustment    = 1;
            scalingInPolicyRequest.AdjustmentType       = "ChangeInCapacity";

            PutScalingPolicyResponse scalingInPolicyResponse = autoScaleClient.PutScalingPolicy(scalingInPolicyRequest);

            scaleInARN = scalingInPolicyResponse.PolicyARN;

            Console.WriteLine("Created the scale in policy with arn: " + scaleInARN);
            Console.WriteLine("");

            AmazonCloudWatchClient cloudWatchClient = new AmazonCloudWatchClient(accessKey, secretKey, RegionEndpoint.USWest2);

            Console.WriteLine("--------- CLOUD WATCH ---------");
            Console.WriteLine("");

            Console.WriteLine("Creating the scale in policy");
            Console.WriteLine("");

            //Scale In
            PutMetricAlarmRequest metricAlarmScaleInRequest = new PutMetricAlarmRequest();

            metricAlarmScaleInRequest.AlarmName          = "ScheduledServicesScaleInMetric";
            metricAlarmScaleInRequest.MetricName         = "ApproximateNumberOfMessagesVisible";
            metricAlarmScaleInRequest.Namespace          = "AWS/SQS";
            metricAlarmScaleInRequest.Period             = 300;
            metricAlarmScaleInRequest.Threshold          = 3;
            metricAlarmScaleInRequest.ComparisonOperator = ComparisonOperator.GreaterThanOrEqualToThreshold;
            metricAlarmScaleInRequest.Statistic          = new Statistic("Average");

            Dimension dimensionScaleIn = new Dimension();

            dimensionScaleIn.Name  = "QueueName";
            dimensionScaleIn.Value = queueName;

            metricAlarmScaleInRequest.Dimensions.Add(dimensionScaleIn);
            metricAlarmScaleInRequest.EvaluationPeriods = 2;
            metricAlarmScaleInRequest.AlarmActions.Add(scaleInARN);

            cloudWatchClient.PutMetricAlarm(metricAlarmScaleInRequest);

            Console.WriteLine("Created the scale in policy with name: ScheduledServicesScaleInMetric");
            Console.WriteLine("");

            Console.WriteLine("Creating the scale out policy");
            Console.WriteLine("");

            //Scale Out
            PutMetricAlarmRequest metricAlarmScaleOutRequest = new PutMetricAlarmRequest();

            metricAlarmScaleOutRequest.AlarmName          = "ScheduledServicesScaleOutMetric";
            metricAlarmScaleOutRequest.MetricName         = "ApproximateNumberOfMessagesVisible";
            metricAlarmScaleOutRequest.Namespace          = "AWS/SQS";
            metricAlarmScaleOutRequest.Period             = 300;
            metricAlarmScaleOutRequest.Threshold          = 3;
            metricAlarmScaleOutRequest.ComparisonOperator = ComparisonOperator.LessThanThreshold;
            metricAlarmScaleOutRequest.Statistic          = new Statistic("Average");

            Dimension dimensionScaleOut = new Dimension();

            dimensionScaleOut.Name  = "QueueName";
            dimensionScaleOut.Value = queueName;

            metricAlarmScaleOutRequest.Dimensions.Add(dimensionScaleOut);
            metricAlarmScaleOutRequest.EvaluationPeriods = 2;
            metricAlarmScaleOutRequest.AlarmActions.Add(scaleOutARN);

            cloudWatchClient.PutMetricAlarm(metricAlarmScaleOutRequest);

            Console.WriteLine("Created the scale out policy with name: ScheduledServicesScaleOutMetric");
            Console.WriteLine("");

            Console.WriteLine("¡¡¡CONFIGURATION FINISHED!!!");
        }