|
| 1 | +resource "aws_sfn_state_machine" "metadata_refresh" { |
| 2 | + name = "${local.csi}-state-machine-housekeeping" |
| 3 | + role_arn = aws_iam_role.sfn_metadata_refresh.arn |
| 4 | + |
| 5 | + definition = jsonencode({ |
| 6 | + "Comment": "Workflow to update the metadata in the reporting tables.", |
| 7 | + "StartAt": "Update Metadata", |
| 8 | + "States": { |
| 9 | + "Update Metadata": { |
| 10 | + "Type": "Task", |
| 11 | + "Resource": "arn:aws:states:::athena:startQueryExecution", |
| 12 | + "Parameters": { |
| 13 | + "QueryString": "MSCK REPAIR TABLE ${aws_glue_catalog_table.event_record.name}", |
| 14 | + "WorkGroup": "${aws_athena_workgroup.reporting.name}", |
| 15 | + "QueryExecutionContext": { |
| 16 | + "Database": "${aws_glue_catalog_database.reporting.name}" |
| 17 | + } |
| 18 | + }, |
| 19 | + "End": true |
| 20 | + } |
| 21 | + } |
| 22 | + }) |
| 23 | + |
| 24 | + logging_configuration { |
| 25 | + log_destination = "${aws_cloudwatch_log_group.reporting.arn}:*" |
| 26 | + include_execution_data = true |
| 27 | + level = "ERROR" |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +resource "aws_cloudwatch_log_group" "reporting" { |
| 32 | + name = "/aws/sfn-state-machine-metadata-refresh/${local.csi}" |
| 33 | + retention_in_days = var.log_retention_in_days |
| 34 | +} |
| 35 | + |
| 36 | +resource "aws_iam_role" "sfn_metadata_refresh" { |
| 37 | + name = "${local.csi}-sf-metadata-refresh-role" |
| 38 | + description = "Role used by the State Machine for Athena metadata refresh queries" |
| 39 | + assume_role_policy = data.aws_iam_policy_document.sfn_assumerole_metadata_refresh.json |
| 40 | +} |
| 41 | + |
| 42 | +data "aws_iam_policy_document" "sfn_assumerole_metadata_refresh" { |
| 43 | + statement { |
| 44 | + sid = "StateMachineAssumeRole" |
| 45 | + effect = "Allow" |
| 46 | + |
| 47 | + actions = [ |
| 48 | + "sts:AssumeRole" |
| 49 | + ] |
| 50 | + |
| 51 | + principals { |
| 52 | + type = "Service" |
| 53 | + |
| 54 | + identifiers = [ |
| 55 | + "states.amazonaws.com", |
| 56 | + "glue.amazonaws.com" |
| 57 | + ] |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +resource "aws_iam_role_policy_attachment" "sfn_metadata_refresh" { |
| 63 | + role = aws_iam_role.sfn_metadata_refresh.name |
| 64 | + policy_arn = aws_iam_policy.sfn_metadata_refresh.arn |
| 65 | +} |
| 66 | + |
| 67 | +resource "aws_iam_policy" "sfn_metadata_refresh" { |
| 68 | + name = "${local.csi}-sfn-metadata-refresh-policy" |
| 69 | + description = "Allow Step Function State Machine to run Athena metadata refresh queries" |
| 70 | + path = "/" |
| 71 | + policy = data.aws_iam_policy_document.sfn_metadata_refresh.json |
| 72 | +} |
| 73 | + |
| 74 | +data "aws_iam_policy_document" "sfn_metadata_refresh" { |
| 75 | + statement { |
| 76 | + sid = "AllowAthena" |
| 77 | + effect = "Allow" |
| 78 | + |
| 79 | + actions = [ |
| 80 | + "athena:startQueryExecution", |
| 81 | + ] |
| 82 | + |
| 83 | + resources = [ |
| 84 | + aws_athena_workgroup.reporting.arn, |
| 85 | + "arn:aws:athena:${var.region}:${var.aws_account_id}:datacatalog/*" |
| 86 | + ] |
| 87 | + } |
| 88 | +} |
0 commit comments