📌 테스트용 보안그룹 생성 Hcl 코드 작성 및 배포
- TEST SG 작성 hcl 코드
[root@Terraform terraform]# vi terraform-test-sg.tf
resource "aws_security_group" "test_sg" {
name = "terraform-test-sg"
description = "Security group for testing terraform"
vpc_id = "vpc-0b73adfjf0fd6" # VPC ID 입력
# Ingress rules (Inbound traffic rules)
ingress {
from_port = 22 # SSH
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # 모든 IP 허용 (테스트용)
}
ingress {
from_port = 80 # HTTP
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# Egress rules (Outbound traffic rules)
egress {
from_port = 0 # 모든 아웃바운드 트래픽 허용
to_port = 0
protocol = "-1" # 모든 프로토콜
cidr_blocks = ["0.0.0.0/0"]
}
# Tags for the security group
tags = {
Name = "terraform-test-sg"
}
}
📌 적용 절차
1. 테라폼 초기화
Terraform이 작업 디렉터리(terraform/)에서 사용할 플러그인을 설정합니다.
[root@Terraform terraform]# terraform init
Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v5.81.0...
- Installed hashicorp/aws v5.81.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
2. 배포 계획 확인
Terraform이 작성한 코드를 기반으로 어떤 작업을 수행할지 미리 확인하는 과정
[root@Terraform terraform]# terraform plan
.
.
.
},
]
+ name = "terraform-test-sg"
+ name_prefix = (known after apply)
+ owner_id = (known after apply)
+ revoke_rules_on_delete = false
+ tags = {
+ "Name" = "terraform-test-sg"
}
+ tags_all = {
+ "Name" = "terraform-test-sg"
}
+ vpc_id = "vpc-0b73fe266928289e6"
}
Plan: 1 to add, 0 to change, 0 to destroy.
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
3. 실행
terraform plan 으로 확인한 내용을 기반으로 실행
[root@Terraform terraform]# terraform apply
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
aws_security_group.test_sg: Creating...
aws_security_group.test_sg: Creation complete after 2s [id=sg-07aae1bd248c1fad7]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
테스트용 보안그룹을 생성해서 정상 작동하는지 확인을 해보았다.
다음 과정에서는 Route53 - ALB - ECS 환경에서 ECS 환경 배포를 terraform으로 진행 실습 예정이다.