title: Create AWS Athena view programmatically
body: <p><a href="https://stackoverflow.com/questions/46413145/can-you-create-views-in-amazon-athena/46416395#46416395">Can you create views in Amazon Athena?</a> outlines how to create a view using the User Interface.</p>
  
  <p>I'd like to create an AWS Athena View programatically, ideally using Terraform (which calls CloudFormation).</p>
  
  <p>I followed the steps outlined here: <a href="https://ujjwalbhardwaj.me/post/create-virtual-views-with-aws-glue-and-query-them-using-athena" rel="noreferrer">https://ujjwalbhardwaj.me/post/create-virtual-views-with-aws-glue-and-query-them-using-athena</a>, however I run into an issue with this in that the view goes stale quickly.</p>
  
  <p><code>...._view' is stale; it must be re-created.</code></p>
  
  <p>The terraform code looks like this:</p>
  
  <pre><code>resource "aws_glue_catalog_table" "adobe_session_view" {
  
    database_name = "${var.database_name}"
    name = "session_view"
  
    table_type = "VIRTUAL_VIEW"
    view_original_text = "/* Presto View: ${base64encode(data.template_file.query_file.rendered)} */"
    view_expanded_text = "/* Presto View */"
  
    parameters = {
      presto_view = "true"
      comment = "Presto View"
    }
  
    storage_descriptor {
      ser_de_info {
        name = "ParquetHiveSerDe"
        serialization_library = "org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe"
      }
  
      columns { name = "first_column" type = "string" }
      columns { name = "second_column" type = "int" }
      ...
      columns { name = "nth_column" type = "string" }
  }
  </code></pre>
  
  <p>An alternative I'd be happy to use is the AWS CLI, however <code>aws athena [option]</code> provides no option for this.</p>
  
  <p>I've tried:</p>
  
  <ul>
  <li><a href="https://docs.aws.amazon.com/cli/latest/reference/athena/create-named-query.html" rel="noreferrer">create-named-query</a> which I have not been able to get working for a statement such as <em>CREATE OR REPLACE VIEW</em> as this doesn't seem to be the intended use case for this command.</li>
  <li><a href="https://docs.aws.amazon.com/cli/latest/reference/athena/start-query-execution.html" rel="noreferrer">start-query-execution</a> which asks for an output location, which suggests that this is meant for querying the data and outputting the results, as opposed to making stateful changes/creations. It also seems to be paired with <a href="https://docs.aws.amazon.com/cli/latest/reference/athena/stop-query-execution.html" rel="noreferrer">stop-query-execution</a>.</li>
  </ul>