Beispiele für Auto Scaling mit SDK für .NET (v4) - AWS SDK-Codebeispiele

Weitere AWS SDK-Beispiele sind im Repo AWS Doc SDK Examples GitHub verfügbar.

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

Beispiele für Auto Scaling mit SDK für .NET (v4)

Die folgenden Codebeispiele zeigen Ihnen, wie Sie mithilfe von AWS SDK für .NET (v4) mit Auto Scaling Aktionen ausführen und allgemeine Szenarien implementieren.

Bei Grundlagen handelt es sich um Code-Beispiele, die Ihnen zeigen, wie Sie die wesentlichen Vorgänge innerhalb eines Services ausführen.

Aktionen sind Codeauszüge aus größeren Programmen und müssen im Kontext ausgeführt werden. Während Aktionen Ihnen zeigen, wie Sie einzelne Service-Funktionen aufrufen, können Sie Aktionen im Kontext der zugehörigen Szenarios anzeigen.

Jedes Beispiel enthält einen Link zum vollständigen Quellcode, in dem Sie Anweisungen zum Einrichten und Ausführen des Codes im Kontext finden.

Erste Schritte

Die folgenden Codebeispiele zeigen, wie Sie mit Auto Scaling beginnen können.

SDK für .NET (v4)
Anmerkung

Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel- einrichten und ausführen.

namespace AutoScalingActions; using Amazon.AutoScaling; public class HelloAutoScaling { /// <summary> /// Hello Amazon EC2 Auto Scaling. List EC2 Auto Scaling groups. /// </summary> /// <param name="args"></param> /// <returns>Async Task.</returns> static async Task Main(string[] args) { var client = new AmazonAutoScalingClient(); Console.WriteLine("Welcome to Amazon EC2 Auto Scaling."); Console.WriteLine("Let's get a description of your Auto Scaling groups."); var response = await client.DescribeAutoScalingGroupsAsync(); if (response.AutoScalingGroups == null || response.AutoScalingGroups.Count == 0) { Console.WriteLine("Sorry, you don't have any Amazon EC2 Auto Scaling groups."); return; } response.AutoScalingGroups.ForEach(autoScalingGroup => { Console.WriteLine($"{autoScalingGroup.AutoScalingGroupName}\t{autoScalingGroup.AvailabilityZones}"); }); } }

Grundlagen

Wie das aussehen kann, sehen Sie am nachfolgenden Beispielcode:

  • Erstellen Sie eine Amazon EC2 Auto Scaling Scaling-Gruppe mit einer Startvorlage und Availability Zones und erhalten Sie Informationen über laufende Instances.

  • Aktivieren Sie die Erfassung von CloudWatch Amazon-Metriken.

  • Aktualisieren Sie die gewünschte Kapazität der Gruppe und warten Sie, bis eine Instance gestartet wird.

  • Beenden Sie eine Instanz in der Gruppe.

  • Listet Skalierungsaktivitäten auf, die als Reaktion auf Benutzeranfragen und Kapazitätsänderungen erfolgen.

  • Holen Sie sich Statistiken für CloudWatch Metriken und bereinigen Sie dann Ressourcen.

SDK für .NET (v4)
Anmerkung

Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel- einrichten und ausführen.

global using Amazon.AutoScaling; global using Amazon.AutoScaling.Model; global using Amazon.CloudWatch; global using AutoScalingActions; global using Microsoft.Extensions.DependencyInjection; global using Microsoft.Extensions.Hosting; global using Microsoft.Extensions.Logging; global using Microsoft.Extensions.Logging.Console; global using Microsoft.Extensions.Logging.Debug; using Amazon.EC2; using Microsoft.Extensions.Configuration; using Host = Microsoft.Extensions.Hosting.Host; namespace AutoScalingBasics; public class AutoScalingBasics { static async Task Main(string[] args) { // Set up dependency injection for Amazon EC2 Auto Scaling, Amazon // CloudWatch, and Amazon EC2. using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .ConfigureServices((_, services) => services.AddAWSService<IAmazonAutoScaling>() .AddAWSService<IAmazonCloudWatch>() .AddAWSService<IAmazonEC2>() .AddTransient<AutoScalingWrapper>() .AddTransient<CloudWatchWrapper>() .AddTransient<EC2Wrapper>() .AddTransient<UIWrapper>() ) .Build(); var autoScalingWrapper = host.Services.GetRequiredService<AutoScalingWrapper>(); var cloudWatchWrapper = host.Services.GetRequiredService<CloudWatchWrapper>(); var ec2Wrapper = host.Services.GetRequiredService<EC2Wrapper>(); var uiWrapper = host.Services.GetRequiredService<UIWrapper>(); var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("settings.json") // Load test settings from .json file. .AddJsonFile("settings.local.json", true) // Optionally load local settings. .Build(); var imageId = configuration["ImageId"]; var instanceType = configuration["InstanceType"]; var launchTemplateName = configuration["LaunchTemplateName"]; launchTemplateName += Guid.NewGuid().ToString(); // The name of the Auto Scaling group. var groupName = configuration["GroupName"]; uiWrapper.DisplayTitle("Auto Scaling Basics"); uiWrapper.DisplayAutoScalingBasicsDescription(); // Create the launch template and save the template Id to use when deleting the // launch template at the end of the application. var launchTemplateId = await ec2Wrapper.CreateLaunchTemplateAsync(imageId!, instanceType!, launchTemplateName); // Confirm that the template was created by asking for a description of it. await ec2Wrapper.DescribeLaunchTemplateAsync(launchTemplateName); uiWrapper.PressEnter(); var availabilityZones = await ec2Wrapper.ListAvailabilityZonesAsync(); Console.WriteLine($"Creating an Auto Scaling group named {groupName}."); await autoScalingWrapper.CreateAutoScalingGroupAsync( groupName!, launchTemplateName, availabilityZones[0].ZoneName); // Keep checking the details of the new group until its lifecycle state // is "InService". Console.WriteLine($"Waiting for the Auto Scaling group to be active."); List<AutoScalingInstanceDetails> instanceDetails; do { instanceDetails = await autoScalingWrapper.DescribeAutoScalingInstancesAsync(groupName!); } while (instanceDetails.Count <= 0); Console.WriteLine($"Auto scaling group {groupName} successfully created."); Console.WriteLine($"{instanceDetails.Count} instances were created for the group."); // Display the details of the Auto Scaling group. instanceDetails.ForEach(detail => { Console.WriteLine($"Group name: {detail.AutoScalingGroupName}"); }); uiWrapper.PressEnter(); uiWrapper.DisplayTitle("Metrics collection"); Console.WriteLine($"Enable metrics collection for {groupName}"); await autoScalingWrapper.EnableMetricsCollectionAsync(groupName!); // Show the metrics that are collected for the group. // Update the maximum size of the group to three instances. Console.WriteLine("--- Update the Auto Scaling group to increase max size to 3 ---"); int maxSize = 3; await autoScalingWrapper.UpdateAutoScalingGroupAsync(groupName!, launchTemplateName, maxSize); Console.WriteLine("--- Describe all Auto Scaling groups to show the current state of the group ---"); var groups = await autoScalingWrapper.DescribeAutoScalingGroupsAsync(groupName!); uiWrapper.DisplayGroupDetails(groups!); uiWrapper.PressEnter(); uiWrapper.DisplayTitle("Describe account limits"); await autoScalingWrapper.DescribeAccountLimitsAsync(); uiWrapper.WaitABit(60, "Waiting for the resources to be ready."); uiWrapper.DisplayTitle("Set desired capacity"); int desiredCapacity = 2; await autoScalingWrapper.SetDesiredCapacityAsync(groupName!, desiredCapacity); Console.WriteLine("Get the two instance Id values"); // Empty the group before getting the details again. groups.Clear(); groups = await autoScalingWrapper.DescribeAutoScalingGroupsAsync(groupName!); if (groups.Any()) { foreach (AutoScalingGroup group in groups) { Console.WriteLine($"The group name is {group.AutoScalingGroupName}"); Console.WriteLine($"The group ARN is {group.AutoScalingGroupARN}"); var instances = group.Instances; foreach (Amazon.AutoScaling.Model.Instance instance in instances) { Console.WriteLine($"The instance id is {instance.InstanceId}"); Console.WriteLine($"The lifecycle state is {instance.LifecycleState}"); } } } uiWrapper.DisplayTitle("Scaling Activities"); Console.WriteLine("Let's list the scaling activities that have occurred for the group."); var activities = await autoScalingWrapper.DescribeScalingActivitiesAsync(groupName!); if (activities.Any()) { activities.ForEach(activity => { Console.WriteLine($"The activity Id is {activity.ActivityId}"); Console.WriteLine($"The activity details are {activity.Details}"); }); } // Display the Amazon CloudWatch metrics that have been collected. var metrics = await cloudWatchWrapper.GetCloudWatchMetricsAsync(groupName!); if (metrics.Any()) { Console.WriteLine($"Metrics collected for {groupName}:"); metrics.ForEach(metric => { Console.Write($"Metric name: {metric.MetricName}\t"); Console.WriteLine($"Namespace: {metric.Namespace}"); }); } var dataPoints = await cloudWatchWrapper.GetMetricStatisticsAsync(groupName!); if (dataPoints.Any()) { Console.WriteLine("Details for the metrics collected:"); dataPoints.ForEach(detail => { Console.WriteLine(detail); }); } // Disable metrics collection. Console.WriteLine("Disabling the collection of metrics for {groupName}."); var success = await autoScalingWrapper.DisableMetricsCollectionAsync(groupName!); if (success) { Console.WriteLine($"Successfully stopped metrics collection for {groupName}."); } else { Console.WriteLine($"Could not stop metrics collection for {groupName}."); } // Terminate all instances in the group. uiWrapper.DisplayTitle("Terminating Auto Scaling instances"); Console.WriteLine("Now terminating all instances in the Auto Scaling group."); if (groups is not null) { groups.ForEach(group => { // Only delete instances in the AutoScaling group we created. if (group.AutoScalingGroupName == groupName) { group.Instances.ForEach(async instance => { await autoScalingWrapper.TerminateInstanceInAutoScalingGroupAsync(instance.InstanceId); }); } }); } // After all instances are terminated, delete the group. uiWrapper.DisplayTitle("Clean up resources"); Console.WriteLine("Deleting the Auto Scaling group."); await autoScalingWrapper.DeleteAutoScalingGroupAsync(groupName!); // Delete the launch template. var deletedLaunchTemplateName = await ec2Wrapper.DeleteLaunchTemplateAsync(launchTemplateId); if (deletedLaunchTemplateName == launchTemplateName) { Console.WriteLine("Successfully deleted the launch template."); } Console.WriteLine("The demo is now concluded."); } } namespace AutoScalingBasics; /// <summary> /// A class to provide user interface methods for the EC2 AutoScaling Basics /// scenario. /// </summary> public class UIWrapper { public readonly string SepBar = new('-', Console.WindowWidth); /// <summary> /// Describe the steps in the EC2 AutoScaling Basics scenario. /// </summary> public void DisplayAutoScalingBasicsDescription() { Console.WriteLine("This code example performs the following operations:"); Console.WriteLine(" 1. Creates an Amazon EC2 launch template."); Console.WriteLine(" 2. Creates an Auto Scaling group."); Console.WriteLine(" 3. Shows the details of the new Auto Scaling group"); Console.WriteLine(" to show that only one instance was created."); Console.WriteLine(" 4. Enables metrics collection."); Console.WriteLine(" 5. Updates the Auto Scaling group to increase the"); Console.WriteLine(" capacity to three."); Console.WriteLine(" 6. Describes Auto Scaling groups again to show the"); Console.WriteLine(" current state of the group."); Console.WriteLine(" 7. Changes the desired capacity of the Auto Scaling"); Console.WriteLine(" group to use an additional instance."); Console.WriteLine(" 8. Shows that there are now instances in the group."); Console.WriteLine(" 9. Lists the scaling activities that have occurred for the group."); Console.WriteLine("10. Displays the Amazon CloudWatch metrics that have"); Console.WriteLine(" been collected."); Console.WriteLine("11. Disables metrics collection."); Console.WriteLine("12. Terminates all instances in the Auto Scaling group."); Console.WriteLine("13. Deletes the Auto Scaling group."); Console.WriteLine("14. Deletes the Amazon EC2 launch template."); PressEnter(); } /// <summary> /// Display information about the Amazon Ec2 AutoScaling groups passed /// in the list of AutoScalingGroup objects. /// </summary> /// <param name="groups">A list of AutoScalingGroup objects.</param> public void DisplayGroupDetails(List<AutoScalingGroup> groups) { if (groups is null) return; groups.ForEach(group => { Console.WriteLine($"Group name:\t{group.AutoScalingGroupName}"); Console.WriteLine($"Group created:\t{group.CreatedTime}"); Console.WriteLine($"Maximum number of instances:\t{group.MaxSize}"); Console.WriteLine($"Desired number of instances:\t{group.DesiredCapacity}"); }); } /// <summary> /// Display a message and wait until the user presses enter. /// </summary> public void PressEnter() { Console.Write("\nPress <Enter> to continue. "); _ = Console.ReadLine(); Console.WriteLine(); } /// <summary> /// Pad a string with spaces to center it on the console display. /// </summary> /// <param name="strToCenter">The string to be centered.</param> /// <returns>The padded string.</returns> public string CenterString(string strToCenter) { var padAmount = (Console.WindowWidth - strToCenter.Length) / 2; var leftPad = new string(' ', padAmount); return $"{leftPad}{strToCenter}"; } /// <summary> /// Display a line of hyphens, the centered text of the title and another /// line of hyphens. /// </summary> /// <param name="strTitle">The string to be displayed.</param> public void DisplayTitle(string strTitle) { Console.WriteLine(SepBar); Console.WriteLine(CenterString(strTitle)); Console.WriteLine(SepBar); } /// <summary> /// Display a countdown and wait for a number of seconds. /// </summary> /// <param name="numSeconds">The number of seconds to wait.</param> public void WaitABit(int numSeconds, string msg) { Console.WriteLine(msg); // Wait for the requested number of seconds. for (int i = numSeconds; i > 0; i--) { System.Threading.Thread.Sleep(1000); Console.Write($"{i}..."); } PressEnter(); } }

Definieren Sie Funktionen, die vom Szenario aufgerufen werden, um Startvorlagen und Metriken zu verwalten. Diese Funktionen umfassen Auto Scaling EC2, Amazon und CloudWatch Aktionen.

namespace AutoScalingActions; using Amazon.AutoScaling; using Amazon.AutoScaling.Model; /// <summary> /// A class that includes methods to perform Amazon EC2 Auto Scaling /// actions. /// </summary> public class AutoScalingWrapper { private readonly IAmazonAutoScaling _amazonAutoScaling; /// <summary> /// Constructor for the AutoScalingWrapper class. /// </summary> /// <param name="amazonAutoScaling">The injected Amazon EC2 Auto Scaling client.</param> public AutoScalingWrapper(IAmazonAutoScaling amazonAutoScaling) { _amazonAutoScaling = amazonAutoScaling; } /// <summary> /// Create a new Amazon EC2 Auto Scaling group. /// </summary> /// <param name="groupName">The name to use for the new Auto Scaling /// group.</param> /// <param name="launchTemplateName">The name of the Amazon EC2 Auto Scaling /// launch template to use to create instances in the group.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> CreateAutoScalingGroupAsync( string groupName, string launchTemplateName, string availabilityZone) { var templateSpecification = new LaunchTemplateSpecification { LaunchTemplateName = launchTemplateName, }; var zoneList = new List<string> { availabilityZone, }; var request = new CreateAutoScalingGroupRequest { AutoScalingGroupName = groupName, AvailabilityZones = zoneList, LaunchTemplate = templateSpecification, MaxSize = 6, MinSize = 1 }; try { var response = await _amazonAutoScaling.CreateAutoScalingGroupAsync(request); Console.WriteLine($"{groupName} Auto Scaling Group created"); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AlreadyExistsException) { Console.WriteLine($"{groupName} Auto Scaling Group already exists."); return true; } } /// <summary> /// Retrieve information about Amazon EC2 Auto Scaling quotas to the /// active AWS account. /// </summary> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> DescribeAccountLimitsAsync() { var response = await _amazonAutoScaling.DescribeAccountLimitsAsync(); Console.WriteLine("The maximum number of Auto Scaling groups is " + response.MaxNumberOfAutoScalingGroups); Console.WriteLine("The current number of Auto Scaling groups is " + response.NumberOfAutoScalingGroups); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } /// <summary> /// Retrieve a list of the Amazon EC2 Auto Scaling activities for an /// Amazon EC2 Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Amazon EC2 Auto Scaling group.</param> /// <returns>A list of Amazon EC2 Auto Scaling activities.</returns> public async Task<List<Activity>> DescribeScalingActivitiesAsync( string groupName) { var activities = new List<Activity>(); var scalingActivitiesRequest = new DescribeScalingActivitiesRequest { AutoScalingGroupName = groupName, MaxRecords = 10, }; var response = await _amazonAutoScaling.DescribeScalingActivitiesAsync(scalingActivitiesRequest); if (response.Activities != null) { activities = response.Activities; } return activities; } /// <summary> /// Get data about the instances in an Amazon EC2 Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Amazon EC2 Auto Scaling group.</param> /// <returns>A list of Amazon EC2 Auto Scaling details.</returns> public async Task<List<AutoScalingInstanceDetails>> DescribeAutoScalingInstancesAsync( string groupName) { var groups = await DescribeAutoScalingGroupsAsync(groupName); var instanceIds = new List<string>(); var instanceDetails = new List<AutoScalingInstanceDetails>(); if (groups != null) { groups.ForEach(group => { if (group.AutoScalingGroupName == groupName && group.Instances != null) { group.Instances.ForEach(instance => { instanceIds.Add(instance.InstanceId); }); } }); var scalingGroupsRequest = new DescribeAutoScalingInstancesRequest { MaxRecords = 10, InstanceIds = instanceIds, }; var response = await _amazonAutoScaling.DescribeAutoScalingInstancesAsync( scalingGroupsRequest); if (response.AutoScalingInstances != null) { instanceDetails = response.AutoScalingInstances; } } return instanceDetails; } /// <summary> /// Retrieve a list of information about Amazon EC2 Auto Scaling groups. /// </summary> /// <param name="groupName">The name of the Amazon EC2 Auto Scaling group.</param> /// <returns>A list of Amazon EC2 Auto Scaling groups.</returns> public async Task<List<AutoScalingGroup>> DescribeAutoScalingGroupsAsync( string groupName) { var groups = new List<AutoScalingGroup>(); var groupList = new List<string> { groupName, }; var request = new DescribeAutoScalingGroupsRequest { AutoScalingGroupNames = groupList, }; var response = await _amazonAutoScaling.DescribeAutoScalingGroupsAsync(request); if (response.AutoScalingGroups != null) { groups = response.AutoScalingGroups; } return groups; } /// <summary> /// Delete an Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Amazon EC2 Auto Scaling group.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> DeleteAutoScalingGroupAsync( string groupName) { var deleteAutoScalingGroupRequest = new DeleteAutoScalingGroupRequest { AutoScalingGroupName = groupName, ForceDelete = true, }; var response = await _amazonAutoScaling.DeleteAutoScalingGroupAsync(deleteAutoScalingGroupRequest); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"You successfully deleted {groupName}"); return true; } Console.WriteLine($"Couldn't delete {groupName}."); return false; } /// <summary> /// Disable the collection of metric data for an Amazon EC2 Auto Scaling /// group. /// </summary> /// <param name="groupName">The name of the Auto Scaling group.</param> /// <returns>A Boolean value that indicates the success or failure of /// the operation.</returns> public async Task<bool> DisableMetricsCollectionAsync(string groupName) { var request = new DisableMetricsCollectionRequest { AutoScalingGroupName = groupName, }; var response = await _amazonAutoScaling.DisableMetricsCollectionAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } /// <summary> /// Enable the collection of metric data for an Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Auto Scaling group.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> EnableMetricsCollectionAsync(string groupName) { var listMetrics = new List<string> { "GroupMaxSize", }; var collectionRequest = new EnableMetricsCollectionRequest { AutoScalingGroupName = groupName, Metrics = listMetrics, Granularity = "1Minute", }; var response = await _amazonAutoScaling.EnableMetricsCollectionAsync(collectionRequest); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } /// <summary> /// Set the desired capacity of an Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Auto Scaling group.</param> /// <param name="desiredCapacity">The desired capacity for the Auto /// Scaling group.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> SetDesiredCapacityAsync( string groupName, int desiredCapacity) { var capacityRequest = new SetDesiredCapacityRequest { AutoScalingGroupName = groupName, DesiredCapacity = desiredCapacity, }; var response = await _amazonAutoScaling.SetDesiredCapacityAsync(capacityRequest); Console.WriteLine($"You have set the DesiredCapacity to {desiredCapacity}."); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } /// <summary> /// Terminate all instances in the Auto Scaling group in preparation for /// deleting the group. /// </summary> /// <param name="instanceId">The instance Id of the instance to terminate.</param> /// <returns>A Boolean value that indicates the success or failure of /// the operation.</returns> public async Task<bool> TerminateInstanceInAutoScalingGroupAsync( string instanceId) { var request = new TerminateInstanceInAutoScalingGroupRequest { InstanceId = instanceId, ShouldDecrementDesiredCapacity = false, }; var response = await _amazonAutoScaling.TerminateInstanceInAutoScalingGroupAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"You have terminated the instance: {instanceId}"); return true; } Console.WriteLine($"Could not terminate {instanceId}"); return false; } /// <summary> /// Update the capacity of an Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Auto Scaling group.</param> /// <param name="launchTemplateName">The name of the EC2 launch template.</param> /// <param name="maxSize">The maximum number of instances that can be /// created for the Auto Scaling group.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> UpdateAutoScalingGroupAsync( string groupName, string launchTemplateName, int maxSize) { var templateSpecification = new LaunchTemplateSpecification { LaunchTemplateName = launchTemplateName, }; var groupRequest = new UpdateAutoScalingGroupRequest { MaxSize = maxSize, AutoScalingGroupName = groupName, LaunchTemplate = templateSpecification, }; var response = await _amazonAutoScaling.UpdateAutoScalingGroupAsync(groupRequest); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"You successfully updated the Auto Scaling group {groupName}."); return true; } else { return false; } } } namespace AutoScalingActions; using Amazon.EC2; using Amazon.EC2.Model; public class EC2Wrapper { private readonly IAmazonEC2 _amazonEc2; /// <summary> /// Constructor for the EC2Wrapper class. /// </summary> /// <param name="amazonEc2">The injected Amazon EC2 client.</param> public EC2Wrapper(IAmazonEC2 amazonEc2) { _amazonEc2 = amazonEc2; } /// <summary> /// Create a new Amazon EC2 launch template. /// </summary> /// <param name="imageId">The image Id to use for instances launched /// using the Amazon EC2 launch template.</param> /// <param name="instanceType">The type of EC2 instances to create.</param> /// <param name="launchTemplateName">The name of the launch template.</param> /// <returns>Returns the TemplateID of the new launch template.</returns> public async Task<string> CreateLaunchTemplateAsync( string imageId, string instanceType, string launchTemplateName) { var request = new CreateLaunchTemplateRequest { LaunchTemplateData = new RequestLaunchTemplateData { ImageId = imageId, InstanceType = instanceType, }, LaunchTemplateName = launchTemplateName, }; var response = await _amazonEc2.CreateLaunchTemplateAsync(request); return response.LaunchTemplate.LaunchTemplateId; } /// <summary> /// Delete an Amazon EC2 launch template. /// </summary> /// <param name="launchTemplateId">The TemplateId of the launch template to /// delete.</param> /// <returns>The name of the EC2 launch template that was deleted.</returns> public async Task<string> DeleteLaunchTemplateAsync(string launchTemplateId) { var request = new DeleteLaunchTemplateRequest { LaunchTemplateId = launchTemplateId, }; var response = await _amazonEc2.DeleteLaunchTemplateAsync(request); return response.LaunchTemplate.LaunchTemplateName; } /// <summary> /// Retrieve information about an EC2 launch template. /// </summary> /// <param name="launchTemplateName">The name of the EC2 launch template.</param> /// <returns>A Boolean value that indicates the success or failure of /// the operation.</returns> public async Task<bool> DescribeLaunchTemplateAsync(string launchTemplateName) { var request = new DescribeLaunchTemplatesRequest { LaunchTemplateNames = new List<string> { launchTemplateName, }, }; var response = await _amazonEc2.DescribeLaunchTemplatesAsync(request); if (response.LaunchTemplates is not null) { response.LaunchTemplates.ForEach(template => { Console.Write($"{template.LaunchTemplateName}\t"); Console.WriteLine(template.LaunchTemplateId); }); return true; } return false; } /// <summary> /// Retrieve the availability zones for the current region. /// </summary> /// <returns>A collection of availability zones.</returns> public async Task<List<AvailabilityZone>> ListAvailabilityZonesAsync() { var response = await _amazonEc2.DescribeAvailabilityZonesAsync( new DescribeAvailabilityZonesRequest()); return response.AvailabilityZones; } } namespace AutoScalingActions; using Amazon.CloudWatch; using Amazon.CloudWatch.Model; /// <summary> /// Contains methods to access Amazon CloudWatch metrics for the /// Amazon EC2 Auto Scaling basics scenario. /// </summary> public class CloudWatchWrapper { private readonly IAmazonCloudWatch _amazonCloudWatch; /// <summary> /// Constructor for the CloudWatchWrapper. /// </summary> /// <param name="amazonCloudWatch">The injected CloudWatch client.</param> public CloudWatchWrapper(IAmazonCloudWatch amazonCloudWatch) { _amazonCloudWatch = amazonCloudWatch; } /// <summary> /// Retrieve the metrics information collection for the Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Auto Scaling group.</param> /// <returns>A list of Metrics collected for the Auto Scaling group.</returns> public async Task<List<Metric>> GetCloudWatchMetricsAsync(string groupName) { var metrics = new List<Metric>(); var filter = new DimensionFilter { Name = "AutoScalingGroupName", Value = $"{groupName}", }; var request = new ListMetricsRequest { MetricName = "AutoScalingGroupName", Dimensions = new List<DimensionFilter> { filter }, Namespace = "AWS/AutoScaling", }; var response = await _amazonCloudWatch.ListMetricsAsync(request); if (response.Metrics != null) { metrics = response.Metrics; } return metrics; } /// <summary> /// Retrieve the metric data collected for an Amazon EC2 Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Amazon EC2 Auto Scaling group.</param> /// <returns>A list of data points.</returns> public async Task<List<Datapoint>> GetMetricStatisticsAsync(string groupName) { var dataPoints = new List<Datapoint>(); var metricDimensions = new List<Dimension> { new Dimension { Name = "AutoScalingGroupName", Value = $"{groupName}", }, }; // The start time will be yesterday. var startTime = DateTime.UtcNow.AddDays(-1); var request = new GetMetricStatisticsRequest { MetricName = "AutoScalingGroupName", Dimensions = metricDimensions, Namespace = "AWS/AutoScaling", Period = 60, // 60 seconds. Statistics = new List<string>() { "Minimum" }, StartTimeUtc = startTime, EndTimeUtc = DateTime.UtcNow, }; var response = await _amazonCloudWatch.GetMetricStatisticsAsync(request); if (response.Datapoints != null) { dataPoints = response.Datapoints; } return dataPoints; } }

Aktionen

Das folgende Codebeispiel zeigt die VerwendungCreateAutoScalingGroup.

SDK für .NET (v4)
Anmerkung

Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel- einrichten und ausführen.

/// <summary> /// Create a new Amazon EC2 Auto Scaling group. /// </summary> /// <param name="groupName">The name to use for the new Auto Scaling /// group.</param> /// <param name="launchTemplateName">The name of the Amazon EC2 Auto Scaling /// launch template to use to create instances in the group.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> CreateAutoScalingGroupAsync( string groupName, string launchTemplateName, string availabilityZone) { var templateSpecification = new LaunchTemplateSpecification { LaunchTemplateName = launchTemplateName, }; var zoneList = new List<string> { availabilityZone, }; var request = new CreateAutoScalingGroupRequest { AutoScalingGroupName = groupName, AvailabilityZones = zoneList, LaunchTemplate = templateSpecification, MaxSize = 6, MinSize = 1 }; try { var response = await _amazonAutoScaling.CreateAutoScalingGroupAsync(request); Console.WriteLine($"{groupName} Auto Scaling Group created"); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AlreadyExistsException) { Console.WriteLine($"{groupName} Auto Scaling Group already exists."); return true; } }

Das folgende Codebeispiel zeigt die VerwendungDescribeAutoScalingGroups.

SDK für .NET (v4)
Anmerkung

Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel- einrichten und ausführen.

/// <summary> /// Get data about the instances in an Amazon EC2 Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Amazon EC2 Auto Scaling group.</param> /// <returns>A list of Amazon EC2 Auto Scaling details.</returns> public async Task<List<AutoScalingInstanceDetails>> DescribeAutoScalingInstancesAsync( string groupName) { var groups = await DescribeAutoScalingGroupsAsync(groupName); var instanceIds = new List<string>(); var instanceDetails = new List<AutoScalingInstanceDetails>(); if (groups != null) { groups.ForEach(group => { if (group.AutoScalingGroupName == groupName && group.Instances != null) { group.Instances.ForEach(instance => { instanceIds.Add(instance.InstanceId); }); } }); var scalingGroupsRequest = new DescribeAutoScalingInstancesRequest { MaxRecords = 10, InstanceIds = instanceIds, }; var response = await _amazonAutoScaling.DescribeAutoScalingInstancesAsync( scalingGroupsRequest); if (response.AutoScalingInstances != null) { instanceDetails = response.AutoScalingInstances; } } return instanceDetails; }

Das folgende Codebeispiel zeigt die VerwendungDescribeAutoScalingInstances.

SDK für .NET (v4)
Anmerkung

Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel- einrichten und ausführen.

/// <summary> /// Get data about the instances in an Amazon EC2 Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Amazon EC2 Auto Scaling group.</param> /// <returns>A list of Amazon EC2 Auto Scaling details.</returns> public async Task<List<AutoScalingInstanceDetails>> DescribeAutoScalingInstancesAsync( string groupName) { var groups = await DescribeAutoScalingGroupsAsync(groupName); var instanceIds = new List<string>(); var instanceDetails = new List<AutoScalingInstanceDetails>(); if (groups != null) { groups.ForEach(group => { if (group.AutoScalingGroupName == groupName && group.Instances != null) { group.Instances.ForEach(instance => { instanceIds.Add(instance.InstanceId); }); } }); var scalingGroupsRequest = new DescribeAutoScalingInstancesRequest { MaxRecords = 10, InstanceIds = instanceIds, }; var response = await _amazonAutoScaling.DescribeAutoScalingInstancesAsync( scalingGroupsRequest); if (response.AutoScalingInstances != null) { instanceDetails = response.AutoScalingInstances; } } return instanceDetails; }

Das folgende Codebeispiel zeigt die VerwendungDescribeScalingActivities.

SDK für .NET (v4)
Anmerkung

Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel- einrichten und ausführen.

/// <summary> /// Retrieve a list of the Amazon EC2 Auto Scaling activities for an /// Amazon EC2 Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Amazon EC2 Auto Scaling group.</param> /// <returns>A list of Amazon EC2 Auto Scaling activities.</returns> public async Task<List<Activity>> DescribeScalingActivitiesAsync( string groupName) { var activities = new List<Activity>(); var scalingActivitiesRequest = new DescribeScalingActivitiesRequest { AutoScalingGroupName = groupName, MaxRecords = 10, }; var response = await _amazonAutoScaling.DescribeScalingActivitiesAsync(scalingActivitiesRequest); if (response.Activities != null) { activities = response.Activities; } return activities; }

Das folgende Codebeispiel zeigt die VerwendungDisableMetricsCollection.

SDK für .NET (v4)
Anmerkung

Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel- einrichten und ausführen.

/// <summary> /// Disable the collection of metric data for an Amazon EC2 Auto Scaling /// group. /// </summary> /// <param name="groupName">The name of the Auto Scaling group.</param> /// <returns>A Boolean value that indicates the success or failure of /// the operation.</returns> public async Task<bool> DisableMetricsCollectionAsync(string groupName) { var request = new DisableMetricsCollectionRequest { AutoScalingGroupName = groupName, }; var response = await _amazonAutoScaling.DisableMetricsCollectionAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }

Das folgende Codebeispiel zeigt die VerwendungEnableMetricsCollection.

SDK für .NET (v4)
Anmerkung

Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel- einrichten und ausführen.

/// <summary> /// Enable the collection of metric data for an Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Auto Scaling group.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> EnableMetricsCollectionAsync(string groupName) { var listMetrics = new List<string> { "GroupMaxSize", }; var collectionRequest = new EnableMetricsCollectionRequest { AutoScalingGroupName = groupName, Metrics = listMetrics, Granularity = "1Minute", }; var response = await _amazonAutoScaling.EnableMetricsCollectionAsync(collectionRequest); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }

Das folgende Codebeispiel zeigt die VerwendungSetDesiredCapacity.

SDK für .NET (v4)
Anmerkung

Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel- einrichten und ausführen.

/// <summary> /// Set the desired capacity of an Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Auto Scaling group.</param> /// <param name="desiredCapacity">The desired capacity for the Auto /// Scaling group.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> SetDesiredCapacityAsync( string groupName, int desiredCapacity) { var capacityRequest = new SetDesiredCapacityRequest { AutoScalingGroupName = groupName, DesiredCapacity = desiredCapacity, }; var response = await _amazonAutoScaling.SetDesiredCapacityAsync(capacityRequest); Console.WriteLine($"You have set the DesiredCapacity to {desiredCapacity}."); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }
  • Einzelheiten zur API finden Sie SetDesiredCapacityin der AWS SDK für .NET API-Referenz.

Das folgende Codebeispiel zeigt die VerwendungTerminateInstanceInAutoScalingGroup.

SDK für .NET (v4)
Anmerkung

Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel- einrichten und ausführen.

/// <summary> /// Terminate all instances in the Auto Scaling group in preparation for /// deleting the group. /// </summary> /// <param name="instanceId">The instance Id of the instance to terminate.</param> /// <returns>A Boolean value that indicates the success or failure of /// the operation.</returns> public async Task<bool> TerminateInstanceInAutoScalingGroupAsync( string instanceId) { var request = new TerminateInstanceInAutoScalingGroupRequest { InstanceId = instanceId, ShouldDecrementDesiredCapacity = false, }; var response = await _amazonAutoScaling.TerminateInstanceInAutoScalingGroupAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"You have terminated the instance: {instanceId}"); return true; } Console.WriteLine($"Could not terminate {instanceId}"); return false; }

Das folgende Codebeispiel zeigt die VerwendungUpdateAutoScalingGroup.

SDK für .NET (v4)
Anmerkung

Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel- einrichten und ausführen.

/// <summary> /// Update the capacity of an Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Auto Scaling group.</param> /// <param name="launchTemplateName">The name of the EC2 launch template.</param> /// <param name="maxSize">The maximum number of instances that can be /// created for the Auto Scaling group.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> UpdateAutoScalingGroupAsync( string groupName, string launchTemplateName, int maxSize) { var templateSpecification = new LaunchTemplateSpecification { LaunchTemplateName = launchTemplateName, }; var groupRequest = new UpdateAutoScalingGroupRequest { MaxSize = maxSize, AutoScalingGroupName = groupName, LaunchTemplate = templateSpecification, }; var response = await _amazonAutoScaling.UpdateAutoScalingGroupAsync(groupRequest); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"You successfully updated the Auto Scaling group {groupName}."); return true; } else { return false; } }