Skip to content

Repository files navigation

terraform-aws-ecs-app-scheduler

Lint Status LICENSE

This terraform module is an AWS ECS Application Module for Scheduler without an Application Load Balancer(ALB), designed to be used with DNXLabs/terraform-aws-ecs.

The following resources will be created:

  • IAM roles - The cloudwatch event needs an IAM Role to run the ECS task definition. A role is created and a policy will be granted via IAM policy.

  • IAM policy - Policy to be attached to the IAM Role. This policy will have a trust with the cloudwatch event service. And it will use the managed policy AmazonEC2ContainerServiceEventsRole created by AWS.

  • Cloudwatch Log Groups

    • You can specify the number of days you want to retain log events in the specified log group. Possible values are: 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, and 3653.
    • Export to a S3 Bucket - Whether to mark the log group to export to an S3 bucket (needs the module terraform-aws-log-exporter (https://github.com/DNXLabs/terraform-aws-log-exporter) to be deployed in the account/region)
  • ECS task definition - A task definition is required to run Docker containers in Amazon ECS. Some of the parameters you can specify in a task definition include:

    • Image - Docker image to deploy
      • Default Value = "alpine:latest"
    • CPU - Hard limit of the CPU for the container
      • Default Value = 0
    • Memory - Hard memory of the container
      • Default Value = 512
    • Name - Name of the ECS Service
    • Set log configuration
  • ECS Task-scheduler activated by cloudwatch events

  • Cron expression - You can create rules that self-trigger on an automated schedule in CloudWatch Events using cron or rate expressions. All scheduled events use UTC time zone and the minimum precision for schedules is 1 minute.

Resources

  1. Cloudwatch Events
  2. Cron expression
  3. IAM role
  4. IAM policy
  5. Cloudwatch Log Group
  6. ECS task definition

Resources definition

Cloudwatch Events

Amazon CloudWatch Events delivers a near real-time stream of system events that describe changes in Amazon Web Services (AWS) resources. Using simple rules that you can quickly set up, you can match events and route them to one or more target functions or streams. CloudWatch Events becomes aware of operational changes as they occur. CloudWatch Events responds to these operational changes and takes corrective action as necessary, by sending messages to respond to the environment, activating functions, making changes, and capturing state information.

Cloudwatch Events documentation

Cron expression

You can create rules that self-trigger on an automated schedule in CloudWatch Events using cron or rate expressions. All scheduled events use UTC time zone and the minimum precision for schedules is 1 minute.

Schedule expressions

Iam Role

The cloudwatch event needs an IAM Role to run the ECS task definition. A role is created and a policy will be granted via IAM policy.

IAM Policy

Policy to be attached to the IAM Role. This policy will have a trust with the cloudwatch event service. And it will use the managed policy AmazonEC2ContainerServiceEventsRole created by AWS.

Cloudwatch Log Group

A log group is a group of log streams that share the same retention, monitoring, and access control settings. You can define log groups and specify which streams to put into each group. There is no limit on the number of log streams that can belong to one log group.

Log Groups

Task Definition

A task definition is required to run Docker containers in Amazon ECS. Some of the parameters you can specify in a task definition include:

  • The Docker image to use with each container in your task
  • How much CPU and memory to use with each task or each container within a task
  • The launch type to use, which determines the infrastructure on which your tasks are hosted
  • The Docker networking mode to use for the containers in your task
  • The logging configuration to use for your tasks
  • Whether the task should continue to run if the container finishes or fails
  • The command the container should run when it is started
  • Any data volumes that should be used with the containers in the task
  • The IAM role that your tasks should use

Task definitions

Usage

module "example" {
  source               = "git::https://github.com/DNXLabs/terraform-aws-ecs-app-scheduler?ref=0.0.2"
  name                 = "example"
  vpc_id               = data.aws_vpc.selected.id # From DNXLabs/terraform-aws-ecs
  cluster_name         = module.ecs_apps.ecs_name # From DNXLabs/terraform-aws-ecs
  service_role_arn     = module.ecs_apps.ecs_service_iam_role_arn # From DNXLabs/terraform-aws-ecs
  task_role_arn        = module.ecs_apps.ecs_task_iam_role_arn # From DNXLabs/terraform-aws-ecs
  memory               = 512
  schedule_expression  = "0/30 * * * ? *" # it will trigger the task every 30 minutes https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html
  account_id           = var.aws_account_id
}

Requirements

Name Version
terraform >= 0.12.0

Providers

Name Version
aws n/a

Inputs

Name Description Type Default Required
account_id n/a any n/a yes
cloudwatch_logs_export Whether to mark the log group to export to an S3 bucket (needs terraform-aws-log-exporter to be deployed in the account/region) bool false no
cloudwatch_logs_retention Specifies the number of days you want to retain log events in the specified log group. Possible values are: 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, and 3653. number 120 no
cluster_name n/a string "Name of existing ECS Cluster to deploy this app to" no
cpu Hard limit for CPU for the container string "0" no
event_description n/a string "ECS task" no
iam_path n/a string "/" no
image Docker image to deploy (can be a placeholder) string "alpine:latest" no
launch_type Instance type. Fargate allowed string "EC2" no
memory Hard memory of the container string "512" no
name Name of your ECS service any n/a yes
rule_enabled Whether the rule should be enabled bool true no
schedule_expression Cron expression any n/a yes
service_role_arn Existing service role ARN created by ECS cluster module any n/a yes
task_role_arn Existing task role ARN created by ECS cluster module any n/a yes
vpc_id VPC ID to deploy this app to any n/a yes

Outputs

Name Description
aws_cloudwatch_log_group_arn n/a

Authors

Module managed by DNX Solutions.

License

Apache 2 Licensed. See LICENSE for full details.

About

This terraform module is an AWS ECS Application Module for Scheduler without an Application Load Balancer(ALB).

Resources

Code of conduct

Contributing

Stars

1 star

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages