Day-16 AWS IAM 사용자 관리 with Terraform
Source: Dev.to
AWS에서 IAM 사용자를 수동으로 관리하는 것은 괜찮지만… 그렇지 않을 때는 문제가 됩니다.
팀 규모가 커지면 콘솔을 클릭하는 것이 고통스럽고 혼란스러워집니다.
이 데모는 Terraform 기반 IAM 사용자 관리 시스템을 보여줍니다. 여기서:
- 사용자는 CSV 파일에 정의됩니다
- 그룹 및 멤버십이 자동으로 할당됩니다
- 모든 것이 재현 가능하고, 버전 관리되며, 확장 가능합니다
개요
데모는 다음을 관리합니다:
- IAM 사용자
- IAM 그룹
- 그룹 멤버십
모든 것이 단일 CSV 파일에 의해 관리됩니다. 사용자를 수동으로 생성하는 대신 CSV를 업데이트하고 terraform apply를 실행하면 완료됩니다.
사전 요구 사항
다음이 준비되어 있는지 확인하십시오:
- AWS CLI가 구성되어 있음 (
aws configure) - Terraform v1.0+이 설치되어 있음
- 사용자 및 그룹을 생성할 수 있는 IAM 권한
- 원격 Terraform 상태를 위한 S3 버킷
Source:
빠른 시작
1️⃣ S3 백엔드 버킷 생성
terraform {
backend "s3" {
bucket = "my-aws-terraform-state-bucket-amit-123"
key = "Day-16/terraform/terraform.tfstate"
region = "us-east-1"
}
}
2️⃣ Terraform 초기화
terraform init
3️⃣ 변경 사항 검토
terraform plan
4️⃣ 구성 적용
terraform apply -auto-approve
5️⃣ 프로젝트 파일 구조
Day16/terraform
├── backend.tf # S3 백엔드 설정
├── provider.tf # AWS 제공자 설정
├── versions.tf # Terraform 및 제공자 버전
├── main.tf # IAM 사용자 + CSV 파싱
├── groups.tf # 그룹 및 멤버십
├── users.csv # 사용자 데이터 소스
└── README.md # 프로젝트 개요
Source: …
작동 방식 (단계별)
1단계 – CSV에서 사용자 읽기
# Read users from CSV
locals {
users = csvdecode(file("users.csv"))
}
2단계 – IAM 사용자 생성
# Create IAM users
resource "aws_iam_user" "users" {
for_each = { for user in local.users : user.first_name => user }
name = lower("${substr(each.value.first_name, 0, 1)}${each.value.last_name}")
path = "/users/"
tags = {
DisplayName = "${each.value.first_name} ${each.value.last_name}"
Department = each.value.department
JobTitle = each.value.job_title
}
}
사용자 이름 형식: {first_initial}{lastname} (예: Michael Scott → mscott)
3단계 – 콘솔 액세스 활성화
# Create IAM user login profile (password)
resource "aws_iam_user_login_profile" "users" {
for_each = aws_iam_user.users
user = each.value.name
password_reset_required = true
lifecycle {
ignore_changes = [
password_length,
password_reset_required,
]
}
}
4단계 – 그룹 및 멤버십 생성
# Create IAM Groups
resource "aws_iam_group" "education" {
name = "Education"
path = "/groups/"
}
resource "aws_iam_group" "managers" {
name = "Managers"
path = "/groups/"
}
resource "aws_iam_group" "engineers" {
name = "Engineers"
path = "/groups/"
}
그룹은 한 번만 생성되며, 멤버십은 동적으로 할당됩니다(간결성을 위해 여기서는 생략).
Terraform 출력
apply 후에 다음을 확인할 수 있습니다:
terraform output account_id
terraform output user_names
terraform output user_passwords # sensitive
검증 및 자동화에 유용합니다.
샘플 사용자 목록 (users.csv)
first_name,last_name,department,job_title
Michael,Scott,Education,Regional Manager
Dwight,Schrute,Sales,Assistant to the Regional Manager
Jim,Halpert,Sales,Sales Representative
Pam,Beesly,Reception,Receptionist
Ryan,Howard,Temps,Temp
Andy,Bernard,Sales,Sales Representative
Robert,California,Corporate,CEO
Stanley,Hudson,Sales,Sales Representative
Kevin,Malone,Accounting,Accountant
Angela,Martin,Accounting,Accountant
Oscar,Martinez,Accounting,Accountant
Phyllis,Vance,Sales,Sales Representative
Toby,Flenderson,HR,HR Representative
Kelly,Kapoor,Customer Service,Customer Service Representative
Darryl,Philbin,Warehouse,Warehouse Foreman
Creed,Bratton,Quality Assurance,Quality Assurance
Meredith,Palmer,Supplier Relations,Supplier Relations
Erin,Hannon,Reception,Receptionist
Gabe,Lewis,Corporate,Coordinating Director of Emerging Regions
Jan,Levinson,Corporate,Vice President of Northeast Sales
David,Wallace,Corporate,CFO
Holly,Flax,HR,HR Representative
Charles,Miner,Corporate,Vice President of the Northeast Region
Jo,Bennett,Corporate,CEO of Sabre
Clark,Green,Sales,Sales Representative
Pete,Miller,Customer Service,Customer Service Representative
정리
생성된 모든 리소스를 제거하려면:
terraform destroy
경고: 이것은 모든 사용자, 그룹 및 멤버십을 삭제합니다.
문제 해결
오류: 백엔드 접근 거부
AWS 자격 증명을 확인하십시오:
aws sts get-caller-identity
오류: 사용자가 이미 존재합니다
기존 사용자를 상태에 가져옵니다:
terraform import aws_iam_user.users["Michael"] mscott
또는 기존 사용자를 수동으로 삭제합니다:
aws iam delete-login-profile --user-name mscott
aws iam delete-user --user-name mscott
Resources
성공!
귀하의 AWS IAM 인프라가 이제 코드로 관리됩니다. 다음을 할 수 있습니다:
- CSV 파일을 편집하여 새 사용자를 추가하기
- 사용자 속성을 변경하여 그룹 멤버십 수정하기
- 모든 변경 사항을 버전 관리하기
- 여러 AWS 계정에 이 설정을 복제하기
행복한 Terraforming! 🚀