Skip to content

Commit 32ea196

Browse files
authored
feat: Add example for ECS + scheduled events (#14)
1 parent 83e1e5a commit 32ea196

File tree

6 files changed

+205
-0
lines changed

6 files changed

+205
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ module "eventbridge" {
231231
* [Using Default Bus](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/default-bus) - Creates resources in the `default` bus.
232232
* [Archive](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-archive) - EventBridge Archives resources in various configurations.
233233
* [Permissions](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-permissions) - Controls permissions to EventBridge.
234+
* [ECS Scheduled Events](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-ecs-scheduling) - Use default bus to schedule events on ECS.
234235

235236

236237
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# EventBridge ECS & Scheduled Events Example
2+
3+
Configuration in this directory creates EventBridge resource configuration including an ECS service.
4+
5+
## Usage
6+
7+
To run this example you need to execute:
8+
9+
```bash
10+
$ terraform init
11+
$ terraform plan
12+
$ terraform apply
13+
```
14+
15+
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.
16+
17+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
18+
## Requirements
19+
20+
| Name | Version |
21+
|------|---------|
22+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
23+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.19 |
24+
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 3 |
25+
26+
## Providers
27+
28+
| Name | Version |
29+
|------|---------|
30+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.19 |
31+
| <a name="provider_random"></a> [random](#provider\_random) | >= 3 |
32+
33+
## Modules
34+
35+
| Name | Source | Version |
36+
|------|--------|---------|
37+
| <a name="module_ecs"></a> [ecs](#module\_ecs) | terraform-aws-modules/ecs/aws | ~> 3.0 |
38+
| <a name="module_eventbridge"></a> [eventbridge](#module\_eventbridge) | ../../ | |
39+
40+
## Resources
41+
42+
| Name | Type |
43+
|------|------|
44+
| [aws_ecs_service.hello_world](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service) | resource |
45+
| [aws_ecs_task_definition.hello_world](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition) | resource |
46+
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
47+
| [aws_security_group.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/security_group) | data source |
48+
| [aws_subnet_ids.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids) | data source |
49+
| [aws_vpc.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
50+
51+
## Inputs
52+
53+
No inputs.
54+
55+
## Outputs
56+
57+
| Name | Description |
58+
|------|-------------|
59+
| <a name="output_eventbridge_bus_arn"></a> [eventbridge\_bus\_arn](#output\_eventbridge\_bus\_arn) | The EventBridge Bus ARN |
60+
| <a name="output_eventbridge_rule_arns"></a> [eventbridge\_rule\_arns](#output\_eventbridge\_rule\_arns) | The EventBridge Rule ARNs |
61+
| <a name="output_eventbridge_rule_ids"></a> [eventbridge\_rule\_ids](#output\_eventbridge\_rule\_ids) | The EventBridge Rule IDs |
62+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
provider "aws" {
2+
region = "ap-southeast-1"
3+
4+
# Make it faster by skipping something
5+
skip_get_ec2_platforms = true
6+
skip_metadata_api_check = true
7+
skip_region_validation = true
8+
skip_credentials_validation = true
9+
skip_requesting_account_id = true
10+
}
11+
12+
#############################################################
13+
# Data sources to get VPC and default security group details
14+
#############################################################
15+
data "aws_vpc" "default" {
16+
default = true
17+
}
18+
19+
data "aws_security_group" "default" {
20+
name = "default"
21+
vpc_id = data.aws_vpc.default.id
22+
}
23+
24+
data "aws_subnet_ids" "default" {
25+
vpc_id = data.aws_vpc.default.id
26+
}
27+
28+
####################
29+
# Actual Eventbridge
30+
####################
31+
module "eventbridge" {
32+
source = "../../"
33+
34+
# Schedules can only be created on default bus
35+
create_bus = false
36+
37+
create_role = true
38+
role_name = "ecs-eventbridge-${random_pet.this.id}"
39+
attach_ecs_policy = true
40+
ecs_target_arns = [aws_ecs_task_definition.hello_world.arn]
41+
42+
# Fire every five minutes
43+
rules = {
44+
orders = {
45+
description = "Cron for Orders"
46+
enabled = false
47+
schedule_expression = "rate(5 minutes)"
48+
}
49+
}
50+
51+
# Send to a fargate ECS cluster
52+
targets = {
53+
orders = [
54+
{
55+
name = "orders"
56+
arn = module.ecs.ecs_cluster_arn
57+
attach_role_arn = true
58+
59+
ecs_target = {
60+
launch_type = "FARGATE"
61+
task_count = 1
62+
task_definition_arn = aws_ecs_task_definition.hello_world.arn
63+
64+
network_configuration = {
65+
assign_public_ip = true
66+
subnets = data.aws_subnet_ids.default
67+
security_groups = data.aws_security_group.default
68+
}
69+
}
70+
}
71+
]
72+
}
73+
}
74+
75+
######
76+
# ECS
77+
######
78+
79+
module "ecs" {
80+
source = "terraform-aws-modules/ecs/aws"
81+
version = "~> 3.0"
82+
83+
name = random_pet.this.id
84+
85+
capacity_providers = ["FARGATE", "FARGATE_SPOT"]
86+
}
87+
88+
resource "aws_ecs_service" "hello_world" {
89+
name = "hello_world-${random_pet.this.id}"
90+
cluster = module.ecs.ecs_cluster_id
91+
task_definition = aws_ecs_task_definition.hello_world.arn
92+
launch_type = "FARGATE"
93+
94+
desired_count = 1
95+
96+
deployment_maximum_percent = 100
97+
deployment_minimum_healthy_percent = 0
98+
}
99+
100+
resource "aws_ecs_task_definition" "hello_world" {
101+
family = "hello_world-${random_pet.this.id}"
102+
103+
container_definitions = jsonencode([
104+
{
105+
name = "hello_world-${random_pet.this.id}",
106+
image = "hello-world",
107+
cpu = 0,
108+
memory = 128
109+
}
110+
])
111+
}
112+
113+
##################
114+
# Extra resources
115+
##################
116+
117+
resource "random_pet" "this" {
118+
length = 2
119+
}
120+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "eventbridge_bus_arn" {
2+
description = "The EventBridge Bus ARN"
3+
value = module.eventbridge.eventbridge_bus_arn
4+
}
5+
6+
output "eventbridge_rule_ids" {
7+
description = "The EventBridge Rule IDs"
8+
value = module.eventbridge.eventbridge_rule_ids
9+
}
10+
11+
output "eventbridge_rule_arns" {
12+
description = "The EventBridge Rule ARNs"
13+
value = module.eventbridge.eventbridge_rule_arns
14+
}

examples/with-ecs-scheduling/variables.tf

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
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)