├── .gitignore ├── README.md ├── base ├── lambda-task-runner.zip ├── lambda-task-runner │ └── index.js └── main.tf └── scheduled-task ├── main.tf └── task.tpl /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbrook/ecs-task-scheduler-tf/4356a402894a5dad58c09a2dfe1aa5686b2994e4/.gitignore -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AWS ECS Task Runner Terraform Module 2 | 3 | Allows scheduling standalone AWS EC2 Container Service (ECS) tasks with 4 | Terraform. There are two modules: 5 | 6 | 1. A [base](./base/main.tf) module to provision shared resources - a generic 7 | lambda function to run AWS ECS tasks and it's associated IAM role and policies. 8 | 2. A [scheduled-task](./scheduled-task/main.tf) module to provision indivdual 9 | scheduled tasks. 10 | * Creates a Cloudwatch scheduled event with a cron-style or rate expression. 11 | * Targets the scheduled event at the Lambda function with the arguments necessary to run the specified ECS task. 12 | 13 | ## Usage Example 14 | 15 | ``` 16 | /** 17 | * Shared resources to be used by ECS scheduled jobs. 18 | * A lambda function and it's IAM role and policies. 19 | */ 20 | module "ecs_task_scheduler_base_resources" { 21 | source = "git::ssh://git@github.com/jbrook/ecs-task-scheduler-tf.git//base" 22 | stack_name = "${var.stack_name}" 23 | ecs_cluster_id = "${var.ecs_cluster_id}" 24 | } 25 | 26 | /** 27 | * Periodically runs a job 28 | */ 29 | module "schedule_dw_access_token_refresh" { 30 | source = "git::ssh://git@github.com/jbrook/ecs-task-scheduler-tf.git//scheduled-task" 31 | job_identifier = "my-job" 32 | ecs_task_def = " { 5 | /* 6 | { 7 | "job_identifier": "${job_identifier}", 8 | "region": "${region}", 9 | "cluster": "${cluster}", 10 | "ecs_task_def": "${ecs_task_def}", 11 | "overrides": { 12 | "containerOverrides": [ 13 | { 14 | "name": "${container_name}", 15 | "command": "${container_cmd}" 16 | } 17 | ] 18 | } 19 | } 20 | */ 21 | 22 | console.log(events) 23 | var ecs_task_def = events.ecs_task_def 24 | var exec_region = events.region || 'undefined' 25 | var cluster = events.cluster || 'undefined' 26 | var overrides = events.overrides || 'undefined' 27 | console.log(ecs_task_def, exec_region) 28 | var params = { 29 | taskDefinition: ecs_task_def, 30 | cluster: cluster, 31 | overrides: overrides 32 | } 33 | ecs.runTask(params, function(err, data) { 34 | if (err) console.log(err, err.stack); // an error occurred 35 | else console.log(data); // successful response 36 | context.done(err, data) 37 | }) 38 | 39 | } 40 | -------------------------------------------------------------------------------- /base/main.tf: -------------------------------------------------------------------------------- 1 | /** 2 | * Module: ecs-task-scheduler-tf/base 3 | * 4 | * This module is a base module to provision shared resources that will be used 5 | * when scheduling ECS tasks. This base module is intended to be used with 6 | * ecs-task-scheduler/scheduled-task. The scheduled-task companion module could 7 | * be used multiple times in a single terraform environment but it does not make 8 | * provision generic resources like the lambda function and it's IAM role and 9 | * policies multiple times. Instead they are created by this module and can then 10 | * be passed on to scheduled-task modules. 11 | */ 12 | 13 | 14 | variable "stack_name" {} 15 | variable "ecs_cluster_id" {} 16 | 17 | /** 18 | * Lambda function to run an ECS task with the AWS SDK. 19 | */ 20 | resource "aws_lambda_function" "task_runner" { 21 | function_name = "${var.stack_name}-ECSTaskRunner" 22 | filename = "${path.module}/lambda-task-runner.zip" 23 | runtime = "nodejs4.3" 24 | timeout = 30 25 | description = "Runs an ECS task with specified overrides" 26 | role = "${aws_iam_role.task_runner_execution.arn}" 27 | handler = "index.handler" 28 | environment { 29 | variables = { 30 | SOME_VAR = "SOME_VALUE" 31 | } 32 | } 33 | lifecycle { 34 | # Attempt to workaround - https://github.com/hashicorp/terraform/issues/7613 35 | ignore_changes = ["filename"] 36 | } 37 | } 38 | 39 | /** 40 | * Execution role - a role that will allow the Lambda function 41 | * to be executed and to run ECS tasks. 42 | */ 43 | resource "aws_iam_role" "task_runner_execution" { 44 | name = "${var.stack_name}-task-runner-execution" 45 | assume_role_policy = <