diff --git a/.changelog/18083.txt b/.changelog/18083.txt new file mode 100644 index 00000000000..7aff48adf89 --- /dev/null +++ b/.changelog/18083.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +aws_cloudfront_monitoring_subscription +``` diff --git a/aws/resource_aws_cloudfront_monitoring_subscription.go b/aws/resource_aws_cloudfront_monitoring_subscription.go index 71664e4c3a5..8cba547f596 100644 --- a/aws/resource_aws_cloudfront_monitoring_subscription.go +++ b/aws/resource_aws_cloudfront_monitoring_subscription.go @@ -16,9 +16,10 @@ func resourceAwsCloudFrontMonitoringSubscription() *schema.Resource { return &schema.Resource{ Create: resourceAwsCloudFrontMonitoringSubscriptionCreate, Read: resourceAwsCloudFrontMonitoringSubscriptionRead, + Update: resourceAwsCloudFrontMonitoringSubscriptionCreate, Delete: resourceAwsCloudFrontMonitoringSubscriptionDelete, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: resourceAwsCloudFrontMonitoringSubscriptionImport, }, Schema: map[string]*schema.Schema{ @@ -31,7 +32,6 @@ func resourceAwsCloudFrontMonitoringSubscription() *schema.Resource { "monitoring_subscription": { Type: schema.TypeList, Required: true, - ForceNew: true, MinItems: 1, MaxItems: 1, Elem: &schema.Resource{ @@ -39,7 +39,6 @@ func resourceAwsCloudFrontMonitoringSubscription() *schema.Resource { "realtime_metrics_subscription_config": { Type: schema.TypeList, Required: true, - ForceNew: true, MinItems: 1, MaxItems: 1, Elem: &schema.Resource{ @@ -47,7 +46,6 @@ func resourceAwsCloudFrontMonitoringSubscription() *schema.Resource { "realtime_metrics_subscription_status": { Type: schema.TypeString, Required: true, - ForceNew: true, ValidateFunc: validation.StringInSlice(cloudfront.RealtimeMetricsSubscriptionStatus_Values(), false), }, }, @@ -86,7 +84,7 @@ func resourceAwsCloudFrontMonitoringSubscriptionRead(d *schema.ResourceData, met subscription, err := finder.MonitoringSubscriptionByDistributionId(conn, d.Id()) - if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, cloudfront.ErrCodeNoSuchDistribution) { + if tfawserr.ErrCodeEquals(err, cloudfront.ErrCodeNoSuchDistribution) { log.Printf("[WARN] CloudFront Distribution (%s) not found, removing from state", d.Id()) d.SetId("") return nil @@ -105,7 +103,6 @@ func resourceAwsCloudFrontMonitoringSubscriptionRead(d *schema.ResourceData, met return nil } - d.Set("distribution_id", d.Id()) if err := d.Set("monitoring_subscription", flattenCloudFrontMonitoringSubscription(subscription)); err != nil { return fmt.Errorf("error setting monitoring_subscription: %w", err) } @@ -163,3 +160,8 @@ func flattenCloudFrontMonitoringSubscription(subscription *cloudfront.Monitoring func flattenCloudFrontRealtimeMetricsSubscriptionConfig(config *cloudfront.RealtimeMetricsSubscriptionConfig) []interface{} { return []interface{}{map[string]interface{}{"realtime_metrics_subscription_status": config.RealtimeMetricsSubscriptionStatus}} } + +func resourceAwsCloudFrontMonitoringSubscriptionImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { + d.Set("distribution_id", d.Id()) + return []*schema.ResourceData{d}, nil +} diff --git a/aws/resource_aws_cloudfront_monitoring_subscription_test.go b/aws/resource_aws_cloudfront_monitoring_subscription_test.go index 4397e9b7c48..31dbb18e8c8 100644 --- a/aws/resource_aws_cloudfront_monitoring_subscription_test.go +++ b/aws/resource_aws_cloudfront_monitoring_subscription_test.go @@ -78,7 +78,7 @@ func TestAccAWSCloudFrontMonitoringSubscription_basic(t *testing.T) { CheckDestroy: testAccCheckCloudFrontMonitoringSubscriptionDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSCloudFrontMonitoringSubscriptionConfig(), + Config: testAccAWSCloudFrontMonitoringSubscriptionConfig("Enabled"), Check: resource.ComposeTestCheckFunc( testAccCheckCloudFrontMonitoringSubscriptionExists(resourceName, &v), resource.TestCheckResourceAttrSet(resourceName, "distribution_id"), @@ -106,7 +106,7 @@ func TestAccAWSCloudFrontMonitoringSubscription_disappears(t *testing.T) { CheckDestroy: testAccCheckCloudFrontMonitoringSubscriptionDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSCloudFrontMonitoringSubscriptionConfig(), + Config: testAccAWSCloudFrontMonitoringSubscriptionConfig("Enabled"), Check: resource.ComposeTestCheckFunc( testAccCheckCloudFrontMonitoringSubscriptionExists(resourceName, &v), testAccCheckResourceDisappears(testAccProvider, resourceAwsCloudFrontMonitoringSubscription(), resourceName), @@ -117,7 +117,7 @@ func TestAccAWSCloudFrontMonitoringSubscription_disappears(t *testing.T) { }) } -func TestAccAWSCloudFrontMonitoringSubscription_RealtimeMetricsSubscriptionConfig(t *testing.T) { +func TestAccAWSCloudFrontMonitoringSubscription_update(t *testing.T) { var v cloudfront.MonitoringSubscription resourceName := "aws_cloudfront_monitoring_subscription.test" @@ -127,7 +127,7 @@ func TestAccAWSCloudFrontMonitoringSubscription_RealtimeMetricsSubscriptionConfi CheckDestroy: testAccCheckCloudFrontMonitoringSubscriptionDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSCloudFrontMonitoringSubscriptionRealtimeMetricsSubscriptionConfigConfig("Enabled"), + Config: testAccAWSCloudFrontMonitoringSubscriptionConfig("Enabled"), Check: resource.ComposeTestCheckFunc( testAccCheckCloudFrontMonitoringSubscriptionExists(resourceName, &v), resource.TestCheckResourceAttrSet(resourceName, "distribution_id"), @@ -142,7 +142,7 @@ func TestAccAWSCloudFrontMonitoringSubscription_RealtimeMetricsSubscriptionConfi ImportStateVerify: true, }, { - Config: testAccAWSCloudFrontMonitoringSubscriptionRealtimeMetricsSubscriptionConfigConfig("Disabled"), + Config: testAccAWSCloudFrontMonitoringSubscriptionConfig("Disabled"), Check: resource.ComposeTestCheckFunc( testAccCheckCloudFrontMonitoringSubscriptionExists(resourceName, &v), resource.TestCheckResourceAttrSet(resourceName, "distribution_id"), @@ -249,23 +249,7 @@ resource "aws_cloudfront_distribution" "test" { ` } -func testAccAWSCloudFrontMonitoringSubscriptionConfig() string { - return composeConfig( - testAccAWSCloudFrontMonitoringSubscriptionConfigBase(), - ` -resource "aws_cloudfront_monitoring_subscription" "test" { - distribution_id = aws_cloudfront_distribution.test.id - - monitoring_subscription { - realtime_metrics_subscription_config { - realtime_metrics_subscription_status = "Enabled" - } - } -} -`) -} - -func testAccAWSCloudFrontMonitoringSubscriptionRealtimeMetricsSubscriptionConfigConfig(status string) string { +func testAccAWSCloudFrontMonitoringSubscriptionConfig(status string) string { return composeConfig( testAccAWSCloudFrontMonitoringSubscriptionConfigBase(), fmt.Sprintf(` @@ -274,7 +258,7 @@ resource "aws_cloudfront_monitoring_subscription" "test" { monitoring_subscription { realtime_metrics_subscription_config { - realtime_metrics_subscription_status = %q + realtime_metrics_subscription_status = %[1]q } } } diff --git a/website/docs/r/cloudfront_monitoring_subscription.html.markdown b/website/docs/r/cloudfront_monitoring_subscription.html.markdown index 63676258745..53655a9e6bc 100644 --- a/website/docs/r/cloudfront_monitoring_subscription.html.markdown +++ b/website/docs/r/cloudfront_monitoring_subscription.html.markdown @@ -31,13 +31,13 @@ The following arguments are supported: * `distribution_id` - (Required) The ID of the distribution that you are enabling metrics for. * `monitoring_subscription` - (Required) A monitoring subscription. This structure contains information about whether additional CloudWatch metrics are enabled for a given CloudFront distribution. -The `monitoring_subscription` object supports the following argument: +### monitoring_subscription -* `realtime_metrics_subscription_config` - (Required) A subscription configuration for additional CloudWatch metrics. +* `realtime_metrics_subscription_config` - (Required) A subscription configuration for additional CloudWatch metrics. See below. -The `realtime_metrics_subscription_config` object supports the following argument: +### realtime_metrics_subscription_config -* `realtime_metrics_subscription_status` - (Required) A flag that indicates whether additional CloudWatch metrics are enabled for a given CloudFront distribution. Valid values are `Enabled` and `Disabled`. +* `realtime_metrics_subscription_status` - (Required) A flag that indicates whether additional CloudWatch metrics are enabled for a given CloudFront distribution. Valid values are `Enabled` and `Disabled`. See below. ## Attributes Reference