Skip to content

Commit 32f75c1

Browse files
authored
feat: Some refactoring and added ability to handle default bus (#5)
1 parent 53b6f46 commit 32f75c1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+417
-921
lines changed

.github/workflows/pre-commit.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
name: Pre-Commit
32

43
on:
@@ -59,6 +58,7 @@ jobs:
5958
run:
6059
pre-commit run terraform_validate --color=always --show-diff-on-failure --files $(ls *.tf)
6160

61+
6262
# Max Terraform version
6363
getBaseVersion:
6464
name: Module max TF version
@@ -94,7 +94,7 @@ jobs:
9494
- name: Install pre-commit dependencies
9595
run: |
9696
pip install pre-commit
97-
curl -L "$(curl -s https://api.github.com/repos/terraform-docs/terraform-docs/releases/latest | grep -o -E "https://.+?-linux-amd64" | head -n1)" > terraform-docs && chmod +x terraform-docs && sudo mv terraform-docs /usr/bin/
97+
curl -L "$(curl -s https://api.github.com/repos/terraform-docs/terraform-docs/releases/latest | grep -o -E "https://.+?-v0.12\..+?-linux-amd64" | head -n1)" > terraform-docs && chmod +x terraform-docs && sudo mv terraform-docs /usr/bin/
9898
curl -L "$(curl -s https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" > tflint.zip && unzip tflint.zip && rm tflint.zip && sudo mv tflint /usr/bin/
9999
- name: Execute pre-commit
100100
# Run all pre-commit checks on max version supported

.pre-commit-config.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
repos:
32
- repo: git://github.com/antonbabenko/pre-commit-terraform
43
rev: v1.48.0

README.md

+72-41
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,67 @@ Terraform module to create EventBridge resources.
44

55
The following resources are currently supported:
66

7-
* [Cloudwatch Event Archive](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_archive)
8-
* [Cloudwatch Event Bus](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_bus)
9-
* [Cloudwatch Event Permission](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_permission)
10-
* [Cloudwatch Event Rule](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_rule)
11-
* [Cloudwatch Event Target](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_target)
7+
* [EventBridge Archive](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_archive)
8+
* [EventBridge Bus](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_bus)
9+
* [EventBridge Permission](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_permission)
10+
* [EventBridge Rule](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_rule)
11+
* [EventBridge Target](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_target)
1212

1313
## Features
1414

15-
- [x] Creates AWS EventBridge Resources
15+
- [x] Creates AWS EventBridge Resources (bus, rules, targets, permissions)
16+
- [x] Attach resources to an existing EventBridge bus
1617
- [x] Support AWS EventBridge Archives and Replays
1718
- [x] Conditional creation for many types of resources
1819
- [x] Support IAM policy attachments and various ways to create and attach additional policies
1920
- [ ] Support monitoring usage with Cloudwatch Metrics
2021

2122
## Usage
2223

24+
### EventBridge Complete
25+
26+
Most common use-case which creates custom bus, rules and targets.
27+
28+
```hcl
29+
module "eventbridge" {
30+
source = "terraform-aws-modules/eventbridge/aws"
31+
32+
bus_name = "my-bus"
33+
34+
rules = {
35+
orders = {
36+
description = "Capture all order data"
37+
event_pattern = jsonencode({ "source" : ["myapp.orders"] })
38+
enabled = true
39+
}
40+
}
41+
42+
targets = {
43+
orders = [
44+
{
45+
name = "send-orders-to-sqs"
46+
arn = aws_sqs_queue.queue.arn
47+
dead_letter_arn = aws_sqs_queue.dlq.arn
48+
},
49+
{
50+
name = "send-orders-to-kinesis"
51+
arn = aws_kinesis_stream.this.arn
52+
dead_letter_arn = aws_sqs_queue.dlq.arn
53+
input_transformer = local.kinesis_input_transformer
54+
},
55+
{
56+
name = "log-orders-to-cloudwatch"
57+
arn = aws_cloudwatch_log_group.this.arn
58+
}
59+
]
60+
}
61+
62+
tags = {
63+
Name = "my-bus"
64+
}
65+
}
66+
```
67+
2368
### EventBridge Bus
2469

2570
```hcl
@@ -50,10 +95,6 @@ module "eventbridge" {
5095
event_pattern = jsonencode({ "source" : ["my.app.logs"] })
5196
}
5297
}
53-
54-
tags = {
55-
Name = "my-bus"
56-
}
5798
}
5899
```
59100

@@ -84,10 +125,6 @@ module "eventbridge" {
84125
}
85126
]
86127
}
87-
88-
tags = {
89-
Name = "my-bus"
90-
}
91128
}
92129
```
93130

@@ -101,9 +138,8 @@ module "eventbridge_with_archive" {
101138
102139
create_archives = true
103140
104-
archive_config = [
105-
{
106-
name = "my-bus-launch-archive",
141+
archives = {
142+
"my-bus-launch-archive" = {
107143
description = "EC2 AutoScaling Event archive",
108144
retention_days = 1
109145
event_pattern = <<PATTERN
@@ -113,7 +149,7 @@ module "eventbridge_with_archive" {
113149
}
114150
PATTERN
115151
}
116-
]
152+
}
117153
118154
tags = {
119155
Name = "my-bus"
@@ -131,12 +167,11 @@ module "eventbridge_with_permissions" {
131167
132168
create_permissions = true
133169
134-
permission_config = [
135-
{
136-
account_id = "YOUR_ACCOUNT_ID",
137-
statement_id = "development_account"
138-
}
139-
]
170+
permissions = {
171+
"099720109477 DevAccess" = {}
172+
"099720109466 ProdAccess" = {}
173+
}
174+
140175
141176
tags = {
142177
Name = "my-bus"
@@ -173,13 +208,13 @@ module "eventbridge" {
173208
create_permissions = false # to control creation of EventBridge Permissions
174209
create_role = false # to control creation of the IAM role and policies required for EventBridge
175210
211+
attach_cloudwatch_policy = false
212+
attach_ecs_policy = false
176213
attach_kinesis_policy = false
177214
attach_kinesis_firehose_policy = false
178-
attach_sqs_policy = false
179-
attach_ecs_policy = false
180215
attach_lambda_policy = false
181216
attach_sfn_policy = false
182-
attach_cloudwatch_policy = false
217+
attach_sqs_policy = false
183218
attach_tracing_policy = false
184219
185220
# ... omitted
@@ -188,23 +223,19 @@ module "eventbridge" {
188223

189224
## Examples
190225

191-
* [Complete](/examples/complete)
192-
* [Simple](/examples/simple)
193-
* [Archive](/examples/with-archive)
194-
* [Permissions](/examples/with-permissions)
195-
* [SQS Target](/examples/sqs-target)
196-
* [API-Gateway](/examples/api-gateway-event-source)
197-
* [Input Transformation](/examples/transform-input)
198-
* [Step Function Target](/examples/step-function-target)
226+
* [Complete](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/complete) - Creates EventBridge resources (bus, rules and targets) and connect with SQS queues, Kinesis Stream, Step Function, CloudWatch Logs, and more.
227+
* [HTTP API Gateway](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/api-gateway-event-source) - Creates an integration with HTTP API Gateway as event source.
228+
* [Using Default Bus](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/default-bus) - Creates resources in the `default` bus.
229+
* [Archive](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-archive) - EventBridge Archives resources in various configurations.
230+
* [Permissions](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-permissions) - Controls permissions to EventBridge.
199231

200-
## Change log
201232

202233
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
203234
## Requirements
204235

205236
| Name | Version |
206237
|------|---------|
207-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.12.26 |
238+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
208239
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.19 |
209240

210241
## Providers
@@ -266,7 +297,7 @@ No modules.
266297

267298
| Name | Description | Type | Default | Required |
268299
|------|-------------|------|---------|:--------:|
269-
| <a name="input_archive_config"></a> [archive\_config](#input\_archive\_config) | A list of objects with the EventBridge Archive definitions. | `list(any)` | `[]` | no |
300+
| <a name="input_archives"></a> [archives](#input\_archives) | A map of objects with the EventBridge Archive definitions. | `map(any)` | `{}` | no |
270301
| <a name="input_attach_cloudwatch_policy"></a> [attach\_cloudwatch\_policy](#input\_attach\_cloudwatch\_policy) | Controls whether the Cloudwatch policy should be added to IAM role for EventBridge Target | `bool` | `false` | no |
271302
| <a name="input_attach_ecs_policy"></a> [attach\_ecs\_policy](#input\_attach\_ecs\_policy) | Controls whether the ECS policy should be added to IAM role for EventBridge Target | `bool` | `false` | no |
272303
| <a name="input_attach_kinesis_firehose_policy"></a> [attach\_kinesis\_firehose\_policy](#input\_attach\_kinesis\_firehose\_policy) | Controls whether the Kinesis Firehose policy should be added to IAM role for EventBridge Target | `bool` | `false` | no |
@@ -280,7 +311,7 @@ No modules.
280311
| <a name="input_attach_sfn_policy"></a> [attach\_sfn\_policy](#input\_attach\_sfn\_policy) | Controls whether the StepFunction policy should be added to IAM role for EventBridge Target | `bool` | `false` | no |
281312
| <a name="input_attach_sqs_policy"></a> [attach\_sqs\_policy](#input\_attach\_sqs\_policy) | Controls whether the SQS policy should be added to IAM role for EventBridge Target | `bool` | `false` | no |
282313
| <a name="input_attach_tracing_policy"></a> [attach\_tracing\_policy](#input\_attach\_tracing\_policy) | Controls whether X-Ray tracing policy should be added to IAM role for EventBridge | `bool` | `false` | no |
283-
| <a name="input_bus_name"></a> [bus\_name](#input\_bus\_name) | A unique name for your EventBridge Bus | `string` | `""` | no |
314+
| <a name="input_bus_name"></a> [bus\_name](#input\_bus\_name) | A unique name for your EventBridge Bus | `string` | `"default"` | no |
284315
| <a name="input_cloudwatch_target_arns"></a> [cloudwatch\_target\_arns](#input\_cloudwatch\_target\_arns) | The Amazon Resource Name (ARN) of the Cloudwatch Log Streams you want to use as EventBridge targets | `list(string)` | `[]` | no |
285316
| <a name="input_create"></a> [create](#input\_create) | Controls whether resources should be created | `bool` | `true` | no |
286317
| <a name="input_create_archives"></a> [create\_archives](#input\_create\_archives) | Controls whether EventBridge Archive resources should be created | `bool` | `false` | no |
@@ -295,7 +326,7 @@ No modules.
295326
| <a name="input_lambda_target_arns"></a> [lambda\_target\_arns](#input\_lambda\_target\_arns) | The Amazon Resource Name (ARN) of the Lambda Functions you want to use as EventBridge targets | `list(string)` | `[]` | no |
296327
| <a name="input_number_of_policies"></a> [number\_of\_policies](#input\_number\_of\_policies) | Number of policies to attach to IAM role | `number` | `0` | no |
297328
| <a name="input_number_of_policy_jsons"></a> [number\_of\_policy\_jsons](#input\_number\_of\_policy\_jsons) | Number of policies JSON to attach to IAM role | `number` | `0` | no |
298-
| <a name="input_permission_config"></a> [permission\_config](#input\_permission\_config) | A list of objects with EventBridge Permission definitions. | `list(any)` | `[]` | no |
329+
| <a name="input_permissions"></a> [permissions](#input\_permissions) | A map of objects with EventBridge Permission definitions. | `map(any)` | `{}` | no |
299330
| <a name="input_policies"></a> [policies](#input\_policies) | List of policy statements ARN to attach to IAM role | `list(string)` | `[]` | no |
300331
| <a name="input_policy"></a> [policy](#input\_policy) | An additional policy document ARN to attach to IAM role | `string` | `null` | no |
301332
| <a name="input_policy_json"></a> [policy\_json](#input\_policy\_json) | An additional policy document as JSON to attach to IAM role | `string` | `null` | no |
@@ -311,7 +342,7 @@ No modules.
311342
| <a name="input_sfn_target_arns"></a> [sfn\_target\_arns](#input\_sfn\_target\_arns) | The Amazon Resource Name (ARN) of the StepFunctions you want to use as EventBridge targets | `list(string)` | `[]` | no |
312343
| <a name="input_sqs_target_arns"></a> [sqs\_target\_arns](#input\_sqs\_target\_arns) | The Amazon Resource Name (ARN) of the AWS SQS Queues you want to use as EventBridge targets | `list(string)` | `[]` | no |
313344
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to assign to resources. | `map(string)` | `{}` | no |
314-
| <a name="input_targets"></a> [targets](#input\_targets) | A Map of objects with EventBridge Target definitions. | `any` | `{}` | no |
345+
| <a name="input_targets"></a> [targets](#input\_targets) | A map of objects with EventBridge Target definitions. | `any` | `{}` | no |
315346
| <a name="input_trusted_entities"></a> [trusted\_entities](#input\_trusted\_entities) | Step Function additional trusted entities for assuming roles (trust relationship) | `list(string)` | `[]` | no |
316347

317348
## Outputs

examples/api-gateway-event-source/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@ Note that this example may create resources which cost money. Run `terraform des
1919

