Application Load Balancer 타입으로 생성
콘솔로 생성가능하지만 cli로 생성해보았다.
https://awscli.amazonaws.com/v2/documentation/api/latest/reference/elbv2/create-load-balancer.htmlhttps://docs.aws.amazon.com/elasticloadbalancing/latest/application/tutorial-application-load-balancer-cli.html
create-load-balancer — AWS CLI 2.4.21 Command Reference
The state code. The initial state of the load balancer is provisioning . After the load balancer is fully set up and ready to route traffic, its state is active . If load balancer is routing traffic but does not have the resources it needs to scale, its st
awscli.amazonaws.com
위 aws document 참고하면서 작업 진행하면된다.
로드밸런서 생성
aws elbv2 create-load-balancer --name ${loadbalancer-name} --subnets ${subnet-id-1} ${subnet-id-2} --security-groups ${security-group}
- ${loadbalancer-name} : 생성할 로드밸런서 이름
- ${subnet-id-1} / ${subnet-id-2} : 서브넷 아이디 입력, 두 서브넷은 서로 다른 존에 있는 서브넷을 입력
- ${security-group} : 로드밸런서에 사용할 security group id를 입력
명령어 실행하면 아래와 같은 결과가 나오는데 LoadBalancerArn은 뒤에도 사용되기 때문에 따로 저장해두면 콘솔에서 확인하지 않아도 되어서 빨리 생성할 수 있다.
{
"LoadBalancers": [
{
"LoadBalancerArn": "arn:aws:elasticloadbalancing:ap-northeast-2:62...",
...
}
}
타겟 그룹 생성
내가 생성할 타겟 그룹
- Target type : instance
- IP address type : ipv4
- Protocol : Port : http 80 포트
aws elbv2 create-target-group --name ${target-group-name} --protocol HTTP --port 80 --vpc-id ${vpc-id} --ip-address-type ipv4
- ${target-group-name} : 생성할 target group 이름
- ${vpc-id} : vpc-id
result :
{
"TargetGroups": [
{
"TargetGroupArn": "arn:aws:elasticloadbalancing:ap-northeast-2:62..",
...
}
]
}
로드 밸런서에 https 리스너 생성
내가 생성할 리스너
- Protocol : https
- Type(작업유형) : forward
aws elbv2 create-listener --load-balancer-arn ${load-balancer-arn} --protocol HTTPS --port 443 --certificates CertificateArn=${certificate-arn} --default-actions Type=forward,TargetGroupArn=${target-group-arn}
- ${load-balancer-arn} : 위에서 로드밸런서 생성 후에 표기된 LoadBalancerArn 입력
- ${certificate-arn} : AWS Certificate Manager 에서 사용할 certificate arn 입력 (없으면 생성)
- ${target-group-arn} : 타겟 그룹 생성 후 결과 값에 표기된 TargetGroupArn 입력
로드 밸런서에 http port redirct 리스너 생성
이 리스너는 작업하면서 생성해보았던 리스너.
http 특정 포트로 접근했을 때 https로 리다이렉트
aws elbv2 create-listener --load-balancer-arn ${load-balancer-arn} --protocol HTTP --port ${port} --default-actions '{"Type": "redirect", "RedirectConfig": {"Protocol": "HTTPS", "Port": "443", "Host": "#{host}", "Query": "#{query}", "Path": "/#{path}", "StatusCode": "HTTP_301"}}'
타겟 그룹에 타겟 인스턴스 등록
aws elbv2 register-targets --target-group-arn ${target-group-arn} --targets Id=${instance-id},Port=${port}
- ${target-group-arn} : 타겟 그룹 생성 후 결과 값에서 표기된 TargetGroupArn
- ${instance-id} : 타겟으로 지정할 인스턴스 아이디
- ${port} : instance에서 사용한 포트