Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add managed policy exclusivity, replace deprecated manged_policy_arn value #496

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ env/
.vscode
*.code-workspace
*.sha256
terraform.tfstate
terraform.tfstate.backup

9 changes: 6 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,16 @@ resource "aws_iam_role" "this" {
}
)

managed_policy_arns = concat(["arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"], var.instance_profile_policies)

tags = merge(local.tags, {
Name = "${var.iam_resource_names_prefix}-role-${var.name}"
})
}

# Attach instance profile policies
resource "aws_iam_role_policy_attachment" "this" {
for_each = { for k, v in var.instance_profile_policies : k => v }
role = aws_iam_role.this.name
policy_arn = each.value
}
data "aws_iam_policy_document" "ssm_params_and_secrets" {
count = var.ssm_parameters != null || var.secretsmanager_secrets != null ? 1 : 0
dynamic "statement" {
Expand Down
9 changes: 5 additions & 4 deletions test/unit-test/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ data "http" "environments_file" {
}
locals {

# create list of common managed policies that can be attached to ec2 instance profiles
ec2_common_managed_policies = [
aws_iam_policy.ec2_test_common_policy.arn
]
# create map of common managed policies that can be attached to ec2 instance profiles
ec2_common_managed_policies = {
AmazonSSMManagedInstanceCore = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
ec2-test-common-policy = aws_iam_policy.ec2_test_common_policy.arn
}

tags = {
component = "test"
Expand Down
5 changes: 4 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ variable "iam_resource_names_prefix" {
}

variable "instance_profile_policies" {
type = list(string)
type = map(string)
default = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this value be moved up to the local value? As written, this PR would only add the AmazonSSMManagedInstanceCore policy when instance_profile_policies is not set. I'd like to preserve the original intent if possible by including this value directly into the local that the for expression is constructed from. Unless, of course, there's something I've missed?

AmazonSSMManagedInstanceCore = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
description = "A list of managed IAM policy document ARNs to be attached to the instance profile"
}

Expand Down
Loading