AWSTemplateFormatVersion: 2010-09-09
Description: Create an Iceberg table with partitions using CloudFormationTemplate

Parameters:
  TableName:
    Type: String
    Description: Table name of the Iceberg Table
    Default: transactiontable2
    MinLength: '1'
    MaxLength: '64'
    ConstraintDescription: the table name must be between 1 and 64 characters
  DatabaseName:
    Type: String
    Description: Database name in which to create the Iceberg Table
    Default: bankdata_icebergdb
    MinLength: '1'
    MaxLength: '64'
    ConstraintDescription: the database name must be between 1 and 64 characters

# grouping of input parameters by relevance
Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      -
        Label:
          default: Table Parameters
        Parameters:
          - DatabaseName
          - TableName

Resources:
  CfnIcebergTable:
    Type: AWS::Glue::Table
    Properties: 
      CatalogId: !Ref AWS::AccountId
      DatabaseName: !Ref DatabaseName
      Name: !Ref TableName
      OpenTableFormatInput:
        IcebergInput:
          MetadataOperation: "CREATE"
          Version: "2"
          IcebergTableInput:
            Location: "s3://sampledatabucket/bankdataiceberg/transactiontable2/"
            Schema:
              Type: "struct"
              Fields:
                - Name: "transaction_id"
                  Type: "string"
                  Id: 1
                  Required: true
                - Name: "transaction_name"
                  Type: "string"
                  Id: 2
                  Required: true
                - Name: "transaction_amount"
                  Type: "float"
                  Id: 3
                  Required: true
                - Name: "transaction_date"
                  Type: "float"
                  Id: 4
                  Required: true
            PartitionSpec:
              SpecId: 1
              Fields:
                - SourceId: 1
                  Transform: "identity"
                  Name: "transaction_id"
                - SourceId: 4
                  Transform: "year"
                  Name: "transaction_date"
            WriteOrder:
              OrderId: 1
              Fields:
                - SourceId: 1
                  Transform: "identity"
                  Direction: "asc"
                  NullOrder: "nulls-last"