# Passing policyid as runtime argument for policy rule

**URL:** <https://discuss.hashicorp.com/t/passing-policyid-as-runtime-argument-for-policy-rule/41361>\
**Category:** Terraform\
**Created:** [June 29, 2022, 5:56am UTC](https://discuss.hashicorp.com/t/passing-policyid-as-runtime-argument-for-policy-rule/41361 "2022-06-29T05:56:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahuljha600](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rahuljha600/32/15966_2.png) [@rahuljha600](https://discuss.hashicorp.com/u/rahuljha600)\
**Post date:** [June 29, 2022, 5:56am UTC](https://discuss.hashicorp.com/t/passing-policyid-as-runtime-argument-for-policy-rule/41361/1 "2022-06-29T05:56:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cregkly](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/cregkly/32/29038_2.png) [@cregkly](https://discuss.hashicorp.com/u/cregkly)\
**Post date:** [June 29, 2022, 7:59am UTC](https://discuss.hashicorp.com/t/passing-policyid-as-runtime-argument-for-policy-rule/41361/2 "2022-06-29T07:59:57Z")

</div>

It would be really helpful if you formatted your code 🙂

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

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

```

---

<div class="post-metadata">

**Author:** ![rahuljha600](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rahuljha600/32/15966_2.png) [@rahuljha600](https://discuss.hashicorp.com/u/rahuljha600)\
**Post date:** [June 29, 2022, 10:29am UTC](https://discuss.hashicorp.com/t/passing-policyid-as-runtime-argument-for-policy-rule/41361/3 "2022-06-29T10:29:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cregkly](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/cregkly/32/29038_2.png) [@cregkly](https://discuss.hashicorp.com/u/cregkly)\
**Post date:** [June 29, 2022, 10:35am UTC](https://discuss.hashicorp.com/t/passing-policyid-as-runtime-argument-for-policy-rule/41361/4 "2022-06-29T10:35:50Z")

</div>

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
