Implementation for accessing AutoScaling Auto Scaling

Auto Scaling is designed to automatically launch or terminate EC2 instances based on user-defined policies, schedules, and health checks. Use this service in conjunction with the Amazon CloudWatch and Elastic Load Balancing services.

Inheritance: AmazonServiceClient, IAmazonAutoScaling
		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			
			var client = new AmazonAutoScalingClient (new BasicAWSCredentials (ACCESS_KEY, SECRET_KEY), Amazon.RegionEndpoint.USEast1);

			// Attach specified instance.
			var request = new AttachInstancesRequest ();
			request.AutoScalingGroupName = "TestGroup";
			request.InstanceIds = new System.Collections.Generic.List<string> { INSTANCE_ID };
			client.AttachInstances (request);
		}
示例#2
0
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			// Get our button from the layout resource,
			// and attach an event to it
			Button button = FindViewById<Button> (Resource.Id.myButton);
			
			button.Click += delegate
			{
				button.Text = string.Format ("{0} clicks!", count++);
			};
				
			var client = new AmazonAutoScalingClient (new BasicAWSCredentials (ACCESS_KEY, SECRET_KEY), Amazon.RegionEndpoint.USEast1);

			// Attach specified instance.
			var request = new AttachInstancesRequest ();
			request.AutoScalingGroupName = "TestGroup";
			request.InstanceIds = new System.Collections.Generic.List<string> { INSTANCE_ID };
			client.AttachInstances (request);
		}
