Skip to content

Commit

Permalink
Merge pull request #20541 from nahratzah/cloudwatch-cross-account
Browse files Browse the repository at this point in the history
Cloudwatch cross account
  • Loading branch information
ewbankkit authored Aug 12, 2021
2 parents d732d60 + ae60714 commit eab99cf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/20541.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_cloudwatch_metric_alarm: Add support for `account_id`
```
9 changes: 9 additions & 0 deletions aws/resource_aws_cloudwatch_metric_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func resourceAwsCloudWatchMetricAlarm() *schema.Resource {
Required: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},
"account_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},
"expression": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -506,6 +511,7 @@ func flattenAwsCloudWatchMetricAlarmMetrics(metrics []*cloudwatch.MetricDataQuer
metricQueries := make([]map[string]interface{}, 0)
for _, mq := range metrics {
metricQuery := map[string]interface{}{
"account_id": aws.StringValue(mq.AccountId),
"expression": aws.StringValue(mq.Expression),
"id": aws.StringValue(mq.Id),
"label": aws.StringValue(mq.Label),
Expand Down Expand Up @@ -559,6 +565,9 @@ func expandCloudWatchMetricAlarmMetrics(v *schema.Set) []*cloudwatch.MetricDataQ
if v := metricQueryResource["metric"]; v != nil && len(v.([]interface{})) > 0 {
metricQuery.MetricStat = expandCloudWatchMetricAlarmMetricsMetric(v.([]interface{}))
}
if v, ok := metricQueryResource["account_id"]; ok && v.(string) != "" {
metricQuery.AccountId = aws.String(v.(string))
}
metrics = append(metrics, &metricQuery)
}
return metrics
Expand Down
40 changes: 40 additions & 0 deletions aws/resource_aws_cloudwatch_metric_alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@ func TestAccAWSCloudWatchMetricAlarm_expression(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "metric_query.#", "2"),
),
},
{
Config: testAccAWSCloudWatchMetricAlarmConfigWithCrossAccountMetric(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchMetricAlarmExists(resourceName, &alarm),
resource.TestCheckResourceAttrPair(resourceName, "metric_query.0.account_id", "data.aws_caller_identity.current", "account_id"),
),
},
{
Config: testAccAWSCloudWatchMetricAlarmConfigWithExpressionUpdated(rName),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -649,6 +656,39 @@ resource "aws_cloudwatch_metric_alarm" "test" {
`, rName)
}

func testAccAWSCloudWatchMetricAlarmConfigWithCrossAccountMetric(rName string) string {
return fmt.Sprintf(`
data "aws_caller_identity" "current" {}
resource "aws_cloudwatch_metric_alarm" "test" {
alarm_name = "%s"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
threshold = "80"
alarm_description = "This metric monitors ec2 cpu utilization"
insufficient_data_actions = []
metric_query {
id = "m1"
account_id = data.aws_caller_identity.current.account_id
return_data = "true"
metric {
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
stat = "Average"
unit = "Count"
dimensions = {
InstanceId = "i-abc123"
}
}
}
}
`, rName)
}

func testAccAWSCloudWatchMetricAlarmConfigWithAnomalyDetectionExpression(rName string) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_metric_alarm" "test" {
Expand Down

0 comments on commit eab99cf

Please sign in to comment.