Sub topic 15
Automating account provisioning and baselines
This chapter assumes you can read Terraform, you know the Organizations concepts from earlier chapters (management account, OUs, OrganizationAccountAccessRole), and you can assume an IAM role from the CLI.
Why automate
Creating accounts by hand works for three accounts. At thirty, the accounts drift. One has a password policy and another doesn't. One blocks public S3 access and another doesn't. Infrastructure as code (IaC) fixes this in two ways. The account structure becomes reviewable code, and every new account gets the same baseline, which is the minimum set of settings and resources every account must have.
Two layers
Keep two layers separate:
- Structure. The organization, OUs, and accounts. This is applied from the management account, and few people should be able to change it.
- Baseline. Resources inside each account, such as security settings, logging, and roles. This is applied into each member account.
The structure layer is the Terraform configuration from the spine. It declares the organization, the Workloads OU, and the prod-app1 account. Add two arguments to the account resource:
resource "aws_organizations_account" "prod_app1" {
name = "prod-app1"
email = "[email protected]"
parent_id = aws_organizations_organizational_unit.workloads.id
role_name = "OrganizationAccountAccessRole"
close_on_deletion = false
lifecycle {
ignore_changes = [role_name]
}
}Both arguments guard against mistakes. close_on_deletion = false means terraform destroy removes the account from state but does not close it. ignore_changes prevents a spurious diff, because the provider cannot read role_name back after creation and a change to it would force replacement.
Applying a baseline
The baseline is a separate configuration that takes an account ID and assumes the administrative role in that account. This is the same sts:AssumeRole call you saw earlier, made by the provider.
variable "account_id" {
type = string
}
provider "aws" {
region = "eu-west-1"
assume_role {
role_arn = "arn:aws:iam::${var.account_id}:role/OrganizationAccountAccessRole"
}
}
resource "aws_s3_account_public_access_block" "baseline" {
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_iam_account_password_policy" "baseline" {
minimum_password_length = 14
require_numbers = true
require_symbols = true
max_password_age = 90
}Run it once per account, for example terraform apply -var account_id=111122223333, with one state file per account.
Common errors
- Reusing an email address. Each account needs a globally unique root email. Use a plus-address or a mail alias per account.
- Slow creation. Account creation is asynchronous and can take minutes, so the baseline step should retry the role assumption.
- Baseline in the same configuration as the account. The provider's
assume_roledepends on a value that doesn't exist until apply. Keep the two layers in separate configurations.
Exercise
Why is the baseline applied from a separate Terraform configuration instead of in the same one that creates the account?
Show answer
The baseline's provider must assume a role in an account that does not exist until the structure apply finishes. Terraform needs the provider settings before it can plan. A separate configuration runs after the account exists. It also keeps one state file per account.