Search code examples
timestampterraformterraform-provider-awsaws-step-functionsformatdatetime

Why terraform generate the same timestamp by using `formatdate` function?


https://www.terraform.io/docs/configuration/functions/formatdate.html Th above is the doc of this function, I used this to append a timestamp on a name of the SageMaker batch transform job, which will be triggered from a stepfunction state machine:

locals {
  timestamp = formatdate("YYYYMMDDhhmmss", timestamp())
}

in stepfunction terraform file:

  definition = templatefile("stepfuntion.json",
    {
      xxx
      timestamp      = local.timestamp
)

in the "stepfuntion.json":

{...
          "TransformJobName": "jobname-${timestamp}",
  
          }
      },
        "End": true
      }
    }
  }

Specifically, the jobname is defined in "TransformJobName": "jobname-${timestamp}", I applied terraform twice, 10am and 11am, but the second time it generated the same timestamp as the first time, am I missing something here? I thought this function will generate the real-time timestamp. I've been struggling for a whole morning now, many thanks.


Solution

  • works perfectly fine, I used your code as described in the question with the template file stepfuction.json

            # main.tf
            locals {
            current  = formatdate("YYYYMMDDhhmmss",timestamp())
        }
        output "tempasda"{
        value = templatefile("task.json", {timestamp = local.current, model_name="mymodel"})
        }
    

    and the corresponding output

        $ terraform  apply -auto-approve |grep TransformJobName
                "TransformJobName": "jobname-20210106134614",
    
        $ terraform  apply -auto-approve |grep TransformJobName
                "TransformJobName": "jobname-20210106134615",
    
        $ terraform  apply -auto-approve |grep TransformJobName
                "TransformJobName": "jobname-20210106134617",
                
        $ terraform  apply -auto-approve |grep TransformJobName
                "TransformJobName": "jobname-20210106134618",
    

    terrform version tested with 0.13.x and 0.14.x