2020
| Name | Version |
2121
|------|---------|
22-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.14.0 |
22+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
2323
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.19 |
24-
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 0 |
24+
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 3 |
2525

2626
## Providers
2727

2828
| Name | Version |
2929
|------|---------|
3030
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.19 |
31-
| <a name="provider_random"></a> [random](#provider\_random) | >= 0 |
31+
| <a name="provider_random"></a> [random](#provider\_random) | >= 3 |
3232

3333
## Modules
3434

3535
| Name | Source | Version |
3636
|------|--------|---------|
37-
| <a name="module_api_gateway"></a> [api\_gateway](#module\_api\_gateway) | terraform-aws-modules/apigateway-v2/aws | 0.14.0 |
38-
| <a name="module_apigateway_put_events_to_eventbridge_policy"></a> [apigateway\_put\_events\_to\_eventbridge\_policy](#module\_apigateway\_put\_events\_to\_eventbridge\_policy) | terraform-aws-modules/iam/aws//modules/iam-policy | 3.13.0 |
39-
| <a name="module_apigateway_put_events_to_eventbridge_role"></a> [apigateway\_put\_events\_to\_eventbridge\_role](#module\_apigateway\_put\_events\_to\_eventbridge\_role) | terraform-aws-modules/iam/aws//modules/iam-assumable-role | 3.13.0 |
37+
| <a name="module_api_gateway"></a> [api\_gateway](#module\_api\_gateway) | terraform-aws-modules/apigateway-v2/aws | ~> 0 |
38+
| <a name="module_apigateway_put_events_to_eventbridge_policy"></a> [apigateway\_put\_events\_to\_eventbridge\_policy](#module\_apigateway\_put\_events\_to\_eventbridge\_policy) | terraform-aws-modules/iam/aws//modules/iam-policy | ~> 3 |
39+
| <a name="module_apigateway_put_events_to_eventbridge_role"></a> [apigateway\_put\_events\_to\_eventbridge\_role](#module\_apigateway\_put\_events\_to\_eventbridge\_role) | terraform-aws-modules/iam/aws//modules/iam-assumable-role | ~> 3 |
4040
| <a name="module_eventbridge"></a> [eventbridge](#module\_eventbridge) | ../../ | |
4141

4242
## Resources

examples/api-gateway-event-source/main.tf

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
terraform {
2-
required_version = ">= 0.14.0"
3-
4-
required_providers {
5-
aws = ">= 3.19"
6-
random = ">= 0"
7-
}
8-
}
9-
101
provider "aws" {
112
region = "ap-southeast-1"
123

@@ -66,7 +57,7 @@ resource "random_pet" "this" {
6657

6758
module "api_gateway" {
6859
source = "terraform-aws-modules/apigateway-v2/aws"
69-
version = "0.14.0"
60+
version = "~> 0"
7061

7162
name = "${random_pet.this.id}-http"
7263
description = "My ${random_pet.this.id} HTTP API Gateway"
@@ -95,16 +86,14 @@ module "api_gateway" {
9586

9687
module "apigateway_put_events_to_eventbridge_role" {
9788
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role"
98-
version = "3.13.0"
89+
version = "~> 3"
9990

10091
create_role = true
10192

10293
role_name = "apigateway-put-events-to-eventbridge"
10394
role_requires_mfa = false
10495

105-
trusted_role_services = [
106-
"apigateway.amazonaws.com"
107-
]
96+
trusted_role_services = ["apigateway.amazonaws.com"]
10897

10998
custom_role_policy_arns = [
11099
module.apigateway_put_events_to_eventbridge_policy.arn
@@ -113,10 +102,9 @@ module "apigateway_put_events_to_eventbridge_role" {
113102

114103
module "apigateway_put_events_to_eventbridge_policy" {
115104
source = "terraform-aws-modules/iam/aws//modules/iam-policy"
116-
version = "3.13.0"
105+
version = "~> 3"
117106

118107
name = "apigateway-put-events-to-eventbridge"
119-
path = "/"
120108
description = "Allow PutEvents to EventBridge"
121109

122110
policy = data.aws_iam_policy_document.apigateway_put_events_to_eventbridge_policy.json
@@ -149,10 +137,12 @@ data "aws_iam_policy_document" "queue" {
149137
statement {
150138
sid = "AllowSendMessage"
151139
actions = ["sqs:SendMessage"]
140+
152141
principals {
153142
type = "Service"
154143
identifiers = ["events.amazonaws.com"]
155144
}
145+
156146
resources = [aws_sqs_queue.queue.arn]
157147
}
158148
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
terraform {
2+
required_version = ">= 0.13.1"
3+
4+
required_providers {
5+
aws = ">= 3.19"
6+
random = ">= 3"
7+
}
8+
}

0 commit comments

Comments
 (0)