Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run s3 jobs via step functions #136

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
be4e592
run s3 jobs for step functions
coilysiren Sep 13, 2024
d832a10
try using detail for command
coilysiren Sep 13, 2024
1cc47a0
Take 2
coilysiren Sep 13, 2024
7f87e36
use containerOverrides command
coilysiren Sep 13, 2024
15f623a
states array
coilysiren Sep 13, 2024
26c8eef
dollar sign
coilysiren Sep 13, 2024
d32b83f
update command syntax
coilysiren Sep 13, 2024
efce4e4
update path
coilysiren Sep 13, 2024
58f8769
casing
coilysiren Sep 13, 2024
e36eff4
take ... 4 or 5
coilysiren Sep 13, 2024
b12eba7
take 6
coilysiren Sep 13, 2024
08f197f
less dollar sign
coilysiren Sep 13, 2024
ee238ed
States.Array
coilysiren Sep 13, 2024
c5e49c0
typo
coilysiren Sep 13, 2024
e7e670b
try with the actual input
coilysiren Sep 13, 2024
3071c12
remove states.array
coilysiren Sep 13, 2024
c069abb
docs
coilysiren Sep 13, 2024
44f0d93
Simplify via task_command
coilysiren Sep 24, 2024
2c4faba
Fix step functions diffs
coilysiren Sep 24, 2024
916b733
meaningless commit to trigger CI
coilysiren Sep 24, 2024
4cb6821
Revert "Simplify via task_command"
coilysiren Sep 27, 2024
ed2664d
Revert "Revert "Simplify via task_command""
coilysiren Sep 27, 2024
909da43
Revert "Fix step functions diffs"
coilysiren Sep 27, 2024
3f581d0
add job type to log group prefix
coilysiren Sep 27, 2024
3287f9d
remove dynamic statement
coilysiren Sep 27, 2024
51350c5
aws_sfn_state_machine typo
coilysiren Sep 27, 2024
3c521ec
Merge branch 'main' into kai/events-via-sfn
coilysiren Sep 27, 2024
a2edc4d
Merge branch 'main' into kai/events-via-sfn
coilysiren Sep 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 80 additions & 17 deletions infra/modules/service/events_jobs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,18 @@ resource "aws_cloudwatch_event_target" "document_upload_jobs" {

target_id = "${local.cluster_name}-${each.key}"
rule = aws_cloudwatch_event_rule.file_upload_jobs[each.key].name
arn = aws_ecs_cluster.cluster.arn
arn = aws_sfn_state_machine.file_upload_jobs[each.key].arn
role_arn = aws_iam_role.events.arn

ecs_target {
task_definition_arn = aws_ecs_task_definition.app.arn
launch_type = "FARGATE"
propagate_tags = "TASK_DEFINITION"

# Configuring Network Configuration is required when the task definition uses the awsvpc network mode.
network_configuration {
subnets = var.private_subnet_ids
security_groups = [aws_security_group.app.id]
}
}

input_transformer {
input_paths = {
bucket_name = "$.detail.bucket.name",
object_key = "$.detail.object.key",
}

# When triggering the ECS task, override the command to run in the container to the
# command specified by the file_upload_job config. To do this define an input_template
# that transforms the input S3 event:
# When triggering the ECS task (via step functions), override the command to run in
# the container to the command specified by the file_upload_job config. To do this
# define an input_template that transforms the input S3 event:
# {
# detail: {
# bucket: { name: "mybucket" },
Expand Down Expand Up @@ -98,10 +86,85 @@ resource "aws_cloudwatch_event_target" "document_upload_jobs" {
input_template = replace(replace(jsonencode({
containerOverrides = [
{
name = local.container_name,
command = each.value.task_command
}
]
}), "\\u003c", "<"), "\\u003e", ">")
}
}
coilysiren marked this conversation as resolved.
Show resolved Hide resolved

resource "aws_sfn_state_machine" "file_upload_jobs" {
for_each = var.file_upload_jobs

name = "${var.service_name}-${each.key}"
role_arn = aws_iam_role.workflow_orchestrator.arn
coilysiren marked this conversation as resolved.
Show resolved Hide resolved

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" : [
{
# Pull the task command out of the input data, which is shaped like so:
#
# {
# "containerOverrides": [
# {
# "command": [
# "<task_command_arg_1>"
# "<task_command_arg_2>"
# ...
# ]
# }
# ]
# }
#
# The syntax for parsing the input data comes from JSONPath.
"Name" : local.container_name,
"Command.$" : "$.containerOverrides[0].command[*]"
coilysiren marked this conversation as resolved.
Show resolved Hide resolved
}
]
}
},
"End" : true
}
}
})

logging_configuration {
log_destination = "${aws_cloudwatch_log_group.file_upload_jobs[each.key].arn}:*"
include_execution_data = true
level = "ERROR"
}

tracing_configuration {
enabled = true
}
}

resource "aws_cloudwatch_log_group" "file_upload_jobs" {
for_each = var.file_upload_jobs

name_prefix = "/aws/vendedlogs/states/${var.service_name}-${each.key}"
coilysiren marked this conversation as resolved.
Show resolved Hide resolved

# 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
}
44 changes: 26 additions & 18 deletions infra/modules/service/events_role.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,35 @@ resource "aws_iam_policy" "run_task" {

data "aws_iam_policy_document" "run_task" {
coilysiren marked this conversation as resolved.
Show resolved Hide resolved
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]
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.file_upload_jobs

content {
actions = [
"states:StartExecution",
]
resources = [statement.value.arn]
coilysiren marked this conversation as resolved.
Show resolved Hide resolved
}
}

statement {
effect = "Allow"
actions = ["iam:PassRole"]
resources = [
aws_iam_role.task_executor.arn,
aws_iam_role.app_service.arn,
]
condition {
test = "StringLike"
variable = "iam:PassedToService"
values = ["ecs-tasks.amazonaws.com"]
dynamic "statement" {
for_each = aws_sfn_state_machine.file_upload_jobs

content {
actions = [
"states:DescribeExecution",
"states:StopExecution",
]
resources = ["${statement.value.arn}:*"]
coilysiren marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Loading