-
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.
- Loading branch information
1 parent
c90b787
commit bce64ed
Showing
12 changed files
with
297 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1844e4cf421c0867fbdf8dad1f3a6dd603118033 | ||
9616fcf8f156206aea4c3cb0a81459d7becef1ef |
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
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 @@ | ||
locals { | ||
# The `cron` here is the literal name of the scheduled job. It can be anything you want. | ||
# For example "file_upload_jobs" or "daily_report". Whatever makes sense for your use case. | ||
# The `task_command` is what you want your scheduled job to run, for example: ["poetry", "run", "flask"]. | ||
# Schedule expression defines the frequency at which the job should run. | ||
# The syntax for `schedule_expression` is explained in the following documentation: | ||
# https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-scheduled-rule-pattern.html | ||
scheduled_jobs = { | ||
# cron = { | ||
# task_command = ["python", "-m", "flask", "--app", "app.py", "cron"] | ||
# schedule_expression = "cron(0 * ? * * *)" | ||
# } | ||
} | ||
} |
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
File renamed without changes.
7 changes: 3 additions & 4 deletions
7
infra/modules/service/task-scheduler-role.tf → infra/modules/service/events_role.tf
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,86 @@ | ||
resource "aws_scheduler_schedule" "scheduled_jobs" { | ||
for_each = var.scheduled_jobs | ||
|
||
# TODO(https://github.com/navapbc/template-infra/issues/164) Encrypt with customer managed KMS key | ||
# checkov:skip=CKV_AWS_297:Encrypt with customer key in future work | ||
|
||
name = "${var.service_name}-${each.key}" | ||
state = "ENABLED" | ||
schedule_expression = each.value.schedule_expression | ||
schedule_expression_timezone = "Etc/UTC" | ||
|
||
flexible_time_window { | ||
mode = "OFF" | ||
} | ||
|
||
# target is the state machine | ||
target { | ||
arn = aws_sfn_state_machine.scheduled_jobs[each.key].arn | ||
role_arn = aws_iam_role.scheduler.arn | ||
|
||
retry_policy { | ||
maximum_retry_attempts = 0 | ||
} | ||
} | ||
} | ||
|
||
resource "aws_sfn_state_machine" "scheduled_jobs" { | ||
for_each = var.scheduled_jobs | ||
|
||
name = "${var.service_name}-${each.key}" | ||
role_arn = aws_iam_role.workflow_orchestrator.arn | ||
|
||
definition = jsonencode({ | ||
"StartAt" : "RunTask", | ||
"States" : { | ||
"RunTask" : { | ||
"Type" : "Task", | ||
# docs: https://docs.aws.amazon.com/step-functions/latest/dg/connect-ecs.html | ||
"Resource" : "arn:aws:states:::ecs:runTask.sync", | ||
"Parameters" : { | ||
"Cluster" : aws_ecs_cluster.cluster.arn, | ||
"TaskDefinition" : aws_ecs_task_definition.app.arn, | ||
"LaunchType" : "FARGATE", | ||
"NetworkConfiguration" : { | ||
"AwsvpcConfiguration" : { | ||
"Subnets" : var.private_subnet_ids, | ||
"SecurityGroups" : [aws_security_group.app.id], | ||
} | ||
}, | ||
"Overrides" : { | ||
"ContainerOverrides" : [ | ||
{ | ||
"Name" : var.service_name, | ||
"Command" : each.value.task_command | ||
} | ||
] | ||
} | ||
}, | ||
"End" : true | ||
} | ||
} | ||
}) | ||
|
||
logging_configuration { | ||
log_destination = "${aws_cloudwatch_log_group.scheduled_jobs[each.key].arn}:*" | ||
include_execution_data = true | ||
level = "ERROR" | ||
} | ||
|
||
tracing_configuration { | ||
enabled = true | ||
} | ||
} | ||
|
||
resource "aws_cloudwatch_log_group" "scheduled_jobs" { | ||
for_each = var.scheduled_jobs | ||
|
||
name_prefix = "/aws/vendedlogs/states/${var.service_name}-${each.key}" | ||
|
||
# Conservatively retain logs for 5 years. | ||
# Looser requirements may allow shorter retention periods | ||
retention_in_days = 1827 | ||
|
||
# TODO(https://github.com/navapbc/template-infra/issues/164) Encrypt with customer managed KMS key | ||
# checkov:skip=CKV_AWS_158:Encrypt service logs with customer key in future work | ||
} |
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,61 @@ | ||
#---------------------- | ||
# Schedule Manager Role | ||
#---------------------- | ||
# This role and policy are used by EventBridge to manage the scheduled jobs. | ||
|
||
resource "aws_iam_role" "scheduler" { | ||
name = "${var.service_name}-scheduler" | ||
managed_policy_arns = [aws_iam_policy.scheduler.arn] | ||
assume_role_policy = data.aws_iam_policy_document.scheduler_assume_role.json | ||
} | ||
|
||
data "aws_iam_policy_document" "scheduler_assume_role" { | ||
statement { | ||
actions = ["sts:AssumeRole"] | ||
principals { | ||
type = "Service" | ||
identifiers = ["scheduler.amazonaws.com"] | ||
} | ||
} | ||
} | ||
|
||
resource "aws_iam_policy" "scheduler" { | ||
name = "${var.service_name}-scheduler" | ||
policy = data.aws_iam_policy_document.scheduler.json | ||
} | ||
|
||
data "aws_iam_policy_document" "scheduler" { | ||
|
||
statement { | ||
sid = "StepFunctionsEvents" | ||
actions = [ | ||
"events:PutTargets", | ||
"events:PutRule", | ||
"events:DescribeRule", | ||
] | ||
resources = ["arn:aws:events:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:rule/StepFunctionsGetEventsForStepFunctionsExecutionRule"] | ||
} | ||
|
||
dynamic "statement" { | ||
for_each = aws_sfn_state_machine.scheduled_jobs | ||
|
||
content { | ||
actions = [ | ||
"states:StartExecution", | ||
] | ||
resources = [statement.value.arn] | ||
} | ||
} | ||
|
||
dynamic "statement" { | ||
for_each = aws_sfn_state_machine.scheduled_jobs | ||
|
||
content { | ||
actions = [ | ||
"states:DescribeExecution", | ||
"states:StopExecution", | ||
] | ||
resources = ["${statement.value.arn}:*"] | ||
} | ||
} | ||
} |
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,109 @@ | ||
#-------------------------------- | ||
# Scheduler Workflow Manager Role | ||
#-------------------------------- | ||
# This role and policy are used by the Step Functions state machine that manages the scheduled jobs workflow. | ||
|
||
resource "aws_iam_role" "workflow_orchestrator" { | ||
name = "${var.service_name}-workflow-orchestrator" | ||
managed_policy_arns = [aws_iam_policy.workflow_orchestrator.arn] | ||
assume_role_policy = data.aws_iam_policy_document.workflow_orchestrator_assume_role.json | ||
} | ||
|
||
data "aws_iam_policy_document" "workflow_orchestrator_assume_role" { | ||
statement { | ||
actions = ["sts:AssumeRole"] | ||
principals { | ||
type = "Service" | ||
identifiers = ["states.amazonaws.com"] | ||
} | ||
condition { | ||
test = "ArnLike" | ||
variable = "aws:SourceArn" | ||
values = ["arn:aws:states:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:stateMachine:*"] | ||
} | ||
|
||
condition { | ||
test = "StringLike" | ||
variable = "aws:SourceAccount" | ||
values = [ | ||
data.aws_caller_identity.current.account_id | ||
] | ||
} | ||
} | ||
} | ||
|
||
resource "aws_iam_policy" "workflow_orchestrator" { | ||
name = "${var.service_name}-workflow-orchestrator" | ||
policy = data.aws_iam_policy_document.workflow_orchestrator.json | ||
} | ||
|
||
#tfsec:ignore:aws-iam-no-policy-wildcards | ||
data "aws_iam_policy_document" "workflow_orchestrator" { | ||
# checkov:skip=CKV_AWS_111:These permissions are scoped just fine | ||
|
||
statement { | ||
sid = "UnscopeLogsPermissions" | ||
actions = [ | ||
"logs:CreateLogDelivery", | ||
"logs:CreateLogStream", | ||
"logs:GetLogDelivery", | ||
"logs:UpdateLogDelivery", | ||
"logs:DeleteLogDelivery", | ||
"logs:ListLogDeliveries", | ||
"logs:PutLogEvents", | ||
"logs:PutResourcePolicy", | ||
"logs:DescribeResourcePolicies", | ||
"logs:DescribeLogGroups", | ||
] | ||
resources = ["*"] | ||
} | ||
|
||
statement { | ||
sid = "StepFunctionsEvents" | ||
actions = [ | ||
"events:PutTargets", | ||
"events:PutRule", | ||
"events:DescribeRule", | ||
] | ||
resources = [ | ||
"arn:aws:events:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:rule/StepFunctionsGetEventsForECSTaskRule", | ||
] | ||
} | ||
|
||
statement { | ||
effect = "Allow" | ||
actions = ["ecs:RunTask"] | ||
resources = ["${aws_ecs_task_definition.app.arn_without_revision}:*"] | ||
condition { | ||
test = "ArnLike" | ||
variable = "ecs:cluster" | ||
values = [aws_ecs_cluster.cluster.arn] | ||
} | ||
} | ||
|
||
statement { | ||
effect = "Allow" | ||
actions = [ | ||
"ecs:StopTask", | ||
"ecs:DescribeTasks", | ||
] | ||
resources = ["arn:aws:ecs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:task/${var.service_name}/*"] | ||
condition { | ||
test = "ArnLike" | ||
variable = "ecs:cluster" | ||
values = [aws_ecs_cluster.cluster.arn] | ||
} | ||
} | ||
|
||
|
||
statement { | ||
sid = "PassRole" | ||
actions = [ | ||
"iam:PassRole", | ||
] | ||
resources = [ | ||
aws_iam_role.task_executor.arn, | ||
aws_iam_role.app_service.arn, | ||
] | ||
} | ||
} |
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 |
---|---|---|
|
@@ -30,6 +30,7 @@ locals { | |
"servicediscovery", | ||
"sns", | ||
"ssm", | ||
"states", | ||
"waf-regional", | ||
"wafv2", | ||
] | ||
|
bce64ed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report for
app
Test suite run success
16 tests passing in 5 suites.
Report generated by 🧪jest coverage report action from bce64ed