Skip to content

Commit

Permalink
feat: Add example for ECS + scheduled events (terraform-aws-modules#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
icco authored Oct 12, 2021
1 parent 83e1e5a commit 32ea196
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ module "eventbridge" {
* [Using Default Bus](/~https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/default-bus) - Creates resources in the `default` bus.
* [Archive](/~https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-archive) - EventBridge Archives resources in various configurations.
* [Permissions](/~https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-permissions) - Controls permissions to EventBridge.
* [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.


<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Expand Down
62 changes: 62 additions & 0 deletions examples/with-ecs-scheduling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# EventBridge ECS & Scheduled Events Example

Configuration in this directory creates EventBridge resource configuration including an ECS service.

## Usage

To run this example you need to execute:

```bash
$ terraform init
$ terraform plan
$ terraform apply
```

Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.

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

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.19 |
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 3 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.19 |
| <a name="provider_random"></a> [random](#provider\_random) | >= 3 |

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_ecs"></a> [ecs](#module\_ecs) | terraform-aws-modules/ecs/aws | ~> 3.0 |
| <a name="module_eventbridge"></a> [eventbridge](#module\_eventbridge) | ../../ | |

## Resources

| Name | Type |
|------|------|
| [aws_ecs_service.hello_world](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service) | resource |
| [aws_ecs_task_definition.hello_world](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition) | resource |
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
| [aws_security_group.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/security_group) | data source |
| [aws_subnet_ids.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids) | data source |
| [aws_vpc.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |

## Inputs

No inputs.

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_eventbridge_bus_arn"></a> [eventbridge\_bus\_arn](#output\_eventbridge\_bus\_arn) | The EventBridge Bus ARN |
| <a name="output_eventbridge_rule_arns"></a> [eventbridge\_rule\_arns](#output\_eventbridge\_rule\_arns) | The EventBridge Rule ARNs |
| <a name="output_eventbridge_rule_ids"></a> [eventbridge\_rule\_ids](#output\_eventbridge\_rule\_ids) | The EventBridge Rule IDs |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
120 changes: 120 additions & 0 deletions examples/with-ecs-scheduling/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
provider "aws" {
region = "ap-southeast-1"

# Make it faster by skipping something
skip_get_ec2_platforms = true
skip_metadata_api_check = true
skip_region_validation = true
skip_credentials_validation = true
skip_requesting_account_id = true
}

#############################################################
# Data sources to get VPC and default security group details
#############################################################
data "aws_vpc" "default" {
default = true
}

data "aws_security_group" "default" {
name = "default"
vpc_id = data.aws_vpc.default.id
}

data "aws_subnet_ids" "default" {
vpc_id = data.aws_vpc.default.id
}

####################
# Actual Eventbridge
####################
module "eventbridge" {
source = "../../"

# Schedules can only be created on default bus
create_bus = false

create_role = true
role_name = "ecs-eventbridge-${random_pet.this.id}"
attach_ecs_policy = true
ecs_target_arns = [aws_ecs_task_definition.hello_world.arn]

# Fire every five minutes
rules = {
orders = {
description = "Cron for Orders"
enabled = false
schedule_expression = "rate(5 minutes)"
}
}

# Send to a fargate ECS cluster
targets = {
orders = [
{
name = "orders"
arn = module.ecs.ecs_cluster_arn
attach_role_arn = true

ecs_target = {
launch_type = "FARGATE"
task_count = 1
task_definition_arn = aws_ecs_task_definition.hello_world.arn

network_configuration = {
assign_public_ip = true
subnets = data.aws_subnet_ids.default
security_groups = data.aws_security_group.default
}
}
}
]
}
}

######
# ECS
######

module "ecs" {
source = "terraform-aws-modules/ecs/aws"
version = "~> 3.0"

name = random_pet.this.id

capacity_providers = ["FARGATE", "FARGATE_SPOT"]
}

resource "aws_ecs_service" "hello_world" {
name = "hello_world-${random_pet.this.id}"
cluster = module.ecs.ecs_cluster_id
task_definition = aws_ecs_task_definition.hello_world.arn
launch_type = "FARGATE"

desired_count = 1

deployment_maximum_percent = 100
deployment_minimum_healthy_percent = 0
}

resource "aws_ecs_task_definition" "hello_world" {
family = "hello_world-${random_pet.this.id}"

container_definitions = jsonencode([
{
name = "hello_world-${random_pet.this.id}",
image = "hello-world",
cpu = 0,
memory = 128
}
])
}

##################
# Extra resources
##################

resource "random_pet" "this" {
length = 2
}

14 changes: 14 additions & 0 deletions examples/with-ecs-scheduling/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "eventbridge_bus_arn" {
description = "The EventBridge Bus ARN"
value = module.eventbridge.eventbridge_bus_arn
}

output "eventbridge_rule_ids" {
description = "The EventBridge Rule IDs"
value = module.eventbridge.eventbridge_rule_ids
}

output "eventbridge_rule_arns" {
description = "The EventBridge Rule ARNs"
value = module.eventbridge.eventbridge_rule_arns
}
Empty file.
8 changes: 8 additions & 0 deletions examples/with-ecs-scheduling/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_version = ">= 0.13.1"

required_providers {
aws = ">= 3.19"
random = ">= 3"
}
}

0 comments on commit 32ea196

Please sign in to comment.