Hi Team
I’m New bee to terraform recently started to write a terraform files, i got struck while adding the existing (it’s been created before in AWS console )IAM role to EC2 instance.
I thought instead of create new using terraform i want to use existing one.
I tried below format to attach exiting role to my code but it’s not working, can you help me how to attach it.
#Attach existing IAM Role to EC2 instance
resource "aws_iam_instance_profile" "UC-profile" {
name = "UC-profile"
role = "AWS-SSM-Role-Connect"
}
resource "aws_instance" "webserver-1" {
ami = "ami-01410ca4af28f22b0"
instance_type = "r5.xlarge"
vpc_security_group_ids = ["${aws_security_group.sg-Uc2.id}"]
subnet_id = "${element(aws_subnet.public.*.id,0)}"
associate_public_ip_address = false
iam_instance_profile = aws_iam_instance_profile.UC-profile.name
tags = {
Name = "web-1"
}
}