Skip to content

Commit 71e2936

Browse files
authored
Merge pull request #25 from opszero/jana/add-instance-role
Add instance profile for bastion instance
2 parents 23e94f4 + f864567 commit 71e2936

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

main.tf

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ resource "aws_instance" "this" {
4141
associate_public_ip_address = true
4242
subnet_id = var.subnet_id
4343
vpc_security_group_ids = concat(var.security_group_ids, [aws_security_group.this.id])
44+
iam_instance_profile = var.instance_profile != null ? aws_iam_instance_profile.this.arn : null
4445

4546
monitoring = true
4647

@@ -74,35 +75,36 @@ resource "aws_cloudwatch_metric_alarm" "aws_bastion_cpu_threshold" {
7475
}
7576

7677
resource "aws_iam_instance_profile" "this" {
77-
for_each = var.instance_profiles
78-
79-
name = each.key
80-
role = each.value.role
78+
count = var.instance_profile != null ? 1 : 0
79+
name = "${var.instance_profile.role_name}-profile"
80+
role = aws_iam_role.this.name
8181

8282
depends_on = [
8383
aws_iam_role.this
8484
]
8585
}
8686

8787
resource "aws_iam_role" "this" {
88-
for_each = var.instance_profiles
89-
90-
name = each.value.role
91-
path = "/"
92-
93-
assume_role_policy = <<EOF
94-
{
95-
"Version": "2012-10-17",
96-
"Statement": [
97-
{
98-
"Action": "sts:AssumeRole",
99-
"Principal": {
100-
"Service": "${each.value.assume_role_service}"
101-
},
102-
"Effect": "Allow",
103-
"Sid": ""
104-
}
105-
]
88+
count = var.instance_profile != null ? 1 : 0
89+
name = var.instance_profile.role_name
90+
path = "/"
91+
92+
assume_role_policy = jsonencode({
93+
"Version" : "2012-10-17",
94+
"Statement" : [{
95+
"Action" : "sts:AssumeRole",
96+
"Principal" : {
97+
"Service" : var.instance_profile.assume_role_service
98+
},
99+
"Effect" : "Allow",
100+
"Sid" : ""
101+
}]
102+
})
106103
}
107-
EOF
104+
105+
resource "aws_iam_role_policy_attachment" "this" {
106+
count = length(var.instance_profile.policy_arns) > 0 ? length(var.instance_profile.policy_arns) : 0
107+
108+
policy_arn = var.instance_profile.policy_arns[count.index]
109+
role = aws_iam_role.this.name
108110
}

variables.tf

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,17 @@ variable "efs_mounts" {
9999
#}
100100
}
101101

102-
variable "instance_profiles" {
103-
default = {}
104-
#"test_profile1" = {
105-
# role = "test_role1",
106-
# assume_role_service = "ec2.amazonaws.com"
107-
#},
108-
#"test_profile2" = {
109-
# role = "test_role2",
110-
# assume_role_service = "s3.amazonaws.com"
111-
#}
102+
variable "instance_profile" {
103+
type = object({
104+
role_name = string
105+
assume_role_service = string
106+
policy_arns = list(string)
107+
})
108+
default = null
109+
# default = {
110+
# role_name = "test_role1"
111+
# assume_role_service = "ec2.amazonaws.com"
112+
# policy_arns = ["testarn1", "testarn2"]
113+
# }
112114
}
113115

0 commit comments

Comments
 (0)