퍼블릭 EC2와 프라이빗 EC2의 IAM 역할 공유

노출 수준과 업무가 다른 EC2의 실제 IAM 권한을 분리하세요.

설명

EC2 워크로드마다 필요한 AWS 작업과 리소스를 확인하세요. 공개 인스턴스와 내부 인스턴스의 권한이 다르면 역할과 인스턴스 프로파일을 분리하세요.

잠재적 영향

공유 역할은 공개 인스턴스에도 내부 데이터 접근 권한을 줄 수 있습니다. 외부 접속 가능 여부는 서브넷 이름뿐 아니라 실제 주소, 경로와 보안 그룹에 달려 있습니다.

해결 방법

프로파일마다 필요한 권한의 역할을 연결하세요. 변경 후 각 애플리케이션이 필요한 리소스에는 접근하고 다른 리소스에는 접근할 수 없는지 확인하세요.

예시

AMI, VPC 모듈, 역할과 내부용 aws_iam_instance_profile.test_profile3을 준비하세요. 두 예시의 공개 프로파일은 Terraform 주소가 달라도 이름은 test_profile로 같습니다. 기존 프로파일과 함께 새 프로파일을 만들려면 다른 이름을 사용하세요.

변경 전

hcl
resource "aws_iam_instance_profile" "example" {
  name = "test_profile"
  role = aws_iam_role.test_role.name
}

resource "aws_instance" "public_instance" {
  ami                  = data.aws_ami.ubuntu.id
  instance_type        = "t2.micro"
  subnet_id            = module.vpc.public_subnets[0]
  iam_instance_profile = aws_iam_instance_profile.example.name
}

resource "aws_instance" "private_instance" {
  ami                  = data.aws_ami.ubuntu.id
  instance_type        = "t2.micro"
  subnet_id            = module.vpc.private_subnets[0]
  iam_instance_profile = aws_iam_instance_profile.example.name
}

변경 후

hcl
resource "aws_iam_instance_profile" "public_profile" {
  name = "test_profile"
  role = aws_iam_role.test_role2.name
}

resource "aws_instance" "public_instance" {
  ami                  = data.aws_ami.ubuntu.id
  instance_type        = "t2.micro"
  subnet_id            = module.vpc.public_subnets[0]
  iam_instance_profile = aws_iam_instance_profile.public_profile.name
}

resource "aws_instance" "private_instance" {
  ami                  = data.aws_ami.ubuntu.id
  instance_type        = "t2.micro"
  subnet_id            = module.vpc.private_subnets[0]
  iam_instance_profile = aws_iam_instance_profile.test_profile3.name
}

두 번째 예시에서 공개 인스턴스는 test_role2를 사용합니다. test_profile3에는 내부 워크로드에 필요한 별도 역할을 연결하세요.

참조