forked from terraform-aws-modules/terraform-aws-eventbridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add example for ECS + scheduled events (terraform-aws-modules#14)
- Loading branch information
Showing
6 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |