Passing policyid as runtime argument for policy rule

Hi All,

I am working on a terraform code that creates password policy and then policy rule in Okta, both the code to create a password policy and the rule is in the same tf file, but while creating the rule I need to pass the policyid of newly created password policy, can someone help how can i pass new policy id as argument in rule.

Below is my code sample:
variable “org_name” {}
variable “api_token” {}
variable “base_url” {}

Enable and configure the Okta provider

terraform {
required_providers {
okta = {
source = “okta/okta”
version = “~> 3.15”
}
}
}
provider “okta” {
org_name = var.org_name
base_url = var.base_url
api_token = var.api_token
}
locals {
instances = csvdecode(file(“./passwordpolicy.csv”))
}
resource “okta_policy_password” “passwordpolicy” {
for_each = { for inst in local.instances : inst.name => inst }
name = each.value.name
description = each.value.description
status = each.value.status
auth_provider = each.value.auth_provider
password_min_length = each.value.password_min_length
password_min_uppercase = each.value.password_min_uppercase
password_min_symbol = each.value.password_min_symbol
groups_included = split(“|”, each.value.groups_included)
priority = each.value.priority
}

Above code creates policy id, I need to pass that value in below code

resource “okta_policy_rule_password” “policy_rules” {
for_each = { for inst in local.instances : inst.name => inst }
policy_id =
name = each.value.name
status = each.value.status
password_change = each.value.passwordChange
password_reset = each.value.passwordReset
}

Regards
Rahul Jha

It would be really helpful if you formatted your code :slight_smile:

Since you are using the same map for both resources they share the same keys.

policy_id = okta_policy_password.passwordpolicy[each.key].id

Thanks Cregkly,

So as you said, what if I use to two different map for policy and rule?

when I try that I gets an error stating “The given key does not identify an element in this collection value”

Regards

Your code shows you using the same key, so unless you are using different code there is something else wrong.

You can do one of the following to help troubleshoot

  1. Run a terraform state list to see what resources you have.
  2. Add an output to see what the resources look like.
  3. If you haven’t had a successful run, you comment out the bad code and look at the plan to see the names