Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

f/aws_rds_cluster-add-enhanced-monitoring #41002

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/41002.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_rds_cluster: Add `monitoring_interval` and `monitoring_role_arn` arguments for Enhanced Monitoring for RDS Cluster
```
28 changes: 28 additions & 0 deletions internal/service/rds/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ func resourceCluster() *schema.Resource {
Optional: true,
ForceNew: true,
},
"monitoring_interval": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
},
"monitoring_role_arn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"network_type": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -1180,6 +1190,14 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, meta int
input.MasterUsername = aws.String(v.(string))
}

if v, ok := d.GetOk("monitoring_interval"); ok {
input.MonitoringInterval = aws.Int32(int32(v.(int)))
}

if v, ok := d.GetOk("monitoring_role_arn"); ok {
input.MonitoringRoleArn = aws.String(v.(string))
}

if v, ok := d.GetOk("network_type"); ok {
input.NetworkType = aws.String(v.(string))
}
Expand Down Expand Up @@ -1363,6 +1381,8 @@ func resourceClusterRead(ctx context.Context, d *schema.ResourceData, meta inter
d.Set("master_user_secret", nil)
}
d.Set("master_username", dbc.MasterUsername)
d.Set("monitoring_interval", dbc.MonitoringInterval)
d.Set("monitoring_role_arn", dbc.MonitoringRoleArn)
d.Set("network_type", dbc.NetworkType)
d.Set("performance_insights_enabled", dbc.PerformanceInsightsEnabled)
d.Set("performance_insights_kms_key_id", dbc.PerformanceInsightsKMSKeyId)
Expand Down Expand Up @@ -1568,6 +1588,14 @@ func resourceClusterUpdate(ctx context.Context, d *schema.ResourceData, meta int
}
}

if d.HasChange("monitoring_interval") {
input.MonitoringInterval = aws.Int32(int32(d.Get("monitoring_interval").(int)))
}

if d.HasChange("monitoring_role_arn") {
input.MonitoringRoleArn = aws.String(d.Get("monitoring_role_arn").(string))
}

if d.HasChange("network_type") {
input.NetworkType = aws.String(d.Get("network_type").(string))
}
Expand Down
196 changes: 196 additions & 0 deletions internal/service/rds/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3099,6 +3099,114 @@ func TestAccRDSCluster_performanceInsightsRetentionPeriod(t *testing.T) {
})
}

func TestAccRDSCluster_enhancedMonitoring_enabledToUpdatedMonitoringInterval(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

var dbCluster types.DBCluster
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
iamRoleResourceName := "aws_iam_role.test"
resourceName := "aws_rds_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.RDSServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckClusterDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccClusterConfig_enhancedMonitoring(rName, 30),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &dbCluster),
resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "30"),
resource.TestCheckResourceAttrPair(resourceName, "monitoring_role_arn", iamRoleResourceName, names.AttrARN),
),
},
{
Config: testAccClusterConfig_enhancedMonitoring(rName, 10),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &dbCluster),
resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "10"),
),
},
},
})
}

func TestAccRDSCluster_enhancedMonitoring_enabledToDisabled(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

var dbCluster types.DBCluster
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
iamRoleResourceName := "aws_iam_role.test"
resourceName := "aws_rds_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.RDSServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckClusterDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccClusterConfig_enhancedMonitoring(rName, 30),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &dbCluster),
resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "30"),
resource.TestCheckResourceAttrPair(resourceName, "monitoring_role_arn", iamRoleResourceName, names.AttrARN),
),
},
{
Config: testAccClusterConfig_enhancedMonitoring_disabled(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &dbCluster),
resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "0"),
),
},
},
})
}

func TestAccRDSCluster_enhancedMonitoring_disabledToEnabled(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

var dbCluster types.DBCluster
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
iamRoleResourceName := "aws_iam_role.test"
resourceName := "aws_rds_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.RDSServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckClusterDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccClusterConfig_enhancedMonitoring_disabled(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &dbCluster),
resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "0"),
),
},
{
Config: testAccClusterConfig_enhancedMonitoring(rName, 30),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &dbCluster),
resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "30"),
resource.TestCheckResourceAttrPair(resourceName, "monitoring_role_arn", iamRoleResourceName, names.AttrARN),
),
},
},
})
}

func testAccCheckClusterDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
return testAccCheckClusterDestroyWithProvider(ctx)(s, acctest.Provider)
Expand Down Expand Up @@ -6163,3 +6271,91 @@ resource "aws_rds_cluster" "test" {
}
`, rName, tfrds.ClusterEngineMySQL)
}

