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

Cloudwatch cross account #20541

Merged
merged 7 commits into from
Aug 12, 2021
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
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