terraform-aws-modules / terraform-aws-ec2-instance

Terraform module to create AWS EC2 instance(s) resources πŸ‡ΊπŸ‡¦
https://registry.terraform.io/modules/terraform-aws-modules/ec2-instance/aws
Apache License 2.0
754 stars 1.87k forks source link

Is there way to attach a existent IAM Role to EC2? #316

Closed weyderfs closed 1 year ago

weyderfs commented 1 year ago

Description

I've read and look at code but not found a way to set the attachment of a existent iam > role + policy using this module. in my perception always will be create a new.

Is there a way to use a existent?

If your request is for a new feature, please use the Feature request template.

camerondolan commented 1 year ago

Don't know if you still need this - but you can do this using iam_instance_profile

So create your role, then create an aws_iam_instance_profile resource. Like so:

iam policy ect...

resource "aws_iam_role" "ec2_role" {
  role            = "myRole"
  policy_arn = aws_iam_policy.myPolicy.arn
}

resource "aws_iam_instance_profile" "ec2_profile" {
  name = "instanceProfileName"
  role    = aws_iam_role.ec2_role.name
}

Then in the ec2 module add the property for aws_iam_instance_profile and set it like so: iam_instance_profile = aws_iam_instance_profile.ec2_profile.name

See: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile iam_instance_profile

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days

github-actions[bot] commented 1 year ago

This issue was automatically closed because of stale in 10 days

github-actions[bot] commented 1 year ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.