func testAccClusterConfig_enhancedMonitoring(rName string, monitoringInterval int) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}

resource "aws_iam_role" "test" {
name = %[1]q

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "monitoring.rds.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}

resource "aws_iam_role_policy_attachment" "test" {
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole"
role = aws_iam_role.test.name
}

resource "aws_rds_cluster" "test" {
cluster_identifier = %[1]q
engine = %[3]q
db_cluster_instance_class = "db.m6gd.large"
storage_type = "io1"
allocated_storage = 100
iops = 1000
master_username = "tfacctest"
master_password = "avoid-plaintext-passwords"
skip_final_snapshot = true
monitoring_interval = %[2]d
monitoring_role_arn = aws_iam_role.test.arn
}
`, rName, monitoringInterval, tfrds.ClusterEngineMySQL)
}

func testAccClusterConfig_enhancedMonitoring_disabled(rName string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}

resource "aws_iam_role" "test" {
name = %[1]q

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "monitoring.rds.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}

resource "aws_iam_role_policy_attachment" "test" {
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole"
role = aws_iam_role.test.name
}

resource "aws_rds_cluster" "test" {
cluster_identifier = %[1]q
engine = %[2]q
db_cluster_instance_class = "db.m6gd.large"
storage_type = "io1"
allocated_storage = 100
iops = 1000
master_username = "tfacctest"
master_password = "avoid-plaintext-passwords"
skip_final_snapshot = true
}
`, rName, tfrds.ClusterEngineMySQL)
}
2 changes: 2 additions & 0 deletions website/docs/r/rds_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ This resource supports the following arguments:
* `master_password` - (Required unless `manage_master_user_password` is set to true or unless a `snapshot_identifier` or `replication_source_identifier` is provided or unless a `global_cluster_identifier` is provided when the cluster is the "secondary" cluster of a global database) Password for the master DB user. Note that this may show up in logs, and it will be stored in the state file. Please refer to the [RDS Naming Constraints][5]. Cannot be set if `manage_master_user_password` is set to `true`.
* `master_user_secret_kms_key_id` - (Optional) Amazon Web Services KMS key identifier is the key ARN, key ID, alias ARN, or alias name for the KMS key. To use a KMS key in a different Amazon Web Services account, specify the key ARN or alias ARN. If not specified, the default KMS key for your Amazon Web Services account is used.
* `master_username` - (Required unless a `snapshot_identifier` or `replication_source_identifier` is provided or unless a `global_cluster_identifier` is provided when the cluster is the "secondary" cluster of a global database) Username for the master DB user. Please refer to the [RDS Naming Constraints][5]. This argument does not support in-place updates and cannot be changed during a restore from snapshot.
* `monitoring_interval` - (Optional) Interval, in seconds, in seconds, between points when Enhanced Monitoring metrics are collected for the DB cluster. To turn off collecting Enhanced Monitoring metrics, specify 0. The default is 0. Valid Values: 0, 1, 5, 10, 15, 30, 60.
* `monitoring_role_arn` - (Optional) ARN for the IAM role that permits RDS to send enhanced monitoring metrics to CloudWatch Logs. You can find more information on the [AWS Documentation](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_Monitoring.html#USER_Monitoring.OS.IAMRole.html) what IAM permissions are needed to allow Enhanced Monitoring for RDS Clusters.
* `network_type` - (Optional) Network type of the cluster. Valid values: `IPV4`, `DUAL`.
* `performance_insights_enabled` - (Optional) Enables Performance Insights.
* `performance_insights_kms_key_id` - (Optional) Specifies the KMS Key ID to encrypt Performance Insights data. If not specified, the default RDS KMS key will be used (`aws/rds`).
Expand Down
Loading