Skip to content

Commit 7558d67

Browse files
committed
First version of the module
1 parent 363ceea commit 7558d67

File tree

6 files changed

+249
-0
lines changed

6 files changed

+249
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,31 @@ This Terraform module deploys an AWS ECS Fargate scheduled task service.
44

55
[![CircleCI](https://circleci.com/gh/jnonino/terraform-aws-ecs-fargate-scheduled-task/tree/master.svg?style=svg)](https://circleci.com/gh/jnonino/terraform-aws-ecs-fargate-scheduled-task/tree/master)
66

7+
## Input values
8+
9+
* name_preffix: Name preffix for resources on AWS.
10+
* profile: AWS API key credentials to use.
11+
* region: AWS Region the infrastructure is hosted in.
12+
* ecs_cluster_arn: The ECS Cluster where the scheduled task will run.
13+
* event_rule_name: The rule's name.
14+
* event_rule_description: (Optional) The description of the rule.
15+
* event_rule_schedule_expression: (Required, if event_pattern isn't specified) The scheduling expression. For example, cron(0 20 * * ? *) or rate(5 minutes).
16+
* event_rule_event_pattern: (Required, if schedule_expression isn't specified) Event pattern described a JSON object. See full documentation of CloudWatch Events and Event Patterns for details.
17+
* event_rule_role_arn: (Optional) The Amazon Resource Name (ARN) associated with the role that is used for target invocation.
18+
* event_rule_is_enabled: (Optional) Whether the rule should be enabled (defaults to true).
19+
* event_target_target_id: (Optional) The unique target assignment ID. If missing, will generate a random, unique id.
20+
* event_target_input: (Optional) Valid JSON text passed to the target.
21+
* event_target_input_path: (Optional) The value of the JSONPath that is used for extracting part of the matched event when passing it to the target.
22+
* event_target_ecs_target_task_definition_arn: The ARN of the task definition to use if the event target is an Amazon ECS cluster.
23+
* event_target_ecs_target_subnets: The subnets associated with the task or service.
24+
* event_target_ecs_target_security_groups: (Optional) The security groups associated with the task or service. If you do not specify a security group, the default security group for the VPC is used.
25+
* event_target_ecs_target_assign_public_ip: (Optional) Assign a public IP address to the ENI (Fargate launch type only). Valid values are true or false. Default false.
26+
* event_target_ecs_target_task_count: (Optional) The number of tasks to create based on the TaskDefinition. The default is 1.
27+
* event_target_ecs_target_platform_version: (Optional) Specifies the platform version for the task. Specify only the numeric portion of the platform version, such as 1.1.0. This is used only if LaunchType is FARGATE.
28+
* event_target_ecs_target_group: (Optional) Specifies an ECS task group for the task. The maximum length is 255 characters.
29+
* ecs_execution_task_role_arn: The task definition execution role
30+
31+
## Output values
32+
33+
* aws_cloudwatch_event_rule_event_rule_arn: The Amazon Resource Name (ARN) of the CloudWatch Event Rule.
34+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Action": "sts:AssumeRole",
6+
"Principal": {
7+
"Service": "events.amazonaws.com"
8+
},
9+
"Effect": "Allow",
10+
"Sid": ""
11+
}
12+
]
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Action": [
7+
"ecs:RunTask"
8+
],
9+
"Resource": [
10+
"*"
11+
]
12+
},
13+
{
14+
"Effect": "Allow",
15+
"Action": "iam:PassRole",
16+
"Resource": [
17+
"${TASK_EXECUTION_ROLE_ARN}"
18+
]
19+
}
20+
]
21+
}

