Error: creating IAM instance profile hello-profile: EntityAlreadyExists: Instance Profile hello-profile already exists.
status code: 409, request id: 197f25f2-d436-42bf-a541-0bf36e3f0eaf
on iam_role_hello.tf line 44, in resource "aws_iam_instance_profile" "hello":
44: resource "aws_iam_instance_profile" "hello" {
terraform을 작성하다가 이런 에러가 나왔다.
이 오류는 hello-profile이라는 IAM Instance Profile이 이미 AWS에 존재하기 때문에 발생한다고 한다. Terraform은 동일한 이름으로 새로운 리소스를 생성하려고 시도했으나, 이름이 중복되어 실패한 것이다.
이미 존재하는 hello-profile을 삭제하거나 Terraform 구성에서 이 리소스를 관리하지 않도록 조치해야 합니다.
AWS CLI로 Instance Profile 확인
aws iam get-instance-profile --instance-profile-name hello-profile
Instance Profile의 세부 정보를 확인합니다.
hello-profile이 필요 없으면 AWS CLI 또는 콘솔에서 삭제하세요.
AWS CLI로 Instance Profile 삭제
aws iam delete-instance-profile --instance-profile-name hello-profile
이미 존재하는 리소스를 Terraform 관리 하에 두거나 Terraform 상태를 업데이트해야 할 수 있습니다.
Terraform이 이미 존재하는 hello-profile을 관리하도록 terraform import 명령어를 사용하세요.
terraform import aws_iam_instance_profile.hello hello-profile
그런 다음 terraform plan을 실행하여 Terraform 상태와 코드가 일치하는지 확인하세요.
코드에서 새로운 이름으로 Instance Profile을 생성하도록 변경합니다.
resource "aws_iam_instance_profile" "hello" {
name = "hello-profile-new" # 새로운 이름
role = aws_iam_role.hello.name
}
Terraform을 다시 적용합니다:
terraform apply
만약 Terraform 상태 파일과 실제 AWS 리소스가 불일치하는 경우 terraform state 명령어를 사용하여 상태를 수정합니다.
Terraform 상태에서 리소스 제거
기존 리소스를 더 이상 Terraform으로 관리하지 않으려면 상태에서 제거하세요.
terraform state rm aws_iam_instance_profile.hello
삭제가 가능하면: AWS에서 기존 리소스를 삭제하고 Terraform을 적용.
관리 상태로 두려면: Terraform으로 리소스를 가져오거나 상태를 정리.
새 이름으로 생성하려면: 코드를 수정하여 이름 충돌을 방지.
위 단계를 수행한 후에도 문제가 있으면 Terraform 구성 또는 추가 세부 사항을 공유해 주세요!