본문 바로가기

Ops/Deploy

deploy script - (aws) get ec2 ip from target group

반응형

deploy script - (aws) get ec2 ip from target group

 

 

 

 

■ 설명

AWS 환경에서 EC2의 private ip를 얻기 위한 방법 중 하나입니다.

대부분의 EC2 서버는 Load Balance에 등록이 되어 있기 때문에 LB를 이용한 방법이며,

그 중에서도 ALB의 Target Group에 등록되어 있는 EC2의 ip를 받아오는 방법입니다.

 

 

■ 로직

1. Target Group의 ARN 정보를 이용하여 등록되어 있는 EC2를 확인합니다.

2. 해당 EC2의 ip 정보를 가져옵니다.

 

 

■ 코드

# 변수 설명
TG_ARN : 타겟그룹 ARN
REGION : 서비스 리전
#!/bin/bash
TG_ARN=arn:aws:elasticloadbalancing:ap-northeast-2:123456789012:targetgroup/tg-mkdir-chandler/1234567890123456
REGION=ap-northeast-2
 
function get_ec2ips_from_tg
{  
        local target_group_arn_string=${TG_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_tg

 

 

 


by mkdir-chandler


 

 

 

 

 

728x90
반응형