I'm trying to run my terraform scripts and it's throwing the above errors.
Project Synopsis: I'm creating an AWS multi-account infrastructure using AWS organizations to create a dev and prod environment. Scripts shown here is only for the dev_account
I created a module for the organizational unit (ou) and accounts differently and called them in the root module
Root main.tf
# Root main.tf
module "dev_account" {
source = "./modules/accounts"
name = "development_account"
parent_id = aws_organizations_organizational_unit.development.id
email = "myemail@gmail.com"
}
organizational unit(ou) module
# ./modules/ous/main.tf
data "aws_organizations_organization" "root" {}
locals {
root_id = data.aws_organizations_organization.root.roots[0].id
}
resource "aws_organizations_organizational_unit" "development" {
name = "development_ou"
parent_id = local.root_id
}
Accounts module
# ./modules/accounts/main.tf
# Showing only dev_account
resource "aws_organizations_account" "dev_account" {
name = var.name
email = var.email
parent_id = var.parent_id
}
resource "aws_iam_account_password_policy" "dev_account" {
max_password_age = var.max_password_age
minimum_password_length = var.minimum_password_length
allow_users_to_change_password = var.allow_users_to_change_password
hard_expiry = var.hard_expiry
password_reuse_prevention = var.password_reuse_prevention
require_lowercase_characters = var.require_lowercase_characters
require_uppercase_characters = var.require_uppercase_characters
require_numbers = var.require_numbers
require_symbols = var.require_symbols
}
Accounts module variables
# ./modules/accounts/variables.tf
variable "name" {
default = "development_account"
}
variable "email" {
default = "myemail@gmail.com"
}
variable "parent_id" {
description = "parent of root organization"
}
variable "max_password_age" {
default = "90"
}
variable "minimum_password_length" {
default = "8"
}
variable "allow_users_to_change_password" {
default = "true"
}
variable "hard_expiry" {
default = "true"
}
variable "password_reuse_prevention" {
default = "true"
}
variable "require_lowercase_characters" {
default = "true"
}
variable "require_uppercase_characters" {
default = "true"
}
variable "require_numbers" {
default = "true"
}
variable "require_symbols" {
default = "false"
}
Error
Error: Reference to undeclared resource
│
│ on main.tf line 16, in module "dev_account":
│ 16: parent_id = aws_organizations_organizational_unit.development.id
│
│ A managed resource "aws_organizations_organizational_unit" "development" has not been declared in the root module.
I'm currently stuck in how to resolve this