|
| 1 | +## |
| 2 | +# Module to build the Azure DevOps "seed" configuration |
| 3 | +## |
| 4 | + |
| 5 | +# Build an S3 bucket to store TF state |
| 6 | +resource "aws_s3_bucket" "state_bucket" { |
| 7 | + bucket = var.s3_tfstate_bucket |
| 8 | + |
| 9 | + # Tells AWS to encrypt the S3 bucket at rest by default |
| 10 | + server_side_encryption_configuration { |
| 11 | + rule { |
| 12 | + apply_server_side_encryption_by_default { |
| 13 | + sse_algorithm = "AES256" |
| 14 | + } |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + # Prevents Terraform from destroying or replacing this object - a great safety mechanism |
| 19 | + lifecycle { |
| 20 | + prevent_destroy = true |
| 21 | + } |
| 22 | + |
| 23 | + # Tells AWS to keep a version history of the state file |
| 24 | + versioning { |
| 25 | + enabled = true |
| 26 | + } |
| 27 | + |
| 28 | + tags = { |
| 29 | + Terraform = "true" |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +# Build a DynamoDB to use for terraform state locking |
| 34 | +resource "aws_dynamodb_table" "tf_lock_state" { |
| 35 | + name = var.dynamo_db_table_name |
| 36 | + |
| 37 | + # Pay per request is cheaper for low-i/o applications, like our TF lock state |
| 38 | + billing_mode = "PAY_PER_REQUEST" |
| 39 | + |
| 40 | + # Hash key is required, and must be an attribute |
| 41 | + hash_key = "LockID" |
| 42 | + |
| 43 | + # Attribute LockID is required for TF to use this table for lock state |
| 44 | + attribute { |
| 45 | + name = "LockID" |
| 46 | + type = "S" |
| 47 | + } |
| 48 | + |
| 49 | + tags = { |
| 50 | + Name = var.dynamo_db_table_name |
| 51 | + Terraform = "true" |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +# Build an AWS S3 bucket for logging |
| 57 | +resource "aws_s3_bucket" "s3_logging_bucket" { |
| 58 | + bucket = var.s3_logging_bucket_name |
| 59 | + acl = "private" |
| 60 | + |
| 61 | + server_side_encryption_configuration { |
| 62 | + rule { |
| 63 | + apply_server_side_encryption_by_default { |
| 64 | + sse_algorithm = "AES256" |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +# Output name of S3 logging bucket back to main.tf |
| 71 | +output "s3_logging_bucket_id" { |
| 72 | + value = aws_s3_bucket.s3_logging_bucket.id |
| 73 | +} |
| 74 | +output "s3_logging_bucket" { |
| 75 | + value = aws_s3_bucket.s3_logging_bucket.bucket |
| 76 | +} |
| 77 | + |
| 78 | +# Create an IAM role for CodeBuild to assume |
| 79 | +resource "aws_iam_role" "codebuild_iam_role" { |
| 80 | + name = var.codebuild_iam_role_name |
| 81 | + |
| 82 | + assume_role_policy = <<EOF |
| 83 | +{ |
| 84 | + "Version": "2012-10-17", |
| 85 | + "Statement": [ |
| 86 | + { |
| 87 | + "Effect": "Allow", |
| 88 | + "Principal": { |
| 89 | + "Service": "codebuild.amazonaws.com" |
| 90 | + }, |
| 91 | + "Action": "sts:AssumeRole" |
| 92 | + } |
| 93 | + ] |
| 94 | +} |
| 95 | +EOF |
| 96 | +} |
| 97 | + |
| 98 | +# Output the CodeBuild IAM role |
| 99 | +output "codebuild_iam_role_arn" { |
| 100 | + value = aws_iam_role.codebuild_iam_role.arn |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +# Create an IAM role policy for CodeBuild to use implicitly |
| 105 | +resource "aws_iam_role_policy" "codebuild_iam_role_policy" { |
| 106 | + name = var.codebuild_iam_role_policy_name |
| 107 | + role = aws_iam_role.codebuild_iam_role.name |
| 108 | + |
| 109 | + policy = <<POLICY |
| 110 | +{ |
| 111 | + "Version": "2012-10-17", |
| 112 | + "Statement": [ |
| 113 | + { |
| 114 | + "Effect": "Allow", |
| 115 | + "Resource": [ |
| 116 | + "*" |
| 117 | + ], |
| 118 | + "Action": [ |
| 119 | + "logs:CreateLogGroup", |
| 120 | + "logs:CreateLogStream", |
| 121 | + "logs:PutLogEvents" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "Effect": "Allow", |
| 126 | + "Action": [ |
| 127 | + "s3:*" |
| 128 | + ], |
| 129 | + "Resource": [ |
| 130 | + "${aws_s3_bucket.s3_logging_bucket.arn}", |
| 131 | + "${aws_s3_bucket.s3_logging_bucket.arn}/*", |
| 132 | + "${aws_s3_bucket.state_bucket.arn}", |
| 133 | + "${aws_s3_bucket.state_bucket.arn}/*", |
| 134 | + "arn:aws:s3:::codepipeline-us-east-1*", |
| 135 | + "arn:aws:s3:::codepipeline-us-east-1*/*", |
| 136 | + "${var.tf_codepipeline_artifact_bucket_arn}", |
| 137 | + "${var.tf_codepipeline_artifact_bucket_arn}/*" |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "Effect": "Allow", |
| 142 | + "Action": [ |
| 143 | + "dynamodb:*" |
| 144 | + ], |
| 145 | + "Resource": "${aws_dynamodb_table.tf_lock_state.arn}" |
| 146 | + }, |
| 147 | + { |
| 148 | + "Effect": "Allow", |
| 149 | + "Action": [ |
| 150 | + "codecommit:BatchGet*", |
| 151 | + "codecommit:BatchDescribe*", |
| 152 | + "codecommit:Describe*", |
| 153 | + "codecommit:EvaluatePullRequestApprovalRules", |
| 154 | + "codecommit:Get*", |
| 155 | + "codecommit:List*", |
| 156 | + "codecommit:GitPull" |
| 157 | + ], |
| 158 | + "Resource": "${var.terraform_codecommit_repo_arn}" |
| 159 | + }, |
| 160 | + { |
| 161 | + "Effect": "Allow", |
| 162 | + "Action": [ |
| 163 | + "iam:Get*", |
| 164 | + "iam:List*" |
| 165 | + ], |
| 166 | + "Resource": "${aws_iam_role.codebuild_iam_role.arn}" |
| 167 | + }, |
| 168 | + { |
| 169 | + "Effect": "Allow", |
| 170 | + "Action": "sts:AssumeRole", |
| 171 | + "Resource": "${aws_iam_role.codebuild_iam_role.arn}" |
| 172 | + } |
| 173 | + ] |
| 174 | +} |
| 175 | +POLICY |
| 176 | +} |
| 177 | + |
| 178 | + |
| 179 | +# Create IAM role for Terraform builder to assume |
| 180 | +resource "aws_iam_role" "tf_iam_assumed_role" { |
| 181 | + name = "TerraformAssumedIamRole" |
| 182 | + |
| 183 | + assume_role_policy = <<EOF |
| 184 | +{ |
| 185 | + "Version": "2012-10-17", |
| 186 | + "Statement": [ |
| 187 | + { |
| 188 | + "Effect": "Allow", |
| 189 | + "Principal": { |
| 190 | + "AWS": "${aws_iam_role.codebuild_iam_role.arn}" |
| 191 | + }, |
| 192 | + "Action": "sts:AssumeRole" |
| 193 | + } |
| 194 | + ] |
| 195 | +} |
| 196 | +EOF |
| 197 | + |
| 198 | + lifecycle { |
| 199 | + prevent_destroy = true |
| 200 | + } |
| 201 | + |
| 202 | + tags = { |
| 203 | + Terraform = "true" |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | + |
| 208 | +# Create broad IAM policy Terraform to use to build, modify resources |
| 209 | +resource "aws_iam_policy" "tf_iam_assumed_policy" { |
| 210 | + name = "TerraformAssumedIamPolicy" |
| 211 | + |
| 212 | + policy = <<EOF |
| 213 | +{ |
| 214 | + "Version": "2012-10-17", |
| 215 | + "Statement": [ |
| 216 | + { |
| 217 | + "Sid": "AllowAllPermissions", |
| 218 | + "Effect": "Allow", |
| 219 | + "Action": [ |
| 220 | + "*" |
| 221 | + ], |
| 222 | + "Resource": "*" |
| 223 | + } |
| 224 | + ] |
| 225 | +} |
| 226 | +EOF |
| 227 | + |
| 228 | + lifecycle { |
| 229 | + prevent_destroy = true |
| 230 | + } |
| 231 | +} |
| 232 | + |
| 233 | +# Attach IAM assume role to policy |
| 234 | +resource "aws_iam_role_policy_attachment" "tf_iam_attach_assumed_role_to_permissions_policy" { |
| 235 | + role = aws_iam_role.tf_iam_assumed_role.name |
| 236 | + policy_arn = aws_iam_policy.tf_iam_assumed_policy.arn |
| 237 | + |
| 238 | + lifecycle { |
| 239 | + prevent_destroy = true |
| 240 | + } |
| 241 | +} |
0 commit comments