Setting Latest AMI
data "aws_ami" "ubuntu_latest" {
most_recent = true
owners = ["099720109477"] #Canonical
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
data "aws_ami" "amzn2_lastest" {
most_recent = true
owners = ["amazone"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-ebs"]
}
aws_ami
- AWS 내 ami를 셋팅할 수 있는 명령어입니다.
data
- Terraform 외부에서 정의되거나 다른 별도의 Terraform 구성에 의해 정의되거나 기능에 의해 수정된 정보를 사용할 수 있습니다.
most_recent
- 둘 이상의 결과가 반환되는 경우 가장 최근의 AMI를 사용한다는 것을 의미합니다.
owners
- 검색을 제한할 AMI 소유자 목록. 최소 1개의 값을 지정해야 합니다.
filter
- 필터링할 하나 이상의 이름/값 쌍입니다
Create EC2 (Bastion)
resource "aws_instance" "terraform_bastion" {
ami = data.aws_ami.ubuntu_latsest.id
instance_type = "t2.micro"
key_name = aws_key_pair.aws_bastion_key.key_name
vpc_security_group_ids = ["${aws_security_group.terraform_bastion_sg.id}"]
subnet_id = aws_subnet.terraform_public_subnet.0.id
tags = {
Name = "${var.company_name}-${var.project_name}-${var.environment}-bastion"
}
}
aws_instance
- AWS 내 EC2를 생성하는 명령어입니다.
- AMI, Instance type, key, Subnet(첫 번째로 선택), SG 선택해줍니다.
ami
- 인스턴스를 생성할 ami의 id입니다.
vpc_security_group_ids
- 하나의 인스턴스에는 여러개의 SG을 넣는 것이 가능하기 때문에 배열로 생성합니다.
Create Launch_Config
resource "aws_launch_configuration" "asg_configuration" {
image_id = data.aws_ami.amzn2_lastest.id
instance_type = "t2.micro"
key_name = aws_key_pair.aws_ec2_key.key_name
security_groups = [aws.security_group.terraform_sg.id]
depends_on = [aws_security_group.terraform_sg]
lifecycle {
create_before_destory = true
}
name = "${var.company_name}-${var.project_name}-${var.environment}-asg-configuration"
}
aws_launch_configuration
- Auto Scaling을 통해 생성될 EC2, 생성할 때 각 Instance에 적용할 Launch Configuration을 생성합니다.
depens_on
- 리소스들의 실행 순서를 정해줄 수 있는 명령어입니다.
- depens_on을 사용 하는 이유?
- SG이 생성되기도 전에 resource를 생성하려고 하면, 존재하지 않는다는 에러가 발생할 수 있기 때문에 이와같이 의존성을 부여하는 것입니다.
lifecycle
- 만일 재생성되어야 한다면 새로운 인스턴스 먼저 생성후 예전 인스턴스를 지우도록 합니다. → 재생성으로 인한 downtime이 없도록 합니다.
Create Autoscaling Group
resource "aws_autoscaling_group" "terraform_asg" {
launch_configuration = aws_launch_configuration.asg_configuration.id
health_check_type = "EC2"
vpc_zone_identifier = aws_subnet.terraform_private_subnet.*.id
min_size = 2
max_size = 5
tag {
key = "Name"
value = "${var.company_name}-${var.project_name}-${var.enviroment}-ec2"
propagate_at_launch = true
}
name = "${var.company_name}-${var.project_name}-${var.enviroment}-asg"
}
aws_autoscaling_group
- AWS 내 Autoscaling group을 생성하는 명령어입니다.
health_check_type
- EC2 or ELB 로 작성 가능하며 상태 확인이 수행되는 방식을 선택합니다.
min_size/max_size
- Autosacaling으로 생성될 인스턴스의 최소/최대 개수를 나타냅니다.
propagate_at_launch
- ASG를 통해 시작된 Amazon EC2 인스턴스로 태그 전파 활성화
Create ELB
resource "aws_lb" "terraform_nlb" {
internal = false
load_balancer_type = "network"
subnets = aws_subnet.terraform_private_subnet.*.id
name = "${var.company_name}-${var.project_name}-${var.environment}-nlb"
}
aws_lb
- AWS 내 LB 생성하는 명령어입니다.
internal
- 외부 LB인지, 내부 LB인지를 구분
load_balancer_type
- application(default)/network/gateway
Create Target Group
resource "aws_lb_target_group" "terraform_lb_tg" {
port = 80
protocol = "TCP"
vpc_id = aws_vpc.terraform_vpc.id
target_type = "instance"
health_check {
port = "80"
protocol = "TCP"
}
name = "${var.company_name}-${var.project_name}-${var.enviroment}-tg"
}
aws_lb_target_group
- AWS 내 Target Group을 생성하는 명령어입니다.
target_type
- Target의 대상을 설정해줍니다. (instance(default)/ip/lambda)
Create Listener
resource "aws_lb_listener" "terraform_lb_Listener" {
load_balancer_arn = aws_lb.terraform_nlb.arn
port = 80
protocol = "TCP"
default_action {
target_group_arn = aws_lb_target_group.terraform_lb_tg.arn
type = "forward"
}
}
aws_lb_listener
- AWS 내 LB Listener 생성하는 명령어입니다.
Listerner란
- 로드 밸런서가 등록된 대상으로 요청을 라우팅하는 방법을 결정하는 역할을 합니다.
default_action
- 기본 작업에 대한 구성 블록입니다.
type
- 라우팅 작업의 유형입니다.
forward
- 받아들인 트래픽을 target group으로 전달하라는 명령어입니다.
Attatch Loadbalancer to AutoScaling Group
resource "aws_autoscaling_attachment" "name" {
utoscaling_group_name = aws_autoscaling_group.terraform_asg.id
alb_target_group_arn = aws_lb_target_group.terraform_lb_tg.arn
}
- 생성한 asg과 lb를 연결시킵니다.
Troubleshooting
- apply 를하고 destory를 진행하지 않아 생김
- destory → apply 시 해결
- key-pair 생성 후 커멘드창에서 aws로 전송하는 작업을 진행하였음
- 테라폼에서 전송해주므로 따로 aws를 전송할 필요는 없었음
'Clould > Terraform' 카테고리의 다른 글
Terraform 실습 #2 (0) | 2022.07.28 |
---|---|
Terraform 실습 #1 (0) | 2022.07.28 |
Terraform 설치 (0) | 2022.07.28 |