본문 바로가기

Ops/Deploy

deploy script - (aws) get ec2 ip from elbv2

반응형

deploy script - (aws) get ec2 ip from elbv2

 

 

 

 

■ 설명

Load Balancer에 등록되어 있는 EC2의 private ip를 확인할 수 있는 스크립트입니다.

function 형태로 만들어져 있으니 다른 스크립트에 그대로 추가해서 사용할 수 있습니다.

 

 

■ 로직

1. elbv2는 alb를 의미하며, target group에 ec2를 등록하기 때문에 target group을 확인합니다.

2. target group에 등록되어 있는 ec2 목록을 확인하고 각 private ip 정보를 확인합니다.

 

 

■ 코드

# 변수 설명
ELB_NAME : ELB 이름
REGION : 서비스 리전
#!/bin/bash
ELB_NAME=elbv2-mkdir-chandler
REGION=ap-northeast-2
 
function get_ec2ips_from_elbv2                                        
{  
 
       local elb_arns=$(aws elbv2 --region ${REGION} describe-load-balancers --names $ELB_NAME | jq -r '.LoadBalancers[].LoadBalancerArn')                         
       local target_group_arn=$(aws elbv2 --region ${REGION} describe-target-groups --load-balancer-arn ${elb_arns[0]} | jq -r '.TargetGroups[].TargetGroupArn')
 
        local target_group_arn_string=$(join_by ' ' $target_group_arn)
 
        local ids=$(aws elbv2 --region ${REGION} describe-target-health --target-group-arn ${target_group_arn_string} | jq -r '.TargetHealthDescriptions[].Target.Id'    )
 
        if [ ! "$ids" ];then                                              
                echo " >> There is no ec2 in ELB"                             
                deploy_end                                                    
        fi                                                                
 
        ec2_private_ips=$(aws ec2 --region ${REGION} describe-instances --instance-ids $ids | jq -r '.Reservations[].Instances[].NetworkInterfaces[].PrivateIpAddresses[].PrivateIpAddress')                                               
 
        echo $ec2_private_ips
}
get_ec2ips_from_elbv2

 

 

 


by mkdir-chandler


 

 

 

 

 

728x90
반응형