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

Parametrize EC2 Instance Module based on IAM Role #81

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: 1 addition & 1 deletion modules/aws/EC2-Instance/ec2_instance.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ resource "aws_instance" "ec2_instance" {

tags = local.ec2_tags

iam_instance_profile = aws_iam_instance_profile.iam_instance_profile.name
iam_instance_profile = aws_iam_instance_profile.iam_instance_profile[0].name

network_interface {
device_index = 0
Expand Down
9 changes: 5 additions & 4 deletions modules/aws/EC2-Instance/iam_role.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ resource "aws_iam_policy" "session_manager_policy" {

resource "aws_iam_role" "iam_role" {
name = join("-", [var.project, var.application, var.environment, var.region, "ec2-iam-role"])

count = var.ec2_iam_role_arn != null ? 0 : 1
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
Expand All @@ -56,19 +56,20 @@ POLICY

resource "aws_iam_instance_profile" "iam_instance_profile" {
name = join("-", [var.project, var.application, var.environment, var.region, "ec2-instance-profile"])
role = aws_iam_role.iam_role.name
count = var.ec2_iam_role_arn != null ? 0 : 1
role = aws_iam_role.iam_role[0].name

depends_on = [aws_iam_role.iam_role]
}

resource "aws_iam_role_policy_attachment" "instance_connect" {
count = var.enable_instance_connect == true ? 1 : 0
role = aws_iam_role.iam_role.name
role = aws_iam_role.iam_role[0].name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonSSMManagedInstanceCore"
}

resource "aws_iam_role_policy_attachment" "session_manager" {
count = var.enable_session_manager == true ? 1 : 0
role = aws_iam_role.iam_role.name
role = aws_iam_role.iam_role[0].name
policy_arn = aws_iam_policy.session_manager_policy[0].arn
}
4 changes: 4 additions & 0 deletions modules/aws/EC2-Instance/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,7 @@ variable "user_data" {
description = "User data to be passed to the EC2 instance"
default = null
}
variable "ec2_iam_role_arn" {
type = string
description = "IAM Role ARN for the EC2"
}