Skip to content

Commit 486f21d

Browse files
committed
Refactor Terraform configuration to introduce IAM role and instance profile for build user, and update bucket handling in locals
1 parent 905181d commit 486f21d

File tree

7 files changed

+63
-23
lines changed

7 files changed

+63
-23
lines changed

locals.tf

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
locals {
88
account_id = data.aws_caller_identity.current.account_id
99
region = data.aws_region.current.name
10+
buckets = distinct([
11+
module.s3_artifacts_bucket.bucket,
12+
var.assets_bucket_name,
13+
var.packer_bucket.name,
14+
var.ansible_bucket.name,
15+
var.goss_bucket.name,
16+
var.state.bucket
17+
])
1018
}
1119

1220
data "aws_iam_policy_document" "build_user_default" {
@@ -46,10 +54,14 @@ data "aws_iam_policy_document" "build_user_default" {
4654
actions = [
4755
"s3:*"
4856
]
49-
resources = concat([
50-
"arn:${data.aws_partition.current.partition}:s3:::${module.s3_artifacts_bucket.bucket}/*"
57+
resources = concat(
58+
[
59+
for bucket in local.buckets : "arn:${data.aws_partition.current.partition}:s3:::${bucket}"
5160
],
52-
var.s3_bucket_arns == null ? [] : var.s3_bucket_arns)
61+
[
62+
for bucket in local.buckets : "arn:${data.aws_partition.current.partition}:s3:::${bucket}/*"
63+
]
64+
)
5365
}
5466
}
5567

main.tf

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ module "build_user" {
3737
account_id = local.account_id
3838
region = local.region
3939
build_user_iam_policy = local.build_user_iam_policy
40-
iam_instance_profile = aws_iam_instance_profile.build_user_instance_profile.name
4140
}
4241

4342

@@ -142,3 +141,31 @@ module "codepipeline_terraform" {
142141
Region = local.region
143142
}
144143
}
144+
145+
resource "aws_iam_role" "build_user_role" {
146+
name = "${var.project_name}-build-user-role"
147+
148+
assume_role_policy = jsonencode({
149+
Version = "2012-10-17"
150+
Statement = [
151+
{
152+
Action = "sts:AssumeRole"
153+
Effect = "Allow"
154+
Principal = {
155+
Service = "ec2.amazonaws.com"
156+
}
157+
}
158+
]
159+
})
160+
161+
tags = {
162+
Project_Name = var.project_name
163+
Account_ID = local.account_id
164+
Region = local.region
165+
}
166+
}
167+
168+
resource "aws_iam_instance_profile" "build_user_instance_profile" {
169+
name = "${var.project_name}-instance-profile"
170+
role = aws_iam_role.build_user_role.name
171+
}

modules/build_user/main.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,3 @@ resource "aws_secretsmanager_secret_version" "credentials" {
3434
aws_access_key_id = aws_iam_access_key.build_user.id
3535
})
3636
}
37-
38-
resource "aws_iam_instance_profile" "build_user_instance_profile" {
39-
name = "${var.project_name}-instance-profile"
40-
role = aws_iam_user.build_user.name
41-
}

modules/codebuild/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ locals {
3333
# value is a map with keys vars, environment_variables, and buildspec.
3434
# This map is assigned to the build_projects local value.
3535
_build_projects = var.docker_build ? concat([
36-
for project in var.build_projects : project if ! contains(["test", "build"], project.name)
36+
for project in var.build_projects : project if !contains(["test", "build"], project.name)
3737
],
3838
[
3939
{

modules/codebuild/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ variable "role_arn" {
1515
default = ""
1616
}
1717

18-
variable assets_bucket_name {
18+
variable "assets_bucket_name" {
1919
description = "Name of the S3 bucket used to store the deployment artifacts"
2020
type = string
2121
default = "image-pipeline-assets"

parameters_and_secrets.tf

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ locals {
44
# This includes configurations like region, subnets, security group IDs, VPC ID, source AMI, and more.
55
# Conditional logic is used to include optional parameters only if they are provided.
66
parameters = tomap(merge({
7-
aws_account_id = data.aws_caller_identity.current.account_id, # AWS account ID where resources will be provisioned.
8-
region = local.vpc_config.region, # AWS region where resources will be provisioned.
9-
subnets = join(",", local.vpc_config.subnets), # Comma-separated list of subnet IDs.
10-
security_group_ids = join(",", local.vpc_config.security_group_ids), # Comma-separated list of security group IDs.
11-
vpc_id = local.vpc_config.vpc_id, # VPC ID where resources will be provisioned.
12-
goss_profile = var.goss_profile, # GOSS profile for server testing.
13-
goss_binary = var.goss_binary, # GOSS binary for server testing.
14-
playbook = var.playbook, # Ansible playbook for configuration management.
15-
troubleshoot = var.troubleshoot, # Enable troubleshooting mode.
7+
instance_profile = var.instance_profile == null ? "${var.project_name}-instance-profile" : var.instance_profile # IAM instance profile for the EC2 instance.
8+
aws_account_id = data.aws_caller_identity.current.account_id, # AWS account ID where resources will be provisioned.
9+
region = local.vpc_config.region, # AWS region where resources will be provisioned.
10+
subnets = join(",", local.vpc_config.subnets), # Comma-separated list of subnet IDs.
11+
security_group_ids = join(",", local.vpc_config.security_group_ids), # Comma-separated list of security group IDs.
12+
vpc_id = local.vpc_config.vpc_id, # VPC ID where resources will be provisioned.
13+
goss_profile = var.goss_profile, # GOSS profile for server testing.
14+
goss_binary = var.goss_binary, # GOSS binary for server testing.
15+
playbook = var.playbook, # Ansible playbook for configuration management.
16+
troubleshoot = var.troubleshoot, # Enable troubleshooting mode.
1617
# Mapping of volumes to attach to the instance.
1718
volume_map = jsonencode(var.image_volume_mapping)
1819

@@ -74,7 +75,7 @@ locals {
7475
nonsensitive_parameters = tomap(
7576
{ for k, v in local.ssm_parameters :
7677
(issensitive(k) ? nonsensitive(k) : k) => (issensitive(v) ? nonsensitive(v) : v)
77-
if ! contains(var.nonmanaged_parameters, issensitive(k) ? nonsensitive(k) : k)
78+
if !contains(var.nonmanaged_parameters, issensitive(k) ? nonsensitive(k) : k)
7879
}
7980
)
8081
}

variables.tf

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,13 @@ variable "required_packages" {
384384
default = []
385385
}
386386

387-
variable assets_bucket_name {
387+
variable "assets_bucket_name" {
388388
description = "Name of the S3 bucket used to store the deployment artifacts"
389389
type = string
390390
default = "image-pipeline-assets"
391-
}
391+
}
392+
393+
variable "instance_profile" {
394+
type = string
395+
default = null
396+
}

0 commit comments

Comments
 (0)