main.tf

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# ---------------------------------------------------------------------------------------------------------------------
2+
# PROVIDER
3+
# ---------------------------------------------------------------------------------------------------------------------
4+
provider "aws" {
5+
profile = var.profile
6+
region = var.region
7+
}
8+
9+
# ---------------------------------------------------------------------------------------------------------------------
10+
# CLOUDWATCH EVENT ROLE
11+
# ---------------------------------------------------------------------------------------------------------------------
12+
resource "aws_iam_role" "scheduled_task_cw_event_role" {
13+
name = "${var.name_preffix}-st-cloudwatch-role"
14+
assume_role_policy = "${file("${path.module}/files/iam/scheduled_task_cw_event_role_assume_role_policy.json")}"
15+
}
16+
17+
data "template_file" "scheduled_task_cloudwatch_policy" {
18+
template = "${file("${path.module}/files/iam/scheduled_task_cw_event_role_cloudwatch_policy.json")}"
19+
vars {
20+
TASK_EXECUTION_ROLE_ARN = var.ecs_execution_task_role_arn
21+
}
22+
}
23+
24+
resource "aws_iam_role_policy" "scheduled_task_cloudwatch_policy" {
25+
name = "${var.name_preffix}-st-cloudwatch-policy"
26+
role = "${aws_iam_role.scheduled_task_cloudwatch.id}"
27+
policy = "${data.template_file.scheduled_task_cloudwatch_policy.rendered}"
28+
}
29+
30+
# ---------------------------------------------------------------------------------------------------------------------
31+
# CLOUDWATCH EVENT RULE
32+
# ---------------------------------------------------------------------------------------------------------------------
33+
resource "aws_cloudwatch_event_rule" "event_rule" {
34+
name = var.event_rule_name
35+
description = var.event_rule_description
36+
schedule_expression = var.event_rule_schedule_expression
37+
event_pattern = var.event_rule_event_pattern
38+
role_arn = var.event_rule_role_arn
39+
is_enabled = var.event_rule_is_enabled
40+
tags = {
41+
Name = "${var.name_preffix}-cw-event-rule"
42+
}
43+
}
44+
45+
# ---------------------------------------------------------------------------------------------------------------------
46+
# CLOUDWATCH EVENT TARGET
47+
# ---------------------------------------------------------------------------------------------------------------------
48+
resource "aws_cloudwatch_event_target" "ecs_scheduled_task" {
49+
rule = aws_cloudwatch_event_rule.event_rule.name
50+
target_id = var.event_target_target_id
51+
arn = var.ecs_cluster_arn
52+
input = var.event_target_input
53+
input_path = var.event_target_input_path
54+
role_arn = aws_iam_role.scheduled_task_cw_event_role.arn
55+
ecs_target {
56+
task_definition_arn = var.event_target_ecs_target_task_definition_arn
57+
task_count = var.event_target_ecs_target_task_count
58+
platform_version = var.event_target_ecs_target_platform_version
59+
launch_type = "FARGATE"
60+
group = var.event_target_ecs_target_group
61+
network_configuration {
62+
subnets = var.event_target_ecs_target_subnets
63+
security_groups = var.event_target_ecs_target_security_groups
64+
assign_public_ip = var.event_target_ecs_target_assign_public_ip
65+
}
66+
}
67+
}
68+

outputs.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# ---------------------------------------------------------------------------------------------------------------------
2+
# CLOUDWATCH EVENT RULE
3+
# ---------------------------------------------------------------------------------------------------------------------
4+
output "aws_cloudwatch_event_rule_event_rule_arn" {
5+
description = "The Amazon Resource Name (ARN) of the CloudWatch Event Rule."
6+
value = aws_cloudwatch_event_rule.event_rule.arn
7+
}
8+

