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

resource/aws_rds_cluster_instance: Ensure monitoring attributes are always written to state #9748

Merged
merged 1 commit into from
Aug 15, 2019
Merged
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
10 changes: 2 additions & 8 deletions aws/resource_aws_rds_cluster_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ func resourceAwsRDSClusterInstanceRead(d *schema.ResourceData, meta interface{})
d.Set("identifier", db.DBInstanceIdentifier)
d.Set("instance_class", db.DBInstanceClass)
d.Set("kms_key_id", db.KmsKeyId)
d.Set("monitoring_interval", db.MonitoringInterval)
d.Set("monitoring_role_arn", db.MonitoringRoleArn)
d.Set("performance_insights_enabled", db.PerformanceInsightsEnabled)
d.Set("performance_insights_kms_key_id", db.PerformanceInsightsKMSKeyId)
d.Set("preferred_backup_window", db.PreferredBackupWindow)
Expand All @@ -390,14 +392,6 @@ func resourceAwsRDSClusterInstanceRead(d *schema.ResourceData, meta interface{})
d.Set("publicly_accessible", db.PubliclyAccessible)
d.Set("storage_encrypted", db.StorageEncrypted)

if db.MonitoringInterval != nil {
d.Set("monitoring_interval", db.MonitoringInterval)
}

if db.MonitoringRoleArn != nil {
d.Set("monitoring_role_arn", db.MonitoringRoleArn)
}

if len(db.DBParameterGroups) > 0 {
d.Set("db_parameter_group_name", db.DBParameterGroups[0].DBParameterGroupName)
}
Expand Down
318 changes: 235 additions & 83 deletions aws/resource_aws_rds_cluster_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,30 +354,165 @@ func testAccCheckAWSClusterInstanceExists(n string, v *rds.DBInstance) resource.
}
}

func TestAccAWSRDSClusterInstance_withInstanceEnhancedMonitor(t *testing.T) {
var v rds.DBInstance
func TestAccAWSRDSClusterInstance_MonitoringInterval(t *testing.T) {
var dbInstance rds.DBInstance
resourceName := "aws_rds_cluster_instance.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSClusterDestroy,
CheckDestroy: testAccCheckAWSDBInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSClusterInstanceEnhancedMonitor(acctest.RandInt()),
Config: testAccAWSClusterInstanceConfigMonitoringInterval(rName, 30),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.cluster_instances", &v),
testAccCheckAWSDBClusterInstanceAttributes(&v),
testAccCheckAWSClusterInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "30"),
),
},
{
ResourceName: "aws_rds_cluster_instance.cluster_instances",
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"apply_immediately",
"identifier_prefix",
},
},
{
Config: testAccAWSClusterInstanceConfigMonitoringInterval(rName, 60),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "60"),
),
},
{
Config: testAccAWSClusterInstanceConfigMonitoringInterval(rName, 0),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "0"),
),
},
{
Config: testAccAWSClusterInstanceConfigMonitoringInterval(rName, 30),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "30"),
),
},
},
})
}

func TestAccAWSRDSClusterInstance_MonitoringRoleArn_EnabledToDisabled(t *testing.T) {
var dbInstance rds.DBInstance
iamRoleResourceName := "aws_iam_role.test"
resourceName := "aws_rds_cluster_instance.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDBInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSClusterInstanceConfigMonitoringRoleArn(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttrPair(resourceName, "monitoring_role_arn", iamRoleResourceName, "arn"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"apply_immediately",
"identifier_prefix",
},
},
{
Config: testAccAWSClusterInstanceConfigMonitoringInterval(rName, 0),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "monitoring_interval", "0"),
),
},
},
})
}

func TestAccAWSRDSClusterInstance_MonitoringRoleArn_EnabledToRemoved(t *testing.T) {
var dbInstance rds.DBInstance
iamRoleResourceName := "aws_iam_role.test"
resourceName := "aws_rds_cluster_instance.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDBInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSClusterInstanceConfigMonitoringRoleArn(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttrPair(resourceName, "monitoring_role_arn", iamRoleResourceName, "arn"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"apply_immediately",
"identifier_prefix",
},
},
{
Config: testAccAWSClusterInstanceConfigMonitoringRoleArnRemoved(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterInstanceExists(resourceName, &dbInstance),
),
},
},
})
}

func TestAccAWSRDSClusterInstance_MonitoringRoleArn_RemovedToEnabled(t *testing.T) {
var dbInstance rds.DBInstance
iamRoleResourceName := "aws_iam_role.test"
resourceName := "aws_rds_cluster_instance.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDBInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSClusterInstanceConfigMonitoringRoleArnRemoved(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterInstanceExists(resourceName, &dbInstance),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"apply_immediately",
"identifier_prefix",
},
},
{
Config: testAccAWSClusterInstanceConfigMonitoringRoleArn(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttrPair(resourceName, "monitoring_role_arn", iamRoleResourceName, "arn"),
),
},
},
})
}
Expand Down Expand Up @@ -751,103 +886,120 @@ resource "aws_db_parameter_group" "bar" {
`, n, n, n, n)
}

func testAccAWSClusterInstanceEnhancedMonitor(n int) string {
func testAccAWSClusterInstanceConfigMonitoringInterval(rName string, monitoringInterval int) string {
return fmt.Sprintf(`
resource "aws_rds_cluster" "default" {
cluster_identifier = "tf-aurora-cluster-test-%d"
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
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
database_name = "mydb"
master_username = "foo"
master_password = "mustbeeightcharaters"
skip_final_snapshot = true
}

resource "aws_rds_cluster_instance" "cluster_instances" {
identifier = "tf-cluster-instance-%d"
cluster_identifier = "${aws_rds_cluster.default.id}"
instance_class = "db.t2.small"
db_parameter_group_name = "${aws_db_parameter_group.bar.name}"
monitoring_interval = "60"
monitoring_role_arn = "${aws_iam_role.tf_enhanced_monitor_role.arn}"
}
resource "aws_rds_cluster_instance" "test" {
depends_on = ["aws_iam_role_policy_attachment.test"]

resource "aws_iam_role" "tf_enhanced_monitor_role" {
name = "tf_enhanced_monitor_role-%d"
cluster_identifier = "${aws_rds_cluster.test.id}"
identifier = %[1]q
instance_class = "db.t2.small"
monitoring_interval = %[2]d
monitoring_role_arn = "${aws_iam_role.test.arn}"
}
`, rName, monitoringInterval)
}

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "monitoring.rds.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
func testAccAWSClusterInstanceConfigMonitoringRoleArnRemoved(rName string) string {
return fmt.Sprintf(`
resource "aws_rds_cluster" "test" {
cluster_identifier = %[1]q
database_name = "mydb"
master_username = "foo"
master_password = "mustbeeightcharaters"
skip_final_snapshot = true
}

resource "aws_iam_policy_attachment" "rds_m_attach" {
name = "tf-enhanced-monitoring-attachment-%d"
roles = ["${aws_iam_role.tf_enhanced_monitor_role.name}"]
policy_arn = "${aws_iam_policy.test.arn}"
resource "aws_rds_cluster_instance" "test" {
cluster_identifier = "${aws_rds_cluster.test.id}"
identifier = %[1]q
instance_class = "db.t2.small"
}
`, rName)
}

resource "aws_iam_policy" "test" {
name = "tf-enhanced-monitoring-policy-%d"
func testAccAWSClusterInstanceConfigMonitoringRoleArn(rName string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}

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

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EnableCreationAndManagementOfRDSCloudwatchLogGroups",
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:PutRetentionPolicy"
],
"Resource": [
"arn:aws:logs:*:*:log-group:RDS*"
]
},
{
"Sid": "EnableCreationAndManagementOfRDSCloudwatchLogStreams",
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams",
"logs:GetLogEvents"
],
"Resource": [
"arn:aws:logs:*:*:log-group:RDS*:log-stream:*"
]
}
]
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "monitoring.rds.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
POLICY
EOF
}

resource "aws_db_parameter_group" "bar" {
name = "tfcluster-test-group-%d"
family = "aurora5.6"
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}"
}

parameter {
name = "back_log"
value = "32767"
apply_method = "pending-reboot"
}
resource "aws_rds_cluster" "test" {
cluster_identifier = %[1]q
database_name = "mydb"
master_username = "foo"
master_password = "mustbeeightcharaters"
skip_final_snapshot = true
}

tags = {
foo = "bar"
}
resource "aws_rds_cluster_instance" "test" {
depends_on = ["aws_iam_role_policy_attachment.test"]

cluster_identifier = "${aws_rds_cluster.test.id}"
identifier = %[1]q
instance_class = "db.t2.small"
monitoring_interval = 5
monitoring_role_arn = "${aws_iam_role.test.arn}"
}
`, n, n, n, n, n, n)
`, rName)
}

func testAccAWSClusterInstanceConfigPerformanceInsightsEnabledAuroraMysql1(rName string) string {
Expand Down