diff --git a/main.tf b/main.tf index 6a4103d2..076723f0 100644 --- a/main.tf +++ b/main.tf @@ -206,9 +206,11 @@ resource "aws_athena_named_query" "main" { query = templatefile( "${path.module}/templates/create_table.sql", { - bucket = var.existing_bucket_name != "" ? var.existing_bucket_name : module.s3-bucket[0].bucket.id - account_id = var.account_number - region = var.region + bucket = var.existing_bucket_name != "" ? var.existing_bucket_name : module.s3-bucket[0].bucket.id + account_id = var.account_number + region = var.region + application_name = var.application_name + database = aws_athena_database.lb-access-logs[0].name } ) } diff --git a/templates/create_table.sql b/templates/create_table.sql index 5ea6c2a6..25476c6b 100644 --- a/templates/create_table.sql +++ b/templates/create_table.sql @@ -1,4 +1,4 @@ -CREATE EXTERNAL TABLE IF NOT EXISTS lb_logs ( +CREATE EXTERNAL TABLE IF NOT EXISTS ${database}.lb_logs ( type string, time string, elb string, @@ -33,4 +33,4 @@ CREATE EXTERNAL TABLE IF NOT EXISTS lb_logs ( WITH SERDEPROPERTIES ( 'serialization.format' = '1', 'input.regex' = '([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*)[:-]([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-.0-9]*) (|[-0-9]*) (-|[-0-9]*) ([-0-9]*) ([-0-9]*) \"([^ ]*) ([^ ]*) (- |[^ ]*)\" \"([^\"]*)\" ([A-Z0-9-]+) ([A-Za-z0-9.-]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" ([-.0-9]*) ([^ ]*) \"([^\"]*)\"($| \"[^ ]*\")(.*)') - LOCATION 's3://${bucket}/AWSLogs/${account_id}/elasticloadbalancing/${region}/'; + LOCATION 's3://${bucket}/${application_name}/AWSLogs/${account_id}/elasticloadbalancing/${region}/';