Functions may not be called here

I want to receive the date function and use it as a variable.
The code is simple.

variables.tf
variable “date_format” {
type = string
default = “YYYY-MM-DD”
}
variable “current_date” {
type = string
default = formatdate(“${var.date_format}”, timestamp())
}

Initiating this code will result in an error.

│ Error: Function calls not allowed

│ on variables.tf line 7, in variable “current_date”:
│ 7: default = formatdate(“{var.date_format}", timestamp()) │ │ Functions may not be called here. ╵ ╷ │ Error: Function calls not allowed │ │ on variables.tf line 7, in variable "current_date": │ 7: default = formatdate("{var.date_format}”, timestamp())

│ Functions may not be called here.

I’m using aws and using it in an ec2 instance, so it’s a Linux system. Is there a way to solve this problem?

Please wrap your code in a code block so that it’s readable.

I don’t think you can define a variable with a function, so I think it’s trying to use timestamp() in there that’s your issue.

Also, you can’t use a variable in another variable either, so using var.date_format inside var.current_date will also be an issue.

What exactly are you trying to do / how are you trying to use it? You can use it in an attribute or string formatting… here’s a simple / contrived example:

variable "date_format" {
  type    = string
  default = "YYYY-MM-DD"
}

resource "null_resource" "foo" {
  provisioner "local-exec" {
    command = "echo ${formatdate(var.date_format, timestamp())}"
  }
}

output "test" {
  value = formatdate(var.date_format, timestamp())
}

The result of “applying” this code:

% tf apply

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # null_resource.foo will be created
  + resource "null_resource" "foo" {
      + id = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + test = (known after apply)

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

null_resource.foo: Creating...
null_resource.foo: Provisioning with 'local-exec'...
null_resource.foo (local-exec): Executing: ["/bin/sh" "-c" "echo 2025-03-26"]
null_resource.foo (local-exec): 2025-03-26
null_resource.foo: Creation complete after 0s [id=6928785597574062963]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

test = "2025-03-26"