3

I am enabling Athena to query on Cloudtrail s3 logs using Terraform. To do this, I need to create database and tables in Glue Catalog. I am following this link.

In Terraform I am using aws_glue_catalog_table resource. How can I define columns with type struct and Array in terraform file?

I tried defining below ways but did not work.

  resource "aws_glue_catalog_database" "cloud_logs" {
    name = "trail_logs_db"
  }

  resource "aws_glue_catalog_table" "cloud_table" {
    name          = "trail_logs"
    database_name = "${aws_glue_catalog_database.cloud_logs.name}"

    table_type = "EXTERNAL_TABLE"

    parameters = {
      EXTERNAL = "TRUE"
    }

    storage_descriptor {
      location      = "s3://<BUCKET NAME>/AWSLogs/<AWS ACCOUNT ID>/"
      input_format  = "com.amazon.emr.cloudtrail.CloudTrailInputFormat"
      output_format = "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat"

      ser_de_info {
        name                  = "trail-logs"
        serialization_library = "com.amazon.emr.hive.serde.CloudTrailSerde"

        parameters {
          serialization.format = 1
        }
      }

      columns = [
        {
          name = "useridentity"
          type = "struct<type:string,
                 principalid:string,
                 arn:string,
                 accountid:string,
                 invokedby:string,
                 accesskeyid:string,
                 userName:string,>"
          comment = ""
        },
        {
          name    = "resources"
          type    = "array<STRUCT<ARN:string,
                    accountId:string,
                    type:string>>"
          comment = ""
        },
      ]
    }
  }

WHen I run terraform init It throws below error: Error: Error parsing test.tf: At 33:27: illegal char

2
  • Can you share an minimal reproducible example of your Terraform code and also explain more clearly what you mean by "did not work"? Did it error? If so edit your question to include the full error output. If it did something you were not expecting explain the result of the Terraform code and what you wanted instead.
    – ydaetskcoR
    Jul 22, 2019 at 9:44
  • @ydaetskcoR Thanks, I have updated my post. Jul 22, 2019 at 10:08

2 Answers 2

1

Found workaround, It does not look good though.

It works when I format it in one line.

  {
     name = "useridentity"
     type = "struct<type:string, principalid:string, arn:string, accountid:string,  invokedby:string, accesskeyid:string, userName:string,>"
      comment = ""
  },
  {
     name    = "resources"
     type    = "array<STRUCT<ARN:string, accountId:string, type:string>>"
     comment = ""
  },
2
  • for me the multiline version doesn't work at all. only in a single line, does it accept the resources...
    – Meike
    Sep 2, 2022 at 13:17
  • also, for me the type string my not contain any capital letters or spaces.
    – Meike
    Sep 29, 2022 at 11:27
1

What you want is a heredoc:

    {
          name = "useridentity"
          type = <<- EOT
                 struct<type:string,
                 principalid:string,
                 arn:string,
                 accountid:string,
                 invokedby:string,
                 accesskeyid:string,
                 userName:string,>
                 EOT
          comment = ""
        },
        {
          name    = "resources"
          type    = <<- EOT
                    array<STRUCT<ARN:string,
                    accountId:string,
                    type:string>>
                    EOT
          comment = ""
        },
1
  • Thanks @Robert. I tried but it is showing in change in resource.. Expected way --> "array<struct<ARN:string,accountId:string,type:string>>" Actual ==> "array<struct<ARN:string,\naccountId:string,\ntype:string>>\n" Jul 23, 2019 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.