Skip to content

Added vars to reuse existing IAM role instead of creating a new one #388

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

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ data "aws_iam_policy_document" "assume_role_policy" {
}

resource "aws_iam_role" "this" {
count = var.create && var.create_iam_instance_profile ? 1 : 0
count = var.create && var.create_iam_instance_profile && !var.use_existing_iam_role ? 1 : 0

name = var.iam_role_use_name_prefix ? null : local.iam_role_name
name_prefix = var.iam_role_use_name_prefix ? "${local.iam_role_name}-" : null
Expand All @@ -582,7 +582,7 @@ resource "aws_iam_role" "this" {
}

resource "aws_iam_role_policy_attachment" "this" {
for_each = { for k, v in var.iam_role_policies : k => v if var.create && var.create_iam_instance_profile }
for_each = { for k, v in var.iam_role_policies : k => v if var.create && var.create_iam_instance_profile && !var.use_existing_iam_role }

policy_arn = each.value
role = aws_iam_role.this[0].name
Expand All @@ -591,7 +591,7 @@ resource "aws_iam_role_policy_attachment" "this" {
resource "aws_iam_instance_profile" "this" {
count = var.create && var.create_iam_instance_profile ? 1 : 0

role = aws_iam_role.this[0].name
role = var.use_existing_iam_role ? var.existing_iam_role_name : aws_iam_role.this[0].name

name = var.iam_role_use_name_prefix ? null : local.iam_role_name
name_prefix = var.iam_role_use_name_prefix ? "${local.iam_role_name}-" : null
Expand Down
12 changes: 12 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,18 @@ variable "create_iam_instance_profile" {
default = false
}

variable "use_existing_iam_role" {
description = "Whether to use an existing IAM role instead of creating a new one."
type = bool
default = false
}

variable "existing_iam_role_name" {
description = "Name of the existing IAM role to use for the instance profile."
type = string
default = null
}

variable "iam_role_name" {
description = "Name to use on IAM role created"
type = string
Expand Down
Loading