-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
feat: Allow setting id parameter in notification object #236
Conversation
This example is correct because it is a map with keys:
The change you are proposing is correct but it should not be necessary if you provide input as |
@antonbabenko maybe I am missing something, but when using pure terraform and setting the topic blocks, I have control of the order of the notifications: resource "aws_s3_bucket_notification" "bucket_notification" {
bucket = "example-bucket"
topic {
topic_arn = "arn:aws:sns:eu-west-1:123456789012:ios-log-upload"
events = ["s3:ObjectCreated:*"]
filter_prefix = "ios/"
id = "ios"
}
topic {
topic_arn = "arn:aws:sns:eu-west-1:123456789012:android-log-upload"
events = ["s3:ObjectCreated:*"]
filter_prefix = "android/"
id = "android"
}
} In the plan, you can see that the id # aws_s3_bucket_notification.bucket_notification will be created
+ resource "aws_s3_bucket_notification" "bucket_notification" {
+ bucket = "example-bucket"
+ eventbridge = false
+ id = (known after apply)
+ topic {
+ events = [
+ "s3:ObjectCreated:*",
]
+ filter_prefix = "ios/"
+ id = "ios"
+ topic_arn = "arn:aws:sns:eu-west-1:123456789012:ios-log-upload"
}
+ topic {
+ events = [
+ "s3:ObjectCreated:*",
]
+ filter_prefix = "android/"
+ id = "android"
+ topic_arn = "arn:aws:sns:eu-west-1:123456789012:android-log-upload"
}
}
Plan: 1 to add, 0 to change, 0 to destroy. When configuring the same order using the module, the order of the notifications is changing, and module "notification" {
source = "github.com/terraform-aws-modules/terraform-aws-s3-bucket//modules/notification?ref=v3.11.0"
eventbridge = true
create_sqs_policy = false
create_sns_policy = false
sns_notifications = {
ios = {
topic_arn = "arn:aws:sns:eu-west-1:123456789012:ios-log-upload"
events = ["s3:ObjectCreated:*"]
filter_prefix = "ios/"
},
android = {
topic_arn = "arn:aws:sns:eu-west-1:123456789012:android-log-upload"
events = ["s3:ObjectCreated:*"]
filter_prefix = "android"
}
}
} Terraform will perform the following actions:
# module.notification.aws_s3_bucket_notification.this[0] will be created
+ resource "aws_s3_bucket_notification" "this" {
+ eventbridge = true
+ id = (known after apply)
+ topic {
+ events = [
+ "s3:ObjectCreated:*",
]
+ filter_prefix = "android"
+ id = "android"
+ topic_arn = "arn:aws:sns:eu-west-1:123456789012:android-log-upload"
}
+ topic {
+ events = [
+ "s3:ObjectCreated:*",
]
+ filter_prefix = "ios/"
+ id = "ios"
+ topic_arn = "arn:aws:sns:eu-west-1:123456789012:ios-log-upload"
}
}
Plan: 1 to add, 0 to change, 0 to destroy. So when using the module with map type, the order is not preserved and the module passes the map keys down to the resource in alphabetical order and not in the order they were configured: module "notification" {
source = "github.com/terraform-aws-modules/terraform-aws-s3-bucket//modules/notification?ref=v3.11.0"
eventbridge = true
create_sqs_policy = false
create_sns_policy = false
sns_notifications = {
c = {
topic_arn = "arn:aws:sns:eu-west-1:123456789012:c"
events = ["s3:ObjectCreated:*"]
filter_prefix = "c"
}
a = {
topic_arn = "arn:aws:sns:eu-west-1:123456789012:a"
events = ["s3:ObjectCreated:*"]
filter_prefix = "a"
}
b = {
topic_arn = "arn:aws:sns:eu-west-1:123456789012:b"
events = ["s3:ObjectCreated:*"]
filter_prefix = "b"
}
}
} Terraform will perform the following actions:
# module.notification.aws_s3_bucket_notification.this[0] will be created
+ resource "aws_s3_bucket_notification" "this" {
+ eventbridge = true
+ id = (known after apply)
+ topic {
+ events = [
+ "s3:ObjectCreated:*",
]
+ filter_prefix = "a"
+ id = "a"
+ topic_arn = "arn:aws:sns:eu-west-1:123456789012:a"
}
+ topic {
+ events = [
+ "s3:ObjectCreated:*",
]
+ filter_prefix = "b"
+ id = "b"
+ topic_arn = "arn:aws:sns:eu-west-1:123456789012:b"
}
+ topic {
+ events = [
+ "s3:ObjectCreated:*",
]
+ filter_prefix = "c"
+ id = "c"
+ topic_arn = "arn:aws:sns:eu-west-1:123456789012:c"
}
}
Plan: 1 to add, 0 to change, 0 to destroy. This is makes sense when creating resources, but when importing notifications that are not in alphabetical order, terraform forces a change of the resource. |
Thank you very much for the explanation. This seems like a bug in Terraform AWS provider (please open an issue there), but I will also merge your PR since it fixes the problem you're experiencing! |
## [3.12.0](v3.11.0...v3.12.0) (2023-06-08) ### Features * Allow setting id parameter in notification object ([#236](#236)) ([f9067dc](f9067dc))
This PR is included in version 3.12.0 🎉 |
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Description
Allow setting the id parameter inside the notification block.
Motivation and Context
When configuring bucket notifications using map type inside another map, like in the example here, there is no control on the order of the notifications.
This can cause terraform to change the resource and replace the notifications order after importing bucket notification, because the state contains list of sorted notifications and the map order can be different.
This made me configure the notifications in a list rather then map type, but then I realized that the id parameter is not configurable, and will be the key (index) of the object in the list.
The PR will make it possible to configure the id inside each notification block and will allow use of list type over map when you need to control the order of the notifications.
Breaking Changes
No breaking changes.
How Has This Been Tested?
examples/*
to demonstrate and validate my change(s)examples/*
projectspre-commit run -a
on my pull request