文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
了解使用 AWS SDK Auto Scaling 的基本概念
下列程式碼範例示範如何:
使用啟動範本和可用區域建立 Amazon EC2 Auto Scaling 群組,並取得執行中執行個體的相關資訊。
啟用 Amazon CloudWatch 指標集合。
更新群組所需的容量,並等待執行個體啟動。
終止 群組中的執行個體。
列出為回應使用者請求和容量變更而發生的擴展活動。
取得 CloudWatch 指標的統計資料,然後清除資源。
- AWS SDK for .NET
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 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.First().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 is not null) { 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 is not null) { 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!); 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!); 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(); } }
定義 案例呼叫的函數,以管理啟動範本和指標。這些函數會包裝 Auto Scaling、Amazon EC2 和 CloudWatch 動作。
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 }; var response = await _amazonAutoScaling.CreateAutoScalingGroupAsync(request); Console.WriteLine($"{groupName} Auto Scaling Group created"); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } /// <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<Amazon.AutoScaling.Model.Activity>> DescribeScalingActivitiesAsync( string groupName) { var scalingActivitiesRequest = new DescribeScalingActivitiesRequest { AutoScalingGroupName = groupName, MaxRecords = 10, }; var response = await _amazonAutoScaling.DescribeScalingActivitiesAsync(scalingActivitiesRequest); return response.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>(); groups!.ForEach(group => { if (group.AutoScalingGroupName == groupName) { group.Instances.ForEach(instance => { instanceIds.Add(instance.InstanceId); }); } }); var scalingGroupsRequest = new DescribeAutoScalingInstancesRequest { MaxRecords = 10, InstanceIds = instanceIds, }; var response = await _amazonAutoScaling.DescribeAutoScalingInstancesAsync(scalingGroupsRequest); var 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 groupList = new List<string> { groupName, }; var request = new DescribeAutoScalingGroupsRequest { AutoScalingGroupNames = groupList, }; var response = await _amazonAutoScaling.DescribeAutoScalingGroupsAsync(request); var 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<Amazon.CloudWatch.Model.Metric>> GetCloudWatchMetricsAsync(string groupName) { 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); return response.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 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); return response.Datapoints; } }