Skip to content

Commit 55828c8

Browse files
author
arnol377
committed
working on workspace cleanup
1 parent a6fa698 commit 55828c8

File tree

5 files changed

+37
-27
lines changed

5 files changed

+37
-27
lines changed

main.tf

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,11 @@ module "s3_artifacts_bucket" {
2929
}
3030
}
3131

32-
# call build_user module
33-
module "build_user" {
34-
count = local.build_user_iam_policy == null ? 0 : 1
35-
source = "./modules/build_user"
36-
project_name = var.project_name
37-
account_id = local.account_id
38-
region = local.region
39-
build_user_iam_policy = local.build_user_iam_policy
32+
# Move away from conditional build_user module to use static IAM role
33+
locals {
34+
build_user_role_arn = aws_iam_role.build_user_role.arn
4035
}
4136

42-
4337
module "codepipeline_kms" {
4438
source = "./modules/kms"
4539
codepipeline_role_arn = module.codepipeline_iam_role.role_arn
@@ -107,7 +101,15 @@ module "codepipeline_iam_role" {
107101
}
108102
}
109103

110-
104+
module "build_user" {
105+
source = "./modules/build_user"
106+
count = var.create_build_user ? 1 : 0
107+
project_name = var.project_name
108+
region = local.region
109+
account_id = local.account_id
110+
build_user_iam_policy = local.build_user_iam_policy
111+
secret_arns = var.secret_arns
112+
}
111113

112114
# Module for Infrastructure Validate, Plan, Apply and Destroy - CodePipeline
113115
module "codepipeline_terraform" {
@@ -152,7 +154,7 @@ resource "aws_iam_role" "build_user_role" {
152154
Action = "sts:AssumeRole"
153155
Effect = "Allow"
154156
Principal = {
155-
Service = "ec2.amazonaws.com"
157+
Service = ["ec2.amazonaws.com", "codebuild.amazonaws.com"]
156158
}
157159
}
158160
]

modules/build_user/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ variable "build_user_iam_policy" {
1919
description = "The IAM policy for the build user."
2020
type = string
2121
}
22+
23+
variable "secret_arns" {
24+
description = "List of secret ARNs that the build user needs access to"
25+
type = list(string)
26+
default = null
27+
}

modules/iam-role/main.tf

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
#This AWS Content is provided subject to the terms of the AWS Customer Agreement available at
44
#http://aws.amazon.com/agreement or other written agreement between Customer and either
55
#Amazon Web Services, Inc. or Amazon Web Services EMEA SARL or both.
6-
data "aws_s3_bucket" "assets" {
7-
for_each = toset(concat(
8-
var.goss_bucket == null ? [] : [var.goss_bucket.name],
9-
var.ansible_bucket == null ? [] : [var.ansible_bucket.name],
10-
var.packer_bucket == null ? [] : [var.packer_bucket.name]
11-
))
12-
bucket = each.value
13-
}
146

157
data "aws_iam_policy_document" "codepipeline_assume_role" {
168
# iam:GetInstanceProfile
@@ -51,6 +43,9 @@ locals {
5143
var.goss_repo == null ? [] : [var.goss_repo.arn]
5244
)
5345
codecommit_repo_count = length(local.codecommit_repos)
46+
47+
# Construct bucket ARNs directly since we know the bucket name
48+
assets_bucket_arn = "arn:${data.aws_partition.current.partition}:s3:::${var.goss_bucket.name}"
5449
}
5550

5651
data "aws_iam_policy_document" "codepipeline_policy" {
@@ -73,13 +68,13 @@ data "aws_iam_policy_document" "codepipeline_policy" {
7368
"arn:${data.aws_partition.current.partition}:s3:::${var.state.bucket}/*"
7469
],
7570
var.goss_bucket == null ? [] : [
76-
"${lookup(data.aws_s3_bucket.assets, var.goss_bucket.name).arn}/*"
71+
"${local.assets_bucket_arn}/*"
7772
],
7873
var.ansible_bucket == null ? [] : [
79-
"${lookup(data.aws_s3_bucket.assets, var.ansible_bucket.name).arn}/*"
74+
"${local.assets_bucket_arn}/*"
8075
],
8176
var.packer_bucket == null ? [] : [
82-
"${lookup(data.aws_s3_bucket.assets, var.packer_bucket.name).arn}/*"
77+
"${local.assets_bucket_arn}/*"
8378
]))
8479
}
8580
statement {

outputs.tf

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ output "iam_arn" {
3131

3232
output "kms_arn" {
3333
value = module.codepipeline_kms.arn
34-
description = "The ARN of the KMS key used in the codepipeline"
34+
description = "The KMS key ARN used in the codepipeline"
3535
}
3636

3737
output "s3_arn" {
@@ -44,10 +44,6 @@ output "s3_bucket" {
4444
description = "The Name of the S3 Bucket"
4545
}
4646

47-
output "user" {
48-
value = one(module.build_user).user
49-
}
50-
5147
output "sec_group" {
5248
value = aws_security_group.packer
5349
}
@@ -60,3 +56,8 @@ output "secrets" {
6056
value = aws_secretsmanager_secret.secrets
6157
}
6258

59+
output "role_name" {
60+
value = aws_iam_role.build_user_role.name
61+
description = "The name of the IAM role used for build and pipeline operations"
62+
}
63+

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,9 @@ variable "image_volume_mapping" {
394394
}))
395395
default = []
396396
}
397+
398+
variable "create_build_user" {
399+
description = "Whether to create a build user. Set to false if you want to use an existing user."
400+
type = bool
401+
default = true
402+
}

0 commit comments

Comments
 (0)