示例#3
0
        public Deployer(AwsConfiguration awsConfiguration)
        {
            _awsEndpoint = awsConfiguration.AwsEndpoint;
            _bucket = awsConfiguration.Bucket;
            _assumeRoleTrustDocument = awsConfiguration.AssumeRoleTrustDocument;
            _iamRolePolicyDocument = awsConfiguration.IamRolePolicyDocument;

            AWSCredentials credentials;

            if (isArn(awsConfiguration.RoleName))
            {
                var securityTokenServiceClient = new AmazonSecurityTokenServiceClient(awsConfiguration.AwsEndpoint);

                var assumeRoleResult = securityTokenServiceClient.AssumeRole(new AssumeRoleRequest
                {
                    RoleArn = awsConfiguration.RoleName,
                    DurationSeconds = 3600,
                    RoleSessionName = "Net2User",
                    ExternalId = Guid.NewGuid().ToString()
                });

                Credentials stsCredentials = assumeRoleResult.Credentials;

                SessionAWSCredentials sessionCredentials =
                          new SessionAWSCredentials(stsCredentials.AccessKeyId,
                                                    stsCredentials.SecretAccessKey,
                                                    stsCredentials.SessionToken);

                credentials = sessionCredentials;

                _role = new AssumedRole(assumeRoleResult.AssumedRoleUser);
            }
            else {
                credentials = awsConfiguration.Credentials ?? new EnvironmentAWSCredentials();
            }

            _codeDeployClient = new AmazonCodeDeployClient(
                credentials,
                new AmazonCodeDeployConfig {
                    RegionEndpoint = awsConfiguration.AwsEndpoint,
                    ProxyHost = awsConfiguration.ProxyHost,
                    ProxyPort = awsConfiguration.ProxyPort
                });

            _cloudFormationClient = new AmazonCloudFormationClient(
                credentials,
                new AmazonCloudFormationConfig {
                    RegionEndpoint = awsConfiguration.AwsEndpoint,
                    ProxyHost = awsConfiguration.ProxyHost,
                    ProxyPort = awsConfiguration.ProxyPort
                });

            _s3Client = new AmazonS3Client(
                credentials,
                new AmazonS3Config {
                    RegionEndpoint = awsConfiguration.AwsEndpoint,
                    ProxyHost = awsConfiguration.ProxyHost,
                    ProxyPort = awsConfiguration.ProxyPort
                });

            _iamClient = new AmazonIdentityManagementServiceClient(
                credentials,
                new AmazonIdentityManagementServiceConfig  {
                    RegionEndpoint = awsConfiguration.AwsEndpoint,
                    ProxyHost = awsConfiguration.ProxyHost,
                    ProxyPort = awsConfiguration.ProxyPort
                });

            _autoScalingClient = new AmazonAutoScalingClient(
                credentials,
                new AmazonAutoScalingConfig {
                    RegionEndpoint = awsConfiguration.AwsEndpoint,
                    ProxyHost = awsConfiguration.ProxyHost,
                    ProxyPort = awsConfiguration.ProxyPort
                });
        }
 /// <summary>
 /// Load launch configurations to view model with AWS data based on region selected and EC2 classic/vpc
 /// </summary>
 private void LoadLaunchConfigurations(AmazonAutoScalingClient asClient)
 {
     try
     {
         DescribeLaunchConfigurationsRequest lcreq = new DescribeLaunchConfigurationsRequest();
         DescribeLaunchConfigurationsResponse lcresp = asClient.DescribeLaunchConfigurations(lcreq);
         Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
         {
             vm.LaunchConfigurations.Clear();
         }));
         foreach (LaunchConfiguration lcg in lcresp.DescribeLaunchConfigurationsResult.LaunchConfigurations)
         {
             if (lcg.SecurityGroups.Count() == 0)
             {
                 Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                 {
                     vm.LaunchConfigurations.Add(new Models.ConsoleLC() { LaunchConfiguration = lcg });
                 }));
             }
             else
             {
                 foreach (Models.ConsoleSG lcsg in vm.SecurityGroups)
                 {
                     if (lcg.SecurityGroups.Contains(lcsg.SecurityGroup.GroupId))
                     {
                         Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                         {
                             vm.LaunchConfigurations.Add(new Models.ConsoleLC() { LaunchConfiguration = lcg });
                         }));
                         break;
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         LogManager.LogEntry(ex.Message);
         LogManager.LogEntry(ex.StackTrace);
         throw new DataLoadingException("Error occurred loading launch configurations for region and environment type");
     }
 }
        /// <summary>
        /// Load auto scaling groups to view model with AWS data based on region selected and EC2 classic/vpc
        /// </summary>
        private void LoadAutoScalingGroups(AmazonAutoScalingClient asClient)
        {
            try
            {
                DescribeAutoScalingGroupsRequest asreq = new DescribeAutoScalingGroupsRequest();
                DescribeAutoScalingGroupsResponse asresp = asClient.DescribeAutoScalingGroups(asreq);
                Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                {
                    vm.AutoScalingGroups.Clear();
                }));
                foreach (AutoScalingGroup asg in asresp.DescribeAutoScalingGroupsResult.AutoScalingGroups)
                {
                    if (vm.IsVpc)
                    {
                        if (!string.IsNullOrEmpty(asg.VPCZoneIdentifier) && vm.SelectedVpc != null)
                        {
                            foreach (Models.ConsoleSubnet subnet in vm.SelectedVpc.Subnets)
                            {
                                if (asg.VPCZoneIdentifier.Contains(subnet.Subnet.SubnetId))
                                {
                                    //vm.AutoScalingGroups.Add(asg);
                                    Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                                    {
                                        vm.AutoScalingGroups.Add(new Models.ConsoleASG() { AutoScalingGroup = asg });
                                    }));
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(asg.VPCZoneIdentifier))
                        {
                            Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                            {
                                vm.AutoScalingGroups.Add(new Models.ConsoleASG() { AutoScalingGroup = asg });
                            }));
                        }
                    }
                }

                Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                {
                    for (int i = 0; i < vm.AutoScalingGroups.Count(); i++)
                    {
                        DescribeNotificationConfigurationsRequest nreq = new DescribeNotificationConfigurationsRequest();
                        nreq.WithAutoScalingGroupNames(vm.AutoScalingGroups[i].AutoScalingGroup.AutoScalingGroupName);
                        DescribeNotificationConfigurationsResponse nresp = asClient.DescribeNotificationConfigurations(nreq);
                        vm.AutoScalingGroups[i].NotificationConfigurations = new List<NotificationConfiguration>();
                        foreach (NotificationConfiguration nc in nresp.DescribeNotificationConfigurationsResult.NotificationConfigurations)
                        {
                            vm.AutoScalingGroups[i].NotificationConfigurations.Add(nc);
                        }
                    }
                }));
            }
            catch (Exception ex)
            {
                LogManager.LogEntry(ex.Message);
                LogManager.LogEntry(ex.StackTrace);
                throw new DataLoadingException("Error occurred loading auto scaling groups for region and environment type");
            }
        }
        /// <summary>
        /// Creates AWS auto scaling group client
        /// </summary>
        /// <returns>AmazonAutoScalingClient</returns>
        private AmazonAutoScalingClient GetAutoScaleClient()
        {
            if (vm.Region == null)
            {
                throw new InvalidRegionException("No region defined when creating auto scaling client");
            }

            AmazonAutoScalingConfig config = new AmazonAutoScalingConfig();
            config.ServiceURL = vm.Region.Url;

            AmazonAutoScalingClient client = new AmazonAutoScalingClient(config);

            return client;
        }
示例#7
0
        void EnsureDeploymentGroupExistsForBundle(AmazonCodeDeployClient codeDeployClient, AmazonIdentityManagementServiceClient iamClient, AmazonAutoScalingClient autoScalingClient, Role role, string deploymentGroupName)
        {
            var serviceRoleArn = role.Arn;

            if (TargetsAutoScalingDeploymentGroup)
            {
                var group =
                    autoScalingClient.DescribeAutoScalingGroups()
                        .AutoScalingGroups.FirstOrDefault(
                            asg => asg.Tags.Any(t => t.Key == "DeploymentRole" && t.Value == deploymentGroupName));

                if (group == null)
                    throw new ApplicationException(
                        string.Format("Auto scaling group with DeploymentRole {0} does not exist.", deploymentGroupName));

                try
                {
                    codeDeployClient.CreateDeploymentGroup(new CreateDeploymentGroupRequest
                    {
                        ApplicationName = CodeDeployApplicationName,
                        DeploymentGroupName = deploymentGroupName,
                        ServiceRoleArn = serviceRoleArn,
                        AutoScalingGroups = new List<string> {group.AutoScalingGroupName}
                    });
                }
                catch (DeploymentGroupAlreadyExistsException)
                {
                    // reuse a previously created deployment group with the same name
                }
            }
            else
            {
                try
                {
                    Console.WriteLine("Will assume role {0} for deployment", serviceRoleArn);
                    codeDeployClient.CreateDeploymentGroup(new CreateDeploymentGroupRequest
                    {
                        ApplicationName = CodeDeployApplicationName,
                        DeploymentGroupName = deploymentGroupName,
                        ServiceRoleArn = serviceRoleArn,
                        Ec2TagFilters = new List<EC2TagFilter>
                    {
                        new EC2TagFilter
                        {
                            Type = EC2TagFilterType.KEY_AND_VALUE,
                            Key = "DeploymentRole",
                            Value = deploymentGroupName
                        }
                    }
                    });
                }
                catch (DeploymentGroupAlreadyExistsException)
                {
                    // since this is EC2, we can reuse a previously created deployment group with the same name
                }
            }
        }
示例#8
0
        public CreateDeploymentResponse DeployToStack(
            AmazonCodeDeployClient codeDeployClient,
            AmazonIdentityManagementServiceClient iamClient,
            AmazonAutoScalingClient autoScalingClient,
            Role role)
        {
            var deploymentGroupName = _stackName + "_" + BundleName;

            EnsureDeploymentGroupExistsForBundle(codeDeployClient, iamClient, autoScalingClient, role, deploymentGroupName);

            var deploymentResponse = codeDeployClient.CreateDeployment(new CreateDeploymentRequest
            {
                ApplicationName = CodeDeployApplicationName,
                DeploymentGroupName = deploymentGroupName,
                Revision = new RevisionLocation
                {
                    RevisionType = RevisionLocationType.S3,
                    S3Location = new S3Location
                    {
                        Bucket = Bucket,
                        Key = FileName,
                        BundleType = BundleType.Zip,
                        ETag = ETag
                    }
                }
            });

            return deploymentResponse;
        }
        /// <summary>
        /// Notification configuration window load event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NcWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                AmazonAutoScalingConfig config = new AmazonAutoScalingConfig();
                config.ServiceURL = ((ViewModel)this.DataContext).Region.Url;
                AmazonAutoScalingClient client = new AmazonAutoScalingClient(config);
                DescribeAutoScalingNotificationTypesRequest dasntreq = new DescribeAutoScalingNotificationTypesRequest();
                DescribeAutoScalingNotificationTypesResponse dasntresp = client.DescribeAutoScalingNotificationTypes(dasntreq);
                foreach (string asnt in dasntresp.DescribeAutoScalingNotificationTypesResult.AutoScalingNotificationTypes)
                {
                    this.notificationTypes.Add(asnt);
                }

                AmazonSimpleNotificationServiceConfig snsconfig = new AmazonSimpleNotificationServiceConfig();
                config.ServiceURL = ((ViewModel)this.DataContext).Region.Url;
                AmazonSimpleNotificationServiceClient snsclient = new AmazonSimpleNotificationServiceClient(snsconfig);
                ListTopicsRequest ltrequest = new ListTopicsRequest();
                ListTopicsResponse ltresp = snsclient.ListTopics(ltrequest);
                foreach (Topic topic in ltresp.ListTopicsResult.Topics)
                {
                    this.snstopics.Add(topic.TopicArn);
                }

                rlbNcTypes.ItemsSource = this.notificationTypes;
                cboTopics.ItemsSource = this.snstopics;
            }
            catch
            {
                MessageBox.Show(Window.GetWindow(this), "Error occured while loading the notification configuration options.", "Error", MessageBoxButton.OK);
                this.Close();
            }
        }