|
| 1 | +terraform { |
| 2 | + required_version = ">= 0.11.2" |
| 3 | + |
| 4 | + backend "s3" {} |
| 5 | +} |
| 6 | + |
| 7 | +provider "aws" { |
| 8 | + assume_role { |
| 9 | + role_arn = "${var.aws_assume_role_arn}" |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +# User account that will be used for provisioning |
| 14 | +module "user" { |
| 15 | + source = "git::https://github.com/cloudposse/terraform-aws-iam-system-user.git?ref=tags/0.3.2" |
| 16 | + |
| 17 | + namespace = "${var.namespace}" |
| 18 | + stage = "${var.stage}" |
| 19 | + name = "bootstrap" |
| 20 | +} |
| 21 | + |
| 22 | +# Allow the user to assume role |
| 23 | +data "aws_iam_policy_document" "assume_role" { |
| 24 | + statement { |
| 25 | + effect = "Allow" |
| 26 | + |
| 27 | + actions = ["sts:AssumeRole"] |
| 28 | + |
| 29 | + principals { |
| 30 | + type = "AWS" |
| 31 | + identifiers = ["${module.user.user_arn}"] |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +# Fetch the OrganizationAccountAccessRole ARNs from SSM |
| 37 | +module "organization_account_access_role_arns" { |
| 38 | + source = "git::https://github.com/cloudposse/terraform-aws-ssm-parameter-store?ref=tags/0.1.5" |
| 39 | + parameter_read = "${formatlist("/${var.namespace}/%s/organization_account_access_role", var.accounts_enabled)}" |
| 40 | +} |
| 41 | + |
| 42 | +# IAM role for bootstrapping; allow user to assume it |
| 43 | +resource "aws_iam_role" "bootstrap" { |
| 44 | + name = "${module.user.user_name}" |
| 45 | + assume_role_policy = "${data.aws_iam_policy_document.assume_role.json}" |
| 46 | +} |
| 47 | + |
| 48 | +# Grant Administrator Access to the current "root" account to the role |
| 49 | +resource "aws_iam_role_policy_attachment" "administrator_access" { |
| 50 | + role = "${aws_iam_role.bootstrap.name}" |
| 51 | + policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess" |
| 52 | +} |
| 53 | + |
| 54 | +# Generate a policy for assuming role into all child accounts |
| 55 | +data "aws_iam_policy_document" "organization_account_access_role" { |
| 56 | + statement { |
| 57 | + actions = [ |
| 58 | + "sts:AssumeRole", |
| 59 | + ] |
| 60 | + |
| 61 | + effect = "Allow" |
| 62 | + resources = ["${module.organization_account_access_role_arns.values}"] |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +# Create an IAM policy from the generated document |
| 67 | +resource "aws_iam_policy" "organization_account_access_role" { |
| 68 | + name = "${aws_iam_role.bootstrap.name}" |
| 69 | + policy = "${data.aws_iam_policy_document.organization_account_access_role.json}" |
| 70 | +} |
| 71 | + |
| 72 | +# Assign the policy to the user |
| 73 | +resource "aws_iam_user_policy_attachment" "organization_account_access_role" { |
| 74 | + user = "${aws_iam_role.bootstrap.name}" |
| 75 | + policy_arn = "${aws_iam_policy.organization_account_access_role.arn}" |
| 76 | +} |
| 77 | + |
| 78 | +# Render the env file with IAM credentials |
| 79 | +data "template_file" "env" { |
| 80 | + template = "${file("${path.module}/env.tpl")}" |
| 81 | + |
| 82 | + vars { |
| 83 | + aws_access_key_id = "${module.user.access_key_id}" |
| 84 | + aws_secret_access_key = "${module.user.secret_access_key}" |
| 85 | + aws_assume_role_arn = "${aws_iam_role.bootstrap.arn}" |
| 86 | + aws_data_path = "${dirname(local_file.config_file.filename)}" |
| 87 | + aws_config_file = "${local_file.config_file.filename}" |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +# Write the env file to disk |
| 92 | +resource "local_file" "env_file" { |
| 93 | + content = "${data.template_file.env.rendered}" |
| 94 | + filename = "${var.output_path}/${var.env_file}" |
| 95 | +} |
| 96 | + |
| 97 | +# Render the credentials file with IAM credentials |
| 98 | +data "template_file" "credentials" { |
| 99 | + template = "${file("${path.module}/credentials.tpl")}" |
| 100 | + |
| 101 | + vars { |
| 102 | + source_profile_name = "${var.namespace}" |
| 103 | + aws_access_key_id = "${module.user.access_key_id}" |
| 104 | + aws_secret_access_key = "${module.user.secret_access_key}" |
| 105 | + aws_assume_role_arn = "${aws_iam_role.bootstrap.arn}" |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +# Write the credentials file to disk |
| 110 | +resource "local_file" "credentials_file" { |
| 111 | + content = "${data.template_file.credentials.rendered}" |
| 112 | + filename = "${var.output_path}/${var.credentials_file}" |
| 113 | +} |
| 114 | + |
| 115 | +# Render the config file with IAM credentials |
| 116 | +data "template_file" "config_root" { |
| 117 | + template = "${file("${path.module}/config.tpl")}" |
| 118 | + |
| 119 | + vars { |
| 120 | + profile_name = "${var.namespace}-${var.stage}-admin" |
| 121 | + source_profile = "${var.namespace}" |
| 122 | + region = "${var.aws_region}" |
| 123 | + role_arn = "${aws_iam_role.bootstrap.arn}" |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +# Render the config file with IAM credentials |
| 128 | +data "template_file" "config" { |
| 129 | + count = "${length(module.organization_account_access_role_arns.values)}" |
| 130 | + template = "${file("${path.module}/config.tpl")}" |
| 131 | + |
| 132 | + vars { |
| 133 | + profile_name = "${var.namespace}-${var.accounts_enabled[count.index]}-admin" |
| 134 | + source_profile = "${var.namespace}" |
| 135 | + region = "${var.aws_region}" |
| 136 | + role_arn = "${module.organization_account_access_role_arns.values[count.index]}" |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +# Write the config file to disk |
| 141 | +resource "local_file" "config_file" { |
| 142 | + content = "${join("\n\n", |
| 143 | + concat(list("[profile ${var.namespace}]"), |
| 144 | + list(data.template_file.config_root.rendered), data.template_file.config.*.rendered))}" |
| 145 | + |
| 146 | + filename = "${var.output_path}/${var.config_file}" |
| 147 | +} |
0 commit comments