From 4fc139130397e9d690aeb81e05f86110da543279 Mon Sep 17 00:00:00 2001 From: stack72 Date: Fri, 15 Jan 2016 10:29:15 +0000 Subject: [PATCH 1/2] Scaffold the AWS Autoscaling Group Metrics Collection --- builtin/providers/aws/provider.go | 1 + .../aws/resource_aws_autoscaling_group.go | 13 ++ ...urce_aws_autoscaling_metrics_collection.go | 142 +++++++++++++ ...aws_autoscaling_metrics_collection_test.go | 189 ++++++++++++++++++ builtin/providers/aws/structure.go | 11 + builtin/providers/aws/structure_test.go | 22 ++ ...toscaling_metrics_collection.html.markdown | 57 ++++++ website/source/layouts/aws.erb | 4 + 8 files changed, 439 insertions(+) create mode 100644 builtin/providers/aws/resource_aws_autoscaling_metrics_collection.go create mode 100644 builtin/providers/aws/resource_aws_autoscaling_metrics_collection_test.go create mode 100644 website/source/docs/providers/aws/r/autoscaling_metrics_collection.html.markdown diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index fdf84166f..f18f9f7ec 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -116,6 +116,7 @@ func Provider() terraform.ResourceProvider { "aws_ami_from_instance": resourceAwsAmiFromInstance(), "aws_app_cookie_stickiness_policy": resourceAwsAppCookieStickinessPolicy(), "aws_autoscaling_group": resourceAwsAutoscalingGroup(), + "aws_autoscaling_metrics_collection": resourceAwsAutoscalingMetric(), "aws_autoscaling_notification": resourceAwsAutoscalingNotification(), "aws_autoscaling_policy": resourceAwsAutoscalingPolicy(), "aws_autoscaling_schedule": resourceAwsAutoscalingSchedule(), diff --git a/builtin/providers/aws/resource_aws_autoscaling_group.go b/builtin/providers/aws/resource_aws_autoscaling_group.go index 73752c795..a5f7e8c1d 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_group.go +++ b/builtin/providers/aws/resource_aws_autoscaling_group.go @@ -147,6 +147,13 @@ func resourceAwsAutoscalingGroup() *schema.Resource { Optional: true, }, + "enabled_metrics": &schema.Schema{ + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + "tag": autoscalingTagsSchema(), }, } @@ -266,6 +273,12 @@ func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) e d.Set("termination_policies", flattenStringList(g.TerminationPolicies)) } + if g.EnabledMetrics != nil { + if err := d.Set("enabled_metrics", flattenAsgEnabledMetrics(g.EnabledMetrics)); err != nil { + log.Printf("[WARN] Error setting metrics for (%s): %s", d.Id(), err) + } + } + return nil } diff --git a/builtin/providers/aws/resource_aws_autoscaling_metrics_collection.go b/builtin/providers/aws/resource_aws_autoscaling_metrics_collection.go new file mode 100644 index 000000000..3636eafef --- /dev/null +++ b/builtin/providers/aws/resource_aws_autoscaling_metrics_collection.go @@ -0,0 +1,142 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/autoscaling" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsAutoscalingMetric() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsAutoscalingMetricCreate, + Read: resourceAwsAutoscalingMetricRead, + Update: resourceAwsAutoscalingMetricUpdate, + Delete: resourceAwsAutoscalingMetricDelete, + + Schema: map[string]*schema.Schema{ + "autoscaling_group_name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "metrics": &schema.Schema{ + Type: schema.TypeSet, + Required: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + + "granularity": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func resourceAwsAutoscalingMetricCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).autoscalingconn + + asgName := d.Get("autoscaling_group_name").(string) + props := &autoscaling.EnableMetricsCollectionInput{ + AutoScalingGroupName: aws.String(asgName), + Granularity: aws.String(d.Get("granularity").(string)), + Metrics: expandStringList(d.Get("metrics").(*schema.Set).List()), + } + + log.Printf("[INFO] Enabling metrics collection for the ASG: %s", asgName) + _, err := conn.EnableMetricsCollection(props) + if err != nil { + return err + } + + d.SetId(d.Get("autoscaling_group_name").(string)) + + return resourceAwsAutoscalingMetricRead(d, meta) +} + +func resourceAwsAutoscalingMetricRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).autoscalingconn + + g, err := getAwsAutoscalingGroup(d.Get("autoscaling_group_name").(string), conn) + if err != nil { + return err + } + if g == nil { + return nil + } + + if g.EnabledMetrics != nil && len(g.EnabledMetrics) > 0 { + if err := d.Set("metrics", flattenAsgEnabledMetrics(g.EnabledMetrics)); err != nil { + log.Printf("[WARN] Error setting metrics for (%s): %s", d.Id(), err) + } + d.Set("granularity", g.EnabledMetrics[0].Granularity) + } + + return nil +} + +func resourceAwsAutoscalingMetricUpdate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).autoscalingconn + + if d.HasChange("metrics") { + o, n := d.GetChange("metrics") + if o == nil { + o = new(schema.Set) + } + if n == nil { + n = new(schema.Set) + } + + os := o.(*schema.Set) + ns := n.(*schema.Set) + + disableMetrics := os.Difference(ns) + if disableMetrics.Len() != 0 { + props := &autoscaling.DisableMetricsCollectionInput{ + AutoScalingGroupName: aws.String(d.Id()), + Metrics: expandStringList(disableMetrics.List()), + } + + _, err := conn.DisableMetricsCollection(props) + if err != nil { + return fmt.Errorf("Failure to Disable metrics collection types for ASG %s: %s", d.Id(), err) + } + } + + enabledMetrics := ns.Difference(os) + if enabledMetrics.Len() != 0 { + props := &autoscaling.EnableMetricsCollectionInput{ + AutoScalingGroupName: aws.String(d.Id()), + Metrics: expandStringList(enabledMetrics.List()), + } + + _, err := conn.EnableMetricsCollection(props) + if err != nil { + return fmt.Errorf("Failure to Enable metrics collection types for ASG %s: %s", d.Id(), err) + } + } + } + + return resourceAwsAutoscalingMetricRead(d, meta) +} + +func resourceAwsAutoscalingMetricDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).autoscalingconn + + props := &autoscaling.DisableMetricsCollectionInput{ + AutoScalingGroupName: aws.String(d.Id()), + } + + log.Printf("[INFO] Disabling ALL metrics collection for the ASG: %s", d.Id()) + _, err := conn.DisableMetricsCollection(props) + if err != nil { + return err + } + + return nil +} diff --git a/builtin/providers/aws/resource_aws_autoscaling_metrics_collection_test.go b/builtin/providers/aws/resource_aws_autoscaling_metrics_collection_test.go new file mode 100644 index 000000000..a2cb6324d --- /dev/null +++ b/builtin/providers/aws/resource_aws_autoscaling_metrics_collection_test.go @@ -0,0 +1,189 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/service/autoscaling" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSAutoscalingMetricsCollection_basic(t *testing.T) { + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAutoscalingMetricsCollectionDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAutoscalingMetricsCollectionExists("aws_autoscaling_metrics_collection.test"), + resource.TestCheckResourceAttr( + "aws_autoscaling_metrics_collection.test", "metrics.#", "7"), + ), + }, + }, + }) +} + +func TestAccAWSAutoscalingMetricsCollection_update(t *testing.T) { + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAutoscalingMetricsCollectionDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAutoscalingMetricsCollectionExists("aws_autoscaling_metrics_collection.test"), + resource.TestCheckResourceAttr( + "aws_autoscaling_metrics_collection.test", "metrics.#", "7"), + ), + }, + + resource.TestStep{ + Config: testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAutoscalingMetricsCollectionExists("aws_autoscaling_metrics_collection.test"), + resource.TestCheckResourceAttr( + "aws_autoscaling_metrics_collection.test", "metrics.#", "5"), + ), + }, + }, + }) +} +func testAccCheckAWSAutoscalingMetricsCollectionExists(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No AutoScaling Group ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).autoscalingconn + + describeGroups, err := conn.DescribeAutoScalingGroups( + &autoscaling.DescribeAutoScalingGroupsInput{ + AutoScalingGroupNames: []*string{aws.String(rs.Primary.ID)}, + }) + + if err != nil { + return err + } + + if len(describeGroups.AutoScalingGroups) != 1 || + *describeGroups.AutoScalingGroups[0].AutoScalingGroupName != rs.Primary.ID { + return fmt.Errorf("AutoScaling Group not found") + } + + if describeGroups.AutoScalingGroups[0].EnabledMetrics == nil { + return fmt.Errorf("AutoScaling Groups Metrics Collection not found") + } + + return nil + } +} + +func testAccCheckAWSAutoscalingMetricsCollectionDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).autoscalingconn + + for _, rs := range s.RootModule().Resources { + // Try to find the Group + describeGroups, err := conn.DescribeAutoScalingGroups( + &autoscaling.DescribeAutoScalingGroupsInput{ + AutoScalingGroupNames: []*string{aws.String(rs.Primary.ID)}, + }) + + if err == nil { + if len(describeGroups.AutoScalingGroups) != 0 && + *describeGroups.AutoScalingGroups[0].AutoScalingGroupName == rs.Primary.ID { + return fmt.Errorf("AutoScalingGroup still exists") + } + } + + // Verify the error + ec2err, ok := err.(awserr.Error) + if !ok { + return err + } + if ec2err.Code() != "InvalidGroup.NotFound" { + return err + } + } + + return nil +} + +const testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected = ` +resource "aws_launch_configuration" "foobar" { + image_id = "ami-21f78e11" + instance_type = "t1.micro" +} + +resource "aws_autoscaling_group" "bar" { + availability_zones = ["us-west-2a"] + name = "foobar3-terraform-test" + max_size = 1 + min_size = 0 + health_check_grace_period = 300 + health_check_type = "EC2" + desired_capacity = 0 + force_delete = true + termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] + + launch_configuration = "${aws_launch_configuration.foobar.name}" +} + +resource "aws_autoscaling_metrics_collection" "test" { + autoscaling_group_name = "${aws_autoscaling_group.bar.name}" + granularity = "1Minute" + metrics = ["GroupTotalInstances", + "GroupPendingInstances", + "GroupTerminatingInstances", + "GroupDesiredCapacity", + "GroupInServiceInstances", + "GroupMinSize", + "GroupMaxSize" + ] +} +` + +const testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected = ` +resource "aws_launch_configuration" "foobar" { + image_id = "ami-21f78e11" + instance_type = "t1.micro" +} + +resource "aws_autoscaling_group" "bar" { + availability_zones = ["us-west-2a"] + name = "foobar3-terraform-test" + max_size = 1 + min_size = 0 + health_check_grace_period = 300 + health_check_type = "EC2" + desired_capacity = 0 + force_delete = true + termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] + + launch_configuration = "${aws_launch_configuration.foobar.name}" +} + +resource "aws_autoscaling_metrics_collection" "test" { + autoscaling_group_name = "${aws_autoscaling_group.bar.name}" + granularity = "1Minute" + metrics = ["GroupTotalInstances", + "GroupPendingInstances", + "GroupTerminatingInstances", + "GroupDesiredCapacity", + "GroupMaxSize" + ] +} +` diff --git a/builtin/providers/aws/structure.go b/builtin/providers/aws/structure.go index 6facdf792..7937322f8 100644 --- a/builtin/providers/aws/structure.go +++ b/builtin/providers/aws/structure.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/autoscaling" "github.com/aws/aws-sdk-go/service/cloudformation" "github.com/aws/aws-sdk-go/service/directoryservice" "github.com/aws/aws-sdk-go/service/ec2" @@ -772,3 +773,13 @@ func flattenCloudFormationOutputs(cfOutputs []*cloudformation.Output) map[string } return outputs } + +func flattenAsgEnabledMetrics(list []*autoscaling.EnabledMetric) []string { + strs := make([]string, 0, len(list)) + for _, r := range list { + if r.Metric != nil { + strs = append(strs, *r.Metric) + } + } + return strs +} diff --git a/builtin/providers/aws/structure_test.go b/builtin/providers/aws/structure_test.go index 998a25747..898d93ce7 100644 --- a/builtin/providers/aws/structure_test.go +++ b/builtin/providers/aws/structure_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/autoscaling" "github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/elasticache" "github.com/aws/aws-sdk-go/service/elb" @@ -700,3 +701,24 @@ func TestFlattenResourceRecords(t *testing.T) { t.Fatal("expected result to have value, but got nil") } } + +func TestFlattenAsgEnabledMetrics(t *testing.T) { + expanded := []*autoscaling.EnabledMetric{ + &autoscaling.EnabledMetric{Granularity: aws.String("1Minute"), Metric: aws.String("GroupTotalInstances")}, + &autoscaling.EnabledMetric{Granularity: aws.String("1Minute"), Metric: aws.String("GroupMaxSize")}, + } + + result := flattenAsgEnabledMetrics(expanded) + + if len(result) != 2 { + t.Fatalf("expected result had %d elements, but got %d", 2, len(result)) + } + + if result[0] != "GroupTotalInstances" { + t.Fatalf("expected id to be GroupTotalInstances, but was %s", result[0]) + } + + if result[1] != "GroupMaxSize" { + t.Fatalf("expected id to be GroupMaxSize, but was %s", result[1]) + } +} diff --git a/website/source/docs/providers/aws/r/autoscaling_metrics_collection.html.markdown b/website/source/docs/providers/aws/r/autoscaling_metrics_collection.html.markdown new file mode 100644 index 000000000..607d87c8e --- /dev/null +++ b/website/source/docs/providers/aws/r/autoscaling_metrics_collection.html.markdown @@ -0,0 +1,57 @@ +--- +layout: "aws" +page_title: "AWS: aws_autoscaling_metrics_collection" +sidebar_current: "docs-aws-resource-autoscaling-metrics-collection" +description: |- + Enables / Disables Autoscaling Group Metrics Collection. +--- + +# aws\_autoscaling\_metrics\_collection + +Enables / Disables Autoscaling Group Metrics Collection. + +~> **NOTE:** You can only enable metrics collection for an Autoscaling Group if `enable_monitoring` +in it's underlying launch configuration for the group is set to `True`. + +## Example Usage + +``` +resource "aws_launch_configuration" "foobar" { + name = "web_config" + image_id = "ami-408c7f28" + instance_type = "t1.micro" + enable_monitoring = true +} + +resource "aws_autoscaling_group" "foobar" { + availability_zones = ["us-west-2a"] + name = "terraform-test-foobar5" + health_check_type = "EC2" + termination_policies = ["OldestInstance"] + launch_configuration = "${aws_launch_configuration.foobar.name}" + tag { + key = "Foo" + value = "foo-bar" + propagate_at_launch = true + } +} + +resource "aws_autoscaling_metrics_collection" "test" { + autoscaling_group_name = "${aws_autoscaling_group.bar.name}" + granularity = "1Minute" + metrics = ["GroupTotalInstances", + "GroupPendingInstances", + "GroupTerminatingInstances", + "GroupDesiredCapacity", + "GroupMaxSize" + ] +} +``` + +## Argument Reference + +The following arguments are supported: + +* `autoscaling_group_name` - (Required) The name of the Auto Scaling group to which you want to enable / disable metrics collection +* `granularity` - (Required) The granularity to associate with the metrics to collect. The only valid value is `1Minute`. +* `metrics` - (Required) A list of metrics to collect. The allowed values are `GroupMinSize`, `GroupMaxSize`, `GroupDesiredCapacity`, `GroupInServiceInstances`, `GroupPendingInstances`, `GroupStandbyInstances`, `GroupTerminatingInstances`, `GroupTotalInstances`. \ No newline at end of file diff --git a/website/source/layouts/aws.erb b/website/source/layouts/aws.erb index 9643b18ff..c2e3ad256 100644 --- a/website/source/layouts/aws.erb +++ b/website/source/layouts/aws.erb @@ -127,6 +127,10 @@ aws_autoscaling_lifecycle_hook + > + aws_autoscaling_metrics_collection + + > aws_autoscaling_notification From d3697fa50b04e869f2377323b6457b6064598a4b Mon Sep 17 00:00:00 2001 From: stack72 Date: Mon, 29 Feb 2016 20:58:41 +0000 Subject: [PATCH 2/2] provider/aws: Combine AWS Metrics Collection resource and AWS Autoscaling Group --- builtin/providers/aws/provider.go | 1 - .../aws/resource_aws_autoscaling_group.go | 78 +++++++- .../resource_aws_autoscaling_group_test.go | 83 ++++++++ ...urce_aws_autoscaling_metrics_collection.go | 142 ------------- ...aws_autoscaling_metrics_collection_test.go | 189 ------------------ .../aws/r/autoscaling_group.html.markdown | 2 + ...toscaling_metrics_collection.html.markdown | 57 ------ website/source/layouts/aws.erb | 4 - 8 files changed, 162 insertions(+), 394 deletions(-) delete mode 100644 builtin/providers/aws/resource_aws_autoscaling_metrics_collection.go delete mode 100644 builtin/providers/aws/resource_aws_autoscaling_metrics_collection_test.go delete mode 100644 website/source/docs/providers/aws/r/autoscaling_metrics_collection.html.markdown diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index f18f9f7ec..fdf84166f 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -116,7 +116,6 @@ func Provider() terraform.ResourceProvider { "aws_ami_from_instance": resourceAwsAmiFromInstance(), "aws_app_cookie_stickiness_policy": resourceAwsAppCookieStickinessPolicy(), "aws_autoscaling_group": resourceAwsAutoscalingGroup(), - "aws_autoscaling_metrics_collection": resourceAwsAutoscalingMetric(), "aws_autoscaling_notification": resourceAwsAutoscalingNotification(), "aws_autoscaling_policy": resourceAwsAutoscalingPolicy(), "aws_autoscaling_schedule": resourceAwsAutoscalingSchedule(), diff --git a/builtin/providers/aws/resource_aws_autoscaling_group.go b/builtin/providers/aws/resource_aws_autoscaling_group.go index a5f7e8c1d..7c5b38769 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_group.go +++ b/builtin/providers/aws/resource_aws_autoscaling_group.go @@ -149,11 +149,17 @@ func resourceAwsAutoscalingGroup() *schema.Resource { "enabled_metrics": &schema.Schema{ Type: schema.TypeSet, - Computed: true, + Optional: true, Elem: &schema.Schema{Type: schema.TypeString}, Set: schema.HashString, }, + "metrics_granularity": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Default: "1Minute", + }, + "tag": autoscalingTagsSchema(), }, } @@ -233,6 +239,13 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{}) return err } + if _, ok := d.GetOk("enabled_metrics"); ok { + metricsErr := enableASGMetricsCollection(d, conn) + if metricsErr != nil { + return metricsErr + } + } + return resourceAwsAutoscalingGroupRead(d, meta) } @@ -277,6 +290,7 @@ func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) e if err := d.Set("enabled_metrics", flattenAsgEnabledMetrics(g.EnabledMetrics)); err != nil { log.Printf("[WARN] Error setting metrics for (%s): %s", d.Id(), err) } + d.Set("metrics_granularity", g.EnabledMetrics[0].Granularity) } return nil @@ -399,6 +413,10 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{}) waitForASGCapacity(d, meta, capacitySatifiedUpdate) } + if d.HasChange("enabled_metrics") { + updateASGMetricsCollection(d, conn) + } + return resourceAwsAutoscalingGroupRead(d, meta) } @@ -532,6 +550,64 @@ func resourceAwsAutoscalingGroupDrain(d *schema.ResourceData, meta interface{}) }) } +func enableASGMetricsCollection(d *schema.ResourceData, conn *autoscaling.AutoScaling) error { + props := &autoscaling.EnableMetricsCollectionInput{ + AutoScalingGroupName: aws.String(d.Id()), + Granularity: aws.String(d.Get("metrics_granularity").(string)), + Metrics: expandStringList(d.Get("enabled_metrics").(*schema.Set).List()), + } + + log.Printf("[INFO] Enabling metrics collection for the ASG: %s", d.Id()) + _, metricsErr := conn.EnableMetricsCollection(props) + if metricsErr != nil { + return metricsErr + } + + return nil +} + +func updateASGMetricsCollection(d *schema.ResourceData, conn *autoscaling.AutoScaling) error { + + o, n := d.GetChange("enabled_metrics") + if o == nil { + o = new(schema.Set) + } + if n == nil { + n = new(schema.Set) + } + + os := o.(*schema.Set) + ns := n.(*schema.Set) + + disableMetrics := os.Difference(ns) + if disableMetrics.Len() != 0 { + props := &autoscaling.DisableMetricsCollectionInput{ + AutoScalingGroupName: aws.String(d.Id()), + Metrics: expandStringList(disableMetrics.List()), + } + + _, err := conn.DisableMetricsCollection(props) + if err != nil { + return fmt.Errorf("Failure to Disable metrics collection types for ASG %s: %s", d.Id(), err) + } + } + + enabledMetrics := ns.Difference(os) + if enabledMetrics.Len() != 0 { + props := &autoscaling.EnableMetricsCollectionInput{ + AutoScalingGroupName: aws.String(d.Id()), + Metrics: expandStringList(enabledMetrics.List()), + } + + _, err := conn.EnableMetricsCollection(props) + if err != nil { + return fmt.Errorf("Failure to Enable metrics collection types for ASG %s: %s", d.Id(), err) + } + } + + return nil +} + // Returns a mapping of the instance states of all the ELBs attached to the // provided ASG. // diff --git a/builtin/providers/aws/resource_aws_autoscaling_group_test.go b/builtin/providers/aws/resource_aws_autoscaling_group_test.go index 4292baca8..bb23f570a 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_group_test.go +++ b/builtin/providers/aws/resource_aws_autoscaling_group_test.go @@ -247,6 +247,35 @@ func TestAccAWSAutoScalingGroup_withPlacementGroup(t *testing.T) { }) } +func TestAccAWSAutoScalingGroup_withMetrics(t *testing.T) { + var group autoscaling.Group + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), + resource.TestCheckResourceAttr( + "aws_autoscaling_group.bar", "enabled_metrics.#", "7"), + ), + }, + + resource.TestStep{ + Config: testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), + resource.TestCheckResourceAttr( + "aws_autoscaling_group.bar", "enabled_metrics.#", "5"), + ), + }, + }, + }) +} + func testAccCheckAWSAutoScalingGroupDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).autoscalingconn @@ -751,3 +780,57 @@ resource "aws_autoscaling_group" "bar" { } } ` + +const testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected = ` +resource "aws_launch_configuration" "foobar" { + image_id = "ami-21f78e11" + instance_type = "t1.micro" +} + +resource "aws_autoscaling_group" "bar" { + availability_zones = ["us-west-2a"] + max_size = 1 + min_size = 0 + health_check_grace_period = 300 + health_check_type = "EC2" + desired_capacity = 0 + force_delete = true + termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] + launch_configuration = "${aws_launch_configuration.foobar.name}" + enabled_metrics = ["GroupTotalInstances", + "GroupPendingInstances", + "GroupTerminatingInstances", + "GroupDesiredCapacity", + "GroupInServiceInstances", + "GroupMinSize", + "GroupMaxSize" + ] + metrics_granularity = "1Minute" +} +` + +const testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected = ` +resource "aws_launch_configuration" "foobar" { + image_id = "ami-21f78e11" + instance_type = "t1.micro" +} + +resource "aws_autoscaling_group" "bar" { + availability_zones = ["us-west-2a"] + max_size = 1 + min_size = 0 + health_check_grace_period = 300 + health_check_type = "EC2" + desired_capacity = 0 + force_delete = true + termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] + launch_configuration = "${aws_launch_configuration.foobar.name}" + enabled_metrics = ["GroupTotalInstances", + "GroupPendingInstances", + "GroupTerminatingInstances", + "GroupDesiredCapacity", + "GroupMaxSize" + ] + metrics_granularity = "1Minute" +} +` diff --git a/builtin/providers/aws/resource_aws_autoscaling_metrics_collection.go b/builtin/providers/aws/resource_aws_autoscaling_metrics_collection.go deleted file mode 100644 index 3636eafef..000000000 --- a/builtin/providers/aws/resource_aws_autoscaling_metrics_collection.go +++ /dev/null @@ -1,142 +0,0 @@ -package aws - -import ( - "fmt" - "log" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/autoscaling" - "github.com/hashicorp/terraform/helper/schema" -) - -func resourceAwsAutoscalingMetric() *schema.Resource { - return &schema.Resource{ - Create: resourceAwsAutoscalingMetricCreate, - Read: resourceAwsAutoscalingMetricRead, - Update: resourceAwsAutoscalingMetricUpdate, - Delete: resourceAwsAutoscalingMetricDelete, - - Schema: map[string]*schema.Schema{ - "autoscaling_group_name": &schema.Schema{ - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - - "metrics": &schema.Schema{ - Type: schema.TypeSet, - Required: true, - Elem: &schema.Schema{Type: schema.TypeString}, - Set: schema.HashString, - }, - - "granularity": &schema.Schema{ - Type: schema.TypeString, - Required: true, - }, - }, - } -} - -func resourceAwsAutoscalingMetricCreate(d *schema.ResourceData, meta interface{}) error { - conn := meta.(*AWSClient).autoscalingconn - - asgName := d.Get("autoscaling_group_name").(string) - props := &autoscaling.EnableMetricsCollectionInput{ - AutoScalingGroupName: aws.String(asgName), - Granularity: aws.String(d.Get("granularity").(string)), - Metrics: expandStringList(d.Get("metrics").(*schema.Set).List()), - } - - log.Printf("[INFO] Enabling metrics collection for the ASG: %s", asgName) - _, err := conn.EnableMetricsCollection(props) - if err != nil { - return err - } - - d.SetId(d.Get("autoscaling_group_name").(string)) - - return resourceAwsAutoscalingMetricRead(d, meta) -} - -func resourceAwsAutoscalingMetricRead(d *schema.ResourceData, meta interface{}) error { - conn := meta.(*AWSClient).autoscalingconn - - g, err := getAwsAutoscalingGroup(d.Get("autoscaling_group_name").(string), conn) - if err != nil { - return err - } - if g == nil { - return nil - } - - if g.EnabledMetrics != nil && len(g.EnabledMetrics) > 0 { - if err := d.Set("metrics", flattenAsgEnabledMetrics(g.EnabledMetrics)); err != nil { - log.Printf("[WARN] Error setting metrics for (%s): %s", d.Id(), err) - } - d.Set("granularity", g.EnabledMetrics[0].Granularity) - } - - return nil -} - -func resourceAwsAutoscalingMetricUpdate(d *schema.ResourceData, meta interface{}) error { - conn := meta.(*AWSClient).autoscalingconn - - if d.HasChange("metrics") { - o, n := d.GetChange("metrics") - if o == nil { - o = new(schema.Set) - } - if n == nil { - n = new(schema.Set) - } - - os := o.(*schema.Set) - ns := n.(*schema.Set) - - disableMetrics := os.Difference(ns) - if disableMetrics.Len() != 0 { - props := &autoscaling.DisableMetricsCollectionInput{ - AutoScalingGroupName: aws.String(d.Id()), - Metrics: expandStringList(disableMetrics.List()), - } - - _, err := conn.DisableMetricsCollection(props) - if err != nil { - return fmt.Errorf("Failure to Disable metrics collection types for ASG %s: %s", d.Id(), err) - } - } - - enabledMetrics := ns.Difference(os) - if enabledMetrics.Len() != 0 { - props := &autoscaling.EnableMetricsCollectionInput{ - AutoScalingGroupName: aws.String(d.Id()), - Metrics: expandStringList(enabledMetrics.List()), - } - - _, err := conn.EnableMetricsCollection(props) - if err != nil { - return fmt.Errorf("Failure to Enable metrics collection types for ASG %s: %s", d.Id(), err) - } - } - } - - return resourceAwsAutoscalingMetricRead(d, meta) -} - -func resourceAwsAutoscalingMetricDelete(d *schema.ResourceData, meta interface{}) error { - conn := meta.(*AWSClient).autoscalingconn - - props := &autoscaling.DisableMetricsCollectionInput{ - AutoScalingGroupName: aws.String(d.Id()), - } - - log.Printf("[INFO] Disabling ALL metrics collection for the ASG: %s", d.Id()) - _, err := conn.DisableMetricsCollection(props) - if err != nil { - return err - } - - return nil -} diff --git a/builtin/providers/aws/resource_aws_autoscaling_metrics_collection_test.go b/builtin/providers/aws/resource_aws_autoscaling_metrics_collection_test.go deleted file mode 100644 index a2cb6324d..000000000 --- a/builtin/providers/aws/resource_aws_autoscaling_metrics_collection_test.go +++ /dev/null @@ -1,189 +0,0 @@ -package aws - -import ( - "fmt" - "testing" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" - "github.com/aws/aws-sdk-go/service/autoscaling" - "github.com/hashicorp/terraform/helper/resource" - "github.com/hashicorp/terraform/terraform" -) - -func TestAccAWSAutoscalingMetricsCollection_basic(t *testing.T) { - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSAutoscalingMetricsCollectionDestroy, - Steps: []resource.TestStep{ - resource.TestStep{ - Config: testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected, - Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAutoscalingMetricsCollectionExists("aws_autoscaling_metrics_collection.test"), - resource.TestCheckResourceAttr( - "aws_autoscaling_metrics_collection.test", "metrics.#", "7"), - ), - }, - }, - }) -} - -func TestAccAWSAutoscalingMetricsCollection_update(t *testing.T) { - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSAutoscalingMetricsCollectionDestroy, - Steps: []resource.TestStep{ - resource.TestStep{ - Config: testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected, - Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAutoscalingMetricsCollectionExists("aws_autoscaling_metrics_collection.test"), - resource.TestCheckResourceAttr( - "aws_autoscaling_metrics_collection.test", "metrics.#", "7"), - ), - }, - - resource.TestStep{ - Config: testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected, - Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAutoscalingMetricsCollectionExists("aws_autoscaling_metrics_collection.test"), - resource.TestCheckResourceAttr( - "aws_autoscaling_metrics_collection.test", "metrics.#", "5"), - ), - }, - }, - }) -} -func testAccCheckAWSAutoscalingMetricsCollectionExists(n string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No AutoScaling Group ID is set") - } - - conn := testAccProvider.Meta().(*AWSClient).autoscalingconn - - describeGroups, err := conn.DescribeAutoScalingGroups( - &autoscaling.DescribeAutoScalingGroupsInput{ - AutoScalingGroupNames: []*string{aws.String(rs.Primary.ID)}, - }) - - if err != nil { - return err - } - - if len(describeGroups.AutoScalingGroups) != 1 || - *describeGroups.AutoScalingGroups[0].AutoScalingGroupName != rs.Primary.ID { - return fmt.Errorf("AutoScaling Group not found") - } - - if describeGroups.AutoScalingGroups[0].EnabledMetrics == nil { - return fmt.Errorf("AutoScaling Groups Metrics Collection not found") - } - - return nil - } -} - -func testAccCheckAWSAutoscalingMetricsCollectionDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*AWSClient).autoscalingconn - - for _, rs := range s.RootModule().Resources { - // Try to find the Group - describeGroups, err := conn.DescribeAutoScalingGroups( - &autoscaling.DescribeAutoScalingGroupsInput{ - AutoScalingGroupNames: []*string{aws.String(rs.Primary.ID)}, - }) - - if err == nil { - if len(describeGroups.AutoScalingGroups) != 0 && - *describeGroups.AutoScalingGroups[0].AutoScalingGroupName == rs.Primary.ID { - return fmt.Errorf("AutoScalingGroup still exists") - } - } - - // Verify the error - ec2err, ok := err.(awserr.Error) - if !ok { - return err - } - if ec2err.Code() != "InvalidGroup.NotFound" { - return err - } - } - - return nil -} - -const testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected = ` -resource "aws_launch_configuration" "foobar" { - image_id = "ami-21f78e11" - instance_type = "t1.micro" -} - -resource "aws_autoscaling_group" "bar" { - availability_zones = ["us-west-2a"] - name = "foobar3-terraform-test" - max_size = 1 - min_size = 0 - health_check_grace_period = 300 - health_check_type = "EC2" - desired_capacity = 0 - force_delete = true - termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] - - launch_configuration = "${aws_launch_configuration.foobar.name}" -} - -resource "aws_autoscaling_metrics_collection" "test" { - autoscaling_group_name = "${aws_autoscaling_group.bar.name}" - granularity = "1Minute" - metrics = ["GroupTotalInstances", - "GroupPendingInstances", - "GroupTerminatingInstances", - "GroupDesiredCapacity", - "GroupInServiceInstances", - "GroupMinSize", - "GroupMaxSize" - ] -} -` - -const testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected = ` -resource "aws_launch_configuration" "foobar" { - image_id = "ami-21f78e11" - instance_type = "t1.micro" -} - -resource "aws_autoscaling_group" "bar" { - availability_zones = ["us-west-2a"] - name = "foobar3-terraform-test" - max_size = 1 - min_size = 0 - health_check_grace_period = 300 - health_check_type = "EC2" - desired_capacity = 0 - force_delete = true - termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] - - launch_configuration = "${aws_launch_configuration.foobar.name}" -} - -resource "aws_autoscaling_metrics_collection" "test" { - autoscaling_group_name = "${aws_autoscaling_group.bar.name}" - granularity = "1Minute" - metrics = ["GroupTotalInstances", - "GroupPendingInstances", - "GroupTerminatingInstances", - "GroupDesiredCapacity", - "GroupMaxSize" - ] -} -` diff --git a/website/source/docs/providers/aws/r/autoscaling_group.html.markdown b/website/source/docs/providers/aws/r/autoscaling_group.html.markdown index e71ab0750..cd0c5ccb5 100644 --- a/website/source/docs/providers/aws/r/autoscaling_group.html.markdown +++ b/website/source/docs/providers/aws/r/autoscaling_group.html.markdown @@ -70,6 +70,8 @@ The following arguments are supported: * `termination_policies` (Optional) A list of policies to decide how the instances in the auto scale group should be terminated. * `tag` (Optional) A list of tag blocks. Tags documented below. * `placement_group` (Optional) The name of the placement group into which you'll launch your instances, if any. +* `metrics_granularity` - (Required) The granularity to associate with the metrics to collect. The only valid value is `1Minute`. +* `enabled_metrics` - (Required) A list of metrics to collect. The allowed values are `GroupMinSize`, `GroupMaxSize`, `GroupDesiredCapacity`, `GroupInServiceInstances`, `GroupPendingInstances`, `GroupStandbyInstances`, `GroupTerminatingInstances`, `GroupTotalInstances`. * `wait_for_capacity_timeout` (Default: "10m") A maximum [duration](https://golang.org/pkg/time/#ParseDuration) that Terraform should wait for ASG instances to be healthy before timing out. (See also [Waiting diff --git a/website/source/docs/providers/aws/r/autoscaling_metrics_collection.html.markdown b/website/source/docs/providers/aws/r/autoscaling_metrics_collection.html.markdown deleted file mode 100644 index 607d87c8e..000000000 --- a/website/source/docs/providers/aws/r/autoscaling_metrics_collection.html.markdown +++ /dev/null @@ -1,57 +0,0 @@ ---- -layout: "aws" -page_title: "AWS: aws_autoscaling_metrics_collection" -sidebar_current: "docs-aws-resource-autoscaling-metrics-collection" -description: |- - Enables / Disables Autoscaling Group Metrics Collection. ---- - -# aws\_autoscaling\_metrics\_collection - -Enables / Disables Autoscaling Group Metrics Collection. - -~> **NOTE:** You can only enable metrics collection for an Autoscaling Group if `enable_monitoring` -in it's underlying launch configuration for the group is set to `True`. - -## Example Usage - -``` -resource "aws_launch_configuration" "foobar" { - name = "web_config" - image_id = "ami-408c7f28" - instance_type = "t1.micro" - enable_monitoring = true -} - -resource "aws_autoscaling_group" "foobar" { - availability_zones = ["us-west-2a"] - name = "terraform-test-foobar5" - health_check_type = "EC2" - termination_policies = ["OldestInstance"] - launch_configuration = "${aws_launch_configuration.foobar.name}" - tag { - key = "Foo" - value = "foo-bar" - propagate_at_launch = true - } -} - -resource "aws_autoscaling_metrics_collection" "test" { - autoscaling_group_name = "${aws_autoscaling_group.bar.name}" - granularity = "1Minute" - metrics = ["GroupTotalInstances", - "GroupPendingInstances", - "GroupTerminatingInstances", - "GroupDesiredCapacity", - "GroupMaxSize" - ] -} -``` - -## Argument Reference - -The following arguments are supported: - -* `autoscaling_group_name` - (Required) The name of the Auto Scaling group to which you want to enable / disable metrics collection -* `granularity` - (Required) The granularity to associate with the metrics to collect. The only valid value is `1Minute`. -* `metrics` - (Required) A list of metrics to collect. The allowed values are `GroupMinSize`, `GroupMaxSize`, `GroupDesiredCapacity`, `GroupInServiceInstances`, `GroupPendingInstances`, `GroupStandbyInstances`, `GroupTerminatingInstances`, `GroupTotalInstances`. \ No newline at end of file diff --git a/website/source/layouts/aws.erb b/website/source/layouts/aws.erb index c2e3ad256..9643b18ff 100644 --- a/website/source/layouts/aws.erb +++ b/website/source/layouts/aws.erb @@ -127,10 +127,6 @@ aws_autoscaling_lifecycle_hook - > - aws_autoscaling_metrics_collection - - > aws_autoscaling_notification