Contains Function Does Not Work?


Running example Terraform Docs, receiving error not expected.

Example from docs reference: Terraform Registry


data "http" "example" {
  url = ""

  # Optional request headers
  request_headers = {
    Accept = "application/json"

resource "random_uuid" "example" {
  lifecycle {
    precondition {
      condition     = contains([201, 204], data.http.example.status_code)
      error_message = "Status code invalid"

Expectation: No Errors.

Actual Behaviour:

Plan: 0 to add, 0 to change, 1 to destroy.
│ Error: Resource postcondition failed
│   on line 29, in data "http" "example":
│   29:       condition     = contains([201,204], self.status_code)
│     ├────────────────
│     │ self.status_code is 200
│ Status code invalid...

Terraform: v1.6.3

Did I miss something?

As the documentation does not show what the expected output is supposed to be, then I don’t think you are missing anything. Your expectation is that it runs without errors, but I think that is an incorrect assumption.

If the precondition in the example did not fail it would not show anything different from the normal plan output so would be a poor illustration of an operating precondition.

I believe, therefore, that the example code is as expected (and throws an error as an example) because the response from the website has a status code of 200 (HTTP success) but the condition includes only 201 and 204 as ‘valid’