variables.tf

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# ---------------------------------------------------------------------------------------------------------------------
2+
# Misc
3+
# ---------------------------------------------------------------------------------------------------------------------
4+
variable "name_preffix" {
5+
description = "Name preffix for resources on AWS"
6+
}
7+
8+
# ---------------------------------------------------------------------------------------------------------------------
9+
# AWS CREDENTIALS AND REGION
10+
# ---------------------------------------------------------------------------------------------------------------------
11+
variable "profile" {
12+
description = "AWS API key credentials to use"
13+
}
14+
15+
variable "region" {
16+
description = "AWS Region the infrastructure is hosted in"
17+
}
18+
19+
# ---------------------------------------------------------------------------------------------------------------------
20+
# CLOUDWATCH EVENT RULE
21+
# ---------------------------------------------------------------------------------------------------------------------
22+
variable "event_rule_name" {
23+
description = "The rule's name."
24+
}
25+
26+
variable "event_rule_description" {
27+
description = "(Optional) The description of the rule."
28+
}
29+
30+
variable "event_rule_schedule_expression" {
31+
description = "(Required, if event_pattern isn't specified) The scheduling expression. For example, cron(0 20 * * ? *) or rate(5 minutes)."
32+
}
33+
34+
variable "event_rule_event_pattern" {
35+
description = "(Required, if schedule_expression isn't specified) Event pattern described a JSON object. See full documentation of CloudWatch Events and Event Patterns for details."
36+
}
37+
38+
variable "event_rule_role_arn" {
39+
description = "(Optional) The Amazon Resource Name (ARN) associated with the role that is used for target invocation."
40+
default = ""
41+
}
42+
43+
variable "event_rule_is_enabled" {
44+
description = "(Optional) Whether the rule should be enabled (defaults to true)."
45+
type = bool
46+
default = true
47+
}
48+
49+
# ---------------------------------------------------------------------------------------------------------------------
50+
# CLOUDWATCH EVENT TARGET
51+
# ---------------------------------------------------------------------------------------------------------------------
52+
variable "ecs_cluster_arn" {
53+
description = "The ECS Cluster where the scheduled task will run"
54+
}
55+
56+
variable "event_target_target_id" {
57+
description = "(Optional) The unique target assignment ID. If missing, will generate a random, unique id."
58+
default = ""
59+
}
60+
61+
variable "event_target_input" {
62+
description = "(Optional) Valid JSON text passed to the target."
63+
default = ""
64+
}
65+
66+
variable "event_target_input_path" {
67+
description = "(Optional) The value of the JSONPath that is used for extracting part of the matched event when passing it to the target."
68+
default = ""
69+
}
70+
71+
variable "event_target_ecs_target_task_definition_arn" {
72+
description = "(Required) The ARN of the task definition to use if the event target is an Amazon ECS cluster."
73+
}
74+
75+
variable "event_target_ecs_target_subnets" {
76+
description = "The subnets associated with the task or service."
77+
type = list
78+
}
79+
80+
variable "event_target_ecs_target_security_groups" {
81+
description = "(Optional) The security groups associated with the task or service. If you do not specify a security group, the default security group for the VPC is used."
82+
type = list
83+
default = []
84+
}
85+
86+
variable "event_target_ecs_target_assign_public_ip" {
87+
description = "(Optional) Assign a public IP address to the ENI (Fargate launch type only). Valid values are true or false. Default false."
88+
type = bool
89+
default = false
90+
}
91+
92+
variable "event_target_ecs_target_task_count" {
93+
description = "(Optional) The number of tasks to create based on the TaskDefinition. The default is 1."
94+
type = number
95+
default = 1
96+
}
97+
98+
variable "event_target_ecs_target_platform_version" {
99+
description = "(Optional) Specifies the platform version for the task. Specify only the numeric portion of the platform version, such as 1.1.0. This is used only if LaunchType is FARGATE. For more information about valid platform versions, see AWS Fargate Platform Versions. Default to LATEST"
100+
default = "LATEST"
101+
}
102+
103+
variable "event_target_ecs_target_group" {
104+
description = "(Optional) Specifies an ECS task group for the task. The maximum length is 255 characters."
105+
default = ""
106+
}
107+
108+
variable "ecs_execution_task_role_arn" {
109+
description = "The task definition execution role"
110+
}
111+

0 commit comments

Comments
 (0)