How do you recreate a Datadog Synthetics Browser test in Terraform?

Viewed 28

I am attempting to build a series of synthetic browser tests in Datadog via Terraform using a map of URLs. The test will go to a URL, type dummy credentials into the login form, attempt to log in, and assert that there will be an invalid username/password response. My code fails when I attempt to run a terraform apply. I have referenced the documentation, but I have not been able to find examples of browser tests with step types of typeText. Have I set up my params incorrectly?

Code:

resource "datadog_synthetics_test" "login_tests" {
  for_each = var.browser_test_urls
  type = "browser"

  request_definition {
    method = "GET"
    url = each.value
  }

  device_ids = ["laptop_large"]
  locations = ["aws:us-east-1"]

  options_list {
    tick_every = 1800
    follow_redirects = true

    retry {
      count    = 2
      interval = 60000 
    }
  }

  name = "Login Test for ${each.key}"
  message = "Login test failed for ${each.key} on url ${each.value}"

  status = "paused"

  browser_step {
    name = "Type Username"
    type = "typeText"
    params {
      element = "#userItem"
      value = "username"
    }
  }

    browser_step {
    name = "Type Password"
    params {
      element = "#passItem"
      value = "password"
    }
    type = "typeText"
  }

    browser_step {
    name = "Click Login Button"
    params {
      element = "#btlogin"
    }
    type = "click"
  }

  browser_step {
    name = "Check for Invalid Login Message"
    params {
      check = "contains"
      value = "Invalid username or password!"
    }
    type = "assertPageContains"
  }
}

Error:

│ Error: error creating synthetics browser test from https://us3.datadoghq.com/api/v1/synthetics/tests/browser: 400 Bad Request: {"errors":["Invalid steps data: 
Step 0 has invalid params: None is not of type 'object'"]}
│
│   with module.datadog.datadog_synthetics_test.login_tests["Test"],
│   on modules\datadog\browser_tests.tf line 1, in resource "datadog_synthetics_test" "login_tests":
│    1: resource "datadog_synthetics_test" "login_tests" {
1 Answers

To anyone facing a similar issue, this is how I ended up solving it.

I created the synthetic through the Datadog UI, and then imported it into my terraform state. From there I looked at my state file to see the value of the element property. It was a long x-path style. I copied and pasted the entire string into my element property, and it worked like a charm!

Related