admin管理员组文章数量:1122846
I am using Terraform to manage some cloudwatch alarms, and when Terraform destroys the alarms I want it to also delete the anomaly detection on the underlying metrics. As far as I can tell, I cannot manage the metrics themselves directly in Terraform (see ).
I am using a local-exec provisioner block and I think my problem is the command I'm trying to run involves some JSON. I was also trying to leave some newlines in the command itself for readability sake. I am really struggling with the way Terraform handles string literals. I've tried EOT, EOF, using a variable but I just can't get it to work.
Here's the command I'm trying to get Terraform to execute (this command works from my terminal)
aws cloudwatch delete-anomaly-detector \
--single-metric-anomaly-detector
'{
"Namespace": "AWS/ApplicationELB",
"AccountId": "redacted",
"MetricName": "RequestCountPerTarget",
"Dimensions": [
{
"Name": "TargetGroup",
"Value": "redacted"
}
],
"Stat": "Average"
}'
Here's my Terraform configuration (truncated for brevity)
resource "aws_cloudwatch_metric_alarm" "example" {
alarm_name = "example"
#....
#redacted for brevity
#....
provisioner "local-exec" {
when = destroy
command = format("aws cloudwatch delete-anomaly-detector --single-metric-anomoly-detector '%s'", jsonencode({
Namespace = "AWS\\ApplicationELB"
AccountId = "redacted"
MetricName = "RequestCountPerTarget"
Dimensions = [{
Name = "TargetGroup"
Value = "redacted"
}]
Stat = "Average"
}
))
}
}
When I run terraform apply
it does execute, but the command isn't executed properly by the interpreter.
module.example.aws_cloudwatch_metric_alarm.example[0] (local-exec): Executing: ["/bin/sh" "-c" "aws cloudwatch delete-anomaly-detector --single-metric-anomoly-detector '{"AccountId":"redacted","Dimensions":[{"Name":"TargetGroup","Value":"redacted"}],"MetricName":"RequestCountPerTarget","Namespace":"AWS\\ApplicationELB","Stat":"Average"}'"]
module.example.aws_cloudwatch_metric_alarm.example[0] (local-exec): usage: aws [options] [ ...] [parameters] (local-exec): usage: aws [options] [ ...] [parameters]
I also created some gists if that's easier to read.
AWS command I'm trying to get Terraform to run
Terraform config (truncated)
I am using Terraform to manage some cloudwatch alarms, and when Terraform destroys the alarms I want it to also delete the anomaly detection on the underlying metrics. As far as I can tell, I cannot manage the metrics themselves directly in Terraform (see https://github.com/hashicorp/terraform-provider-aws/issues/18344).
I am using a local-exec provisioner block and I think my problem is the command I'm trying to run involves some JSON. I was also trying to leave some newlines in the command itself for readability sake. I am really struggling with the way Terraform handles string literals. I've tried EOT, EOF, using a variable but I just can't get it to work.
Here's the command I'm trying to get Terraform to execute (this command works from my terminal)
aws cloudwatch delete-anomaly-detector \
--single-metric-anomaly-detector
'{
"Namespace": "AWS/ApplicationELB",
"AccountId": "redacted",
"MetricName": "RequestCountPerTarget",
"Dimensions": [
{
"Name": "TargetGroup",
"Value": "redacted"
}
],
"Stat": "Average"
}'
Here's my Terraform configuration (truncated for brevity)
resource "aws_cloudwatch_metric_alarm" "example" {
alarm_name = "example"
#....
#redacted for brevity
#....
provisioner "local-exec" {
when = destroy
command = format("aws cloudwatch delete-anomaly-detector --single-metric-anomoly-detector '%s'", jsonencode({
Namespace = "AWS\\ApplicationELB"
AccountId = "redacted"
MetricName = "RequestCountPerTarget"
Dimensions = [{
Name = "TargetGroup"
Value = "redacted"
}]
Stat = "Average"
}
))
}
}
When I run terraform apply
it does execute, but the command isn't executed properly by the interpreter.
module.example.aws_cloudwatch_metric_alarm.example[0] (local-exec): Executing: ["/bin/sh" "-c" "aws cloudwatch delete-anomaly-detector --single-metric-anomoly-detector '{"AccountId":"redacted","Dimensions":[{"Name":"TargetGroup","Value":"redacted"}],"MetricName":"RequestCountPerTarget","Namespace":"AWS\\ApplicationELB","Stat":"Average"}'"]
module.example.aws_cloudwatch_metric_alarm.example[0] (local-exec): usage: aws [options] [ ...] [parameters] (local-exec): usage: aws [options] [ ...] [parameters]
I also created some gists if that's easier to read.
AWS command I'm trying to get Terraform to run
Terraform config (truncated)
Share Improve this question edited Nov 22, 2024 at 19:08 TylerH 21.2k76 gold badges79 silver badges110 bronze badges asked Nov 22, 2024 at 16:47 pandaPowderpandaPowder 2,1152 gold badges13 silver badges7 bronze badges 5 |1 Answer
Reset to default 0<<EOT Block: The <<EOT (a Here Document) is used to manage multi-line strings without worrying about escaping newlines or quotes. This makes the JSON string more readable.
resource "aws_cloudwatch_metric_alarm" "example" {
alarm_name = "example"
#....
#redacted for brevity
#....
provisioner "local-exec" {
when = destroy
command = <<EOT
aws cloudwatch delete-anomaly-detector --single-metric-anomaly-detector '{
"Namespace": "AWS/ApplicationELB",
"AccountId": "redacted",
"MetricName": "RequestCountPerTarget",
"Dimensions": [
{
"Name": "TargetGroup",
"Value": "redacted"
}
],
"Stat": "Average"
}'
EOT
}
}
I also updated Namespace to use AWS/ApplicationELB directly without double backslashes (\), as the double backslashes were likely misinterpreted.
本文标签:
版权声明:本文标题:amazon web services - How can I modify string literals in Terraform when the string contains JSON? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302225a1931457.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Namespace
value relative to your command:"AWS\\ApplicationELB"
instead of"AWS/ApplicationELB"
. Unfortunately questions where the root cause is a typo are considered invalid for SO, so if this solves your problem please let us know. – Matthew Schuchard Commented Nov 22, 2024 at 18:23