Skip to content

Commit b31cd72

Browse files
authored
[breaking] [feature] aws-iam-instance-profile reuse existing role and remove SSM support (#183)
* Allows passing an existing role to aws-iam-instance-profile, and the module will attach whatever permissions are needed. This allows using roles that are created and managed by other modules (e.g. EKS worker role), but provides ability to attach a standard set of permissions to that role. It will still always create the instance profile, even if the profile will be unused. * Removes unused permissions from the aws-iam-instance-profile module for using SSM.
1 parent 8d7f1ff commit b31cd72

File tree

5 files changed

+23
-130
lines changed

5 files changed

+23
-130
lines changed

aws-iam-instance-profile/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ resource "aws_instance" "instance" {
3939

4040
| Name | Description | Type | Default | Required |
4141
|------|-------------|------|---------|:-----:|
42-
| enable\_ssm | Attach the appropriate policies to allow the instance to integrate with AWS Systems Manager. | `string` | `"true"` | no |
42+
| create\_role | Creates a role for use with the instance profile. | `bool` | `true` | no |
43+
| existing\_role\_name | Use existing role with the given name instead of creating a new role. Attaches all standard policies to given role. Only used if create\_role is false. | `string` | n/a | yes |
4344
| iam\_path | The IAM path to the role. | `string` | `"/"` | no |
4445
| name\_prefix | Creates a unique name for both the role and instance profile beginning with the specified prefix. Max 32 characters long. | `string` | n/a | yes |
4546
| role\_description | The description of the IAM role. | `string` | `""` | no |

aws-iam-instance-profile/main.tf

+7-105
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
locals {
2+
role_name = coalescelist(aws_iam_role.role[*].name, [var.existing_role_name])[0]
3+
}
4+
15
data "aws_iam_policy_document" "assume-role" {
26
statement {
37
sid = "AssumeRole"
@@ -11,6 +15,7 @@ data "aws_iam_policy_document" "assume-role" {
1115
}
1216

1317
resource "aws_iam_role" "role" {
18+
count = var.create_role ? 1 : 0
1419
name_prefix = var.name_prefix
1520
description = var.role_description
1621
path = var.iam_path
@@ -21,120 +26,17 @@ resource "aws_iam_role" "role" {
2126
}
2227
}
2328

24-
resource "aws_iam_role_policy" "ssm" {
25-
count = var.enable_ssm ? 1 : 0
26-
role = aws_iam_role.role.name
27-
policy = data.aws_iam_policy_document.ssm_policy.json
28-
}
29-
3029
resource "aws_iam_role_policy_attachment" "cloudwatch-agent" {
31-
role = aws_iam_role.role.name
30+
role = local.role_name
3231
policy_arn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
3332
}
3433

3534
resource "aws_iam_instance_profile" "profile" {
3635
name_prefix = var.name_prefix
3736
path = var.iam_path
38-
role = aws_iam_role.role.name
37+
role = local.role_name
3938

4039
lifecycle {
4140
ignore_changes = [name, name_prefix, path]
4241
}
4342
}
44-
45-
data "aws_iam_policy_document" "ssm_policy" {
46-
statement {
47-
actions = [
48-
"ssm:DescribeAssociation",
49-
"ssm:GetDeployablePatchSnapshotForInstance",
50-
"ssm:GetDocument",
51-
"ssm:DescribeDocument",
52-
"ssm:GetManifest",
53-
"ssm:GetParameters",
54-
"ssm:ListAssociations",
55-
"ssm:ListInstanceAssociations",
56-
"ssm:PutInventory",
57-
"ssm:PutComplianceItems",
58-
"ssm:PutConfigurePackageResult",
59-
"ssm:UpdateAssociationStatus",
60-
"ssm:UpdateInstanceAssociationStatus",
61-
"ssm:UpdateInstanceInformation",
62-
]
63-
64-
resources = ["*"]
65-
}
66-
67-
statement {
68-
actions = [
69-
"ssmmessages:CreateControlChannel",
70-
"ssmmessages:CreateDataChannel",
71-
"ssmmessages:OpenControlChannel",
72-
"ssmmessages:OpenDataChannel",
73-
]
74-
75-
resources = ["*"]
76-
}
77-
78-
statement {
79-
actions = [
80-
"ec2messages:AcknowledgeMessage",
81-
"ec2messages:DeleteMessage",
82-
"ec2messages:FailMessage",
83-
"ec2messages:GetEndpoint",
84-
"ec2messages:GetMessages",
85-
"ec2messages:SendReply",
86-
]
87-
88-
resources = ["*"]
89-
}
90-
91-
statement {
92-
actions = ["cloudwatch:PutMetricData"]
93-
94-
resources = ["*"]
95-
}
96-
97-
statement {
98-
actions = ["ec2:DescribeInstanceStatus"]
99-
100-
resources = ["*"]
101-
}
102-
103-
statement {
104-
actions = [
105-
"ds:CreateComputer",
106-
"ds:DescribeDirectories",
107-
]
108-
109-
resources = ["*"]
110-
}
111-
112-
statement {
113-
actions = [
114-
"logs:CreateLogGroup",
115-
"logs:CreateLogStream",
116-
"logs:DescribeLogGroups",
117-
"logs:DescribeLogStreams",
118-
"logs:PutLogEvents",
119-
]
120-
121-
resources = ["*"]
122-
}
123-
124-
statement {
125-
actions = [
126-
"s3:GetObject",
127-
"s3:ListBucket",
128-
]
129-
130-
//https://docs.aws.amazon.com/systems-manager/latest/userguide/ssm-agent-minimum-s3-permissions.html
131-
resources = [
132-
"arn:aws:s3:::aws-ssm-*/*",
133-
"arn:aws:s3:::aws-windows-downloads-*/*",
134-
"arn:aws:s3:::amazon-ssm-*/*",
135-
"arn:aws:s3:::amazon-ssm-packages-*/*",
136-
"arn:aws:s3:::*-birdwatcher-prod/*",
137-
"arn:aws:s3:::patch-baseline-snapshot-*/*",
138-
]
139-
}
140-
}

aws-iam-instance-profile/module_test.go

-16
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,3 @@ func TestAWSIAMInstanceProfile(t *testing.T) {
2222

2323
testutil.Run(t, terraformOptions)
2424
}
25-
26-
func TestAWSIAMInstanceProfileDisableSSM(t *testing.T) {
27-
terraformOptions := testutil.Options(
28-
testutil.IAMRegion,
29-
map[string]interface{}{
30-
"name_prefix": random.UniqueId(),
31-
"iam_path": "/foo/",
32-
"role_description": random.UniqueId(),
33-
"enable_ssm": "false",
34-
},
35-
)
36-
37-
defer terraform.Destroy(t, terraformOptions)
38-
39-
testutil.Run(t, terraformOptions)
40-
}

aws-iam-instance-profile/outputs.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
output "role_arn" {
2-
value = aws_iam_role.role.arn
2+
value = coalescelist(aws_iam_role.role[*].arn, [""])[0]
33
description = "The Amazon Resource Name (ARN) specifying the role."
44
}
55

66
output "role_name" {
7-
value = aws_iam_role.role.name
7+
value = local.role_name
88
description = "The name of the role."
99
}
1010

aws-iam-instance-profile/variables.tf

+12-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ variable "name_prefix" {
33
description = "Creates a unique name for both the role and instance profile beginning with the specified prefix. Max 32 characters long."
44
}
55

6+
variable "existing_role_name" {
7+
type = string
8+
description = "Use existing role with the given name instead of creating a new role. Attaches all standard policies to given role. Only used if create_role is false."
9+
default = null
10+
}
11+
12+
variable "create_role" {
13+
type = bool
14+
description = "Creates a role for use with the instance profile."
15+
default = true
16+
}
17+
618
variable "iam_path" {
719
type = string
820
default = "/"
@@ -14,9 +26,3 @@ variable "role_description" {
1426
description = "The description of the IAM role."
1527
default = ""
1628
}
17-
18-
variable "enable_ssm" {
19-
type = string
20-
description = "Attach the appropriate policies to allow the instance to integrate with AWS Systems Manager."
21-
default = "true"
22-
}

0 commit comments

Comments
 (0)