terraform-provider-snowflake icon indicating copy to clipboard operation
terraform-provider-snowflake copied to clipboard

[General Usage]: snowflake_function gets recreated every time we run terraform plan / apply

Open nx-rebecca opened this issue 1 year ago • 6 comments

Terraform CLI Version

1.8.3

Terraform Provider Version

0.96.0

Company Name

No response

Terraform Configuration

resource "snowflake_function" "function" {
  depends_on = [snowflake_view.view]
  name       = "FUNCTION"
  database   = snowflake_database.db.name
  schema     = snowflake_schema.schema.name
  is_secure  = "true"
  arguments {
    name = "REQUIREDID"
    type = "VARCHAR"
  }
  comment     = "Function"
  return_type = "TABLE (Issued_at TIMESTAMP_NTZ, ID VARCHAR(16777216), POINT GEOGRAPHY, Validity_date DATE, Max NUMBER(38,2)"
  statement   = <<-EOT
SELECT * FROM "${snowflake_database.db.name}"."${snowflake_schema.schema.name}"."${snowflake_view.view.name}" WHERE "Issued_at" = (SELECT MAX("Issued_at") FROM "${snowflake_database.db.name}"."${snowflake_schema.schema.name}"."${snowflake_view.view.name}") AND ID = REQUIREDID
EOT
}

Category

category:resource

Object type(s)

resource:function

Expected Behavior

We would expect the snowflake_function not to be destroyed/recreated unless it has changed in some way in the tf definition.

Actual Behavior

Every time we run a terraform plan or apply, the snowflake_functions are destroyed and recreated. We think there's probably something not right in our function definition but we're not sure what.

Steps to Reproduce

.

How much impact is this issue causing?

Low

Logs

No response

Additional Information

No response

nx-rebecca avatar Nov 13 '24 10:11 nx-rebecca

Hey @nx-rebecca. Thanks for reaching out to us.

Function and procedure resources are currently being redesigned so we will tackle this improper behavior with it.

For now, you can use lifecycle#ignore_changes meta-argument to prevent the recreation.

sfc-gh-asawicki avatar Nov 13 '24 13:11 sfc-gh-asawicki

Hey @nx-rebecca.

In v0.100.0, we have introduced new function resources. The handling of data types was greatly improved. PLease check it out and let us know if this solves your issue.

sfc-gh-asawicki avatar Dec 13 '24 14:12 sfc-gh-asawicki

Hello @sfc-gh-asawicki, after the migration of a snowflake_function resource to snowflake_function_sql, there is the same issue with the new attribute function_definition. We can still use the lifecycle ignore_changes to bypass the issue but it could be nice to also resolve this. Thanks a lot.

ppo-38 avatar Feb 05 '25 17:02 ppo-38

Hey @ppo-38. Please provide a config, so that we can reproduce it.

sfc-gh-asawicki avatar Feb 05 '25 17:02 sfc-gh-asawicki

Hey @sfc-gh-asawicki , thanks for your quick answer. Here a sample of the resource :

resource "snowflake_function_sql" "my_function" {
  name     = "MY_FUNCTION"
  database = "MY_DB"
  schema   = "MY_SCHEMA"
  arguments {
    arg_name = "OBJECT_ID"
    arg_data_type = "VARCHAR"
  }
  comment     = "My function description"
  is_secure   = true
  return_type = "VARCHAR"
  function_definition = "sha2(concat(OBJECT_ID, 'xxxxxxx'), 256)"
}

First apply create the function, but next one try to replace it. I have this message in the plan : ~ function_definition = (sensitive value) # forces replacement

To fix the issue, I have to add this block in the resource definition:

lifecycle {
    ignore_changes = [ function_definition ]
}

ppo-38 avatar Feb 05 '25 20:02 ppo-38

Thanks, we will try to reproduce it next week.

sfc-gh-asawicki avatar Feb 06 '25 09:02 sfc-gh-asawicki