Skip to content

Commit 45311f7

Browse files
authored
feat: Added support for custom role_arn in targets (#42)
1 parent 4ad24ec commit 45311f7

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

examples/with-api-destination/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Note that this example may create resources which cost money. Run `terraform des
2727

2828
| Name | Version |
2929
|------|---------|
30+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.44 |
3031
| <a name="provider_random"></a> [random](#provider\_random) | >= 3.0 |
3132

3233
## Modules
@@ -39,7 +40,9 @@ Note that this example may create resources which cost money. Run `terraform des
3940

4041
| Name | Type |
4142
|------|------|
43+
| [aws_iam_role.eventbridge](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
4244
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
45+
| [aws_iam_policy_document.assume_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
4346

4447
## Inputs
4548

examples/with-api-destination/main.tf

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module "eventbridge" {
3333
{
3434
name = "send-orders-to-requestbin"
3535
destination = "requestbin"
36-
attach_role_arn = true
36+
attach_role_arn = aws_iam_role.eventbridge.arn
3737
},
3838
{
3939
name = "send-orders-to-github"
@@ -164,3 +164,20 @@ module "eventbridge" {
164164
resource "random_pet" "this" {
165165
length = 2
166166
}
167+
168+
resource "aws_iam_role" "eventbridge" {
169+
name = "${random_pet.this.id}-role"
170+
assume_role_policy = data.aws_iam_policy_document.assume_role.json
171+
}
172+
173+
data "aws_iam_policy_document" "assume_role" {
174+
statement {
175+
effect = "Allow"
176+
actions = ["sts:AssumeRole"]
177+
178+
principals {
179+
type = "Service"
180+
identifiers = ["events.amazonaws.com"]
181+
}
182+
}
183+
}

main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ resource "aws_cloudwatch_event_target" "this" {
6565
rule = each.value.Name
6666
arn = lookup(each.value, "destination", null) != null ? aws_cloudwatch_event_api_destination.this[each.value.destination].arn : each.value.arn
6767

68-
role_arn = lookup(each.value, "attach_role_arn", null) != null ? try(aws_iam_role.eventbridge[0].arn, "") : null
68+
role_arn = can(length(each.value.attach_role_arn) > 0) ? each.value.attach_role_arn : (try(each.value.attach_role_arn, null) == true ? aws_iam_role.eventbridge[0].arn : null)
69+
6970
target_id = lookup(each.value, "target_id", null)
7071
input = lookup(each.value, "input", null)
7172
input_path = lookup(each.value, "input_path", null)

0 commit comments

Comments
 (0)