【AWS CLI】ALBおよびNLB関連の情報取得編

記事タイトルとURLをコピーする

こんにちは。AWS CLIが好きな福島です。
今回は、AWS CLIを使ってALBおよびNLB関連の情報を取得するコマンドをご紹介いたします。 ちなみにですが、ALBおよびNLB関連は、aws elbv2を使い、CLBは、aws elbコマンドを使います。 ※aws elbコマンド一覧は、今度ブログにまとめたいと思います。

実行環境

今回、コマンドを実行した環境は、以下の通りとなります。
(本記事でご紹介しているコマンドの中には、Linuxのコマンドを利用している箇所があります。)

# uname -a
Linux LAPTOP-CNM26HN6 4.4.0-18362-Microsoft #1049-Microsoft Thu Aug 14 12:01:00 PST 2020 x86_64 x86_64 x86_64 GNU/Linux
#

基本情報

コマンド ヘッダー無し

aws elbv2 describe-load-balancers --query "LoadBalancers[].[LoadBalancerName,Scheme,DNSName,VpcId,AvailabilityZones[0].ZoneName,AvailabilityZones[0].SubnetId,AvailabilityZones[1].ZoneName,AvailabilityZones[1].SubnetId]" --output text | column -t
  • 実行結果
fk-test-nlb-internet  internet-facing  fk-test-nlb-internet-42f1bb9bf168c50f.elb.ap-northeast-1.amazonaws.com    vpc-0fee138d3e0deef81  ap-northeast-1c  subnet-03fbedc39e6211234  ap-northeast-1a  subnet-0093d7475de79313b
fk-test-nlb-internal  internal         fk-test-nlb-internal-74dabe51f4ee8d56.elb.ap-northeast-1.amazonaws.com    vpc-0fee138d3e0deef81  ap-northeast-1a  subnet-04c2354969c7b262a  ap-northeast-1c  subnet-052e505ee96323faa
fk-test-alb-internet  internet-facing  fk-test-alb-internet-1504427073.ap-northeast-1.elb.amazonaws.com          vpc-0fee138d3e0deef81  ap-northeast-1a  subnet-0093d7475de79313b  ap-northeast-1c  subnet-03fbedc39e6211234
fk-test-alb-internal  internal         internal-fk-test-alb-internal-567146242.ap-northeast-1.elb.amazonaws.com  vpc-0fee138d3e0deef81  ap-northeast-1a  subnet-04c2354969c7b262a  ap-northeast-1c  subnet-052e505ee96323faa

コマンド ヘッダー有り

echo "LoadBalancerName Scheme DNSName VpcId AvailabilityZone[1] SubnetId[1] AvailabilityZones[2] SubnetId[2]" > /tmp/awscli.tmp;\
aws elbv2 describe-load-balancers --query "LoadBalancers[].[LoadBalancerName,Scheme,DNSName,VpcId,AvailabilityZones[0].ZoneName,AvailabilityZones[0].SubnetId,AvailabilityZones[1].ZoneName,AvailabilityZones[1].SubnetId]" --output text >> /tmp/awscli.tmp;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
LoadBalancerName      Scheme           DNSName                                                                   VpcId                  AvailabilityZone[1]  SubnetId[1]               AvailabilityZones[2]  SubnetId[2]
fk-test-nlb-internet  internet-facing  fk-test-nlb-internet-42f1bb9bf168c50f.elb.ap-northeast-1.amazonaws.com    vpc-0fee138d3e0deef81  ap-northeast-1c      subnet-03fbedc39e6211234  ap-northeast-1a       subnet-0093d7475de79313b
fk-test-nlb-internal  internal         fk-test-nlb-internal-74dabe51f4ee8d56.elb.ap-northeast-1.amazonaws.com    vpc-0fee138d3e0deef81  ap-northeast-1a      subnet-04c2354969c7b262a  ap-northeast-1c       subnet-052e505ee96323faa
fk-test-alb-internet  internet-facing  fk-test-alb-internet-1504427073.ap-northeast-1.elb.amazonaws.com          vpc-0fee138d3e0deef81  ap-northeast-1a      subnet-0093d7475de79313b  ap-northeast-1c       subnet-03fbedc39e6211234
fk-test-alb-internal  internal         internal-fk-test-alb-internal-567146242.ap-northeast-1.elb.amazonaws.com  vpc-0fee138d3e0deef81  ap-northeast-1a      subnet-04c2354969c7b262a  ap-northeast-1c       subnet-052e505ee96323faa

リスナー設定

aws elbv2 describe-listenersコマンドでリスナーの設定を確認できます。 このコマンドを実行する際にELBのARNを指定する必要があるため、aws elbv2 describe-load-balancersコマンドを使い、 ELBのARNを取得し、for文でループさせています。

コマンド

echo "LoadBalancerArn Port Protcol CertificateArn" > /tmp/awscli.tmp;\
for elb_arn in $(aws elbv2 describe-load-balancers --query "LoadBalancers[].LoadBalancerArn" --output text)
do
aws elbv2 describe-listeners --load-balancer-arn $elb_arn --query "Listeners[].[LoadBalancerArn,Port,Protocol,Certificates[0].CertificateArn]" --output text >> /tmp/awscli.tmp
done;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
LoadBalancerArn                                                                                                  Port  Protcol  CertificateArn
arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxxxxxx:loadbalancer/net/fk-test-nlb-internet/42f1bb9bf168c50f  80    TCP      None
arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxxxxxx:loadbalancer/net/fk-test-nlb-internal/74dabe51f4ee8d56  80    TCP      None
arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxxxxxx:loadbalancer/app/fk-test-alb-internet/1fbcfd4f6e1b176c  443   HTTPS    arn:aws:acm:ap-northeast-1:xxxxxxxxxxxxxx:certificate/e64888e9-f279-4beb-8db9-eb1f1b183236
arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxxxxxx:loadbalancer/app/fk-test-alb-internet/1fbcfd4f6e1b176c  80    HTTP     None
arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxxxxxx:loadbalancer/app/fk-test-alb-internal/076bd34f675a10a7  80    HTTP     None

ターゲットグループ情報

コマンド ヘッダー無し

aws elbv2 describe-target-groups --query "TargetGroups[].[TargetGroupName,Protocol,Port,VpcId,HealthCheckProtocol,HealthCheckPort,HealthCheckEnabled,HealthCheckIntervalSeconds,HealthCheckTimeoutSeconds,HealthyThresholdCount,UnhealthyThresholdCount,HealthCheckPath,TargetType,Matcher.HttpCode]" --output text | column -t
  • 実行結果
fk-test-alb-80     HTTP  80  vpc-0fee138d3e0deef81  HTTP  traffic-port  True  30  5   5  2  /     instance  200
fk-test-alb-80-ip  HTTP  80  vpc-0fee138d3e0deef81  HTTP  traffic-port  True  30  5   5  2  /     ip        200
fk-test-nlb        TCP   80  vpc-0fee138d3e0deef81  TCP   traffic-port  True  30  10  3  3  None  ip        None
fk-test-target     TCP   80  vpc-0fee138d3e0deef81  TCP   traffic-port  True  30  10  3  3  None  instance  None
fk-test-target-ip  TCP   80  vpc-0fee138d3e0deef81  TCP   traffic-port  True  30  10  3  3  None  ip        None
test               HTTP  80  vpc-0905a1f615df30db6  HTTP  traffic-port  True  30  5   5  2  /     instance  200

コマンド ヘッダー有り

echo "TargetGroupName Protocol Port VpcId HealthProtocol HealthPort HealthEnabled HealthInterval HealthTimeout HealthyCount UnhealthyCount HealthCheckPath TargetType HttpCode" > /tmp/awscli.tmp;\
aws elbv2 describe-target-groups --query "TargetGroups[].[TargetGroupName,Protocol,Port,VpcId,HealthCheckProtocol,HealthCheckPort,HealthCheckEnabled,HealthCheckIntervalSeconds,HealthCheckTimeoutSeconds,HealthyThresholdCount,UnhealthyThresholdCount,HealthCheckPath,TargetType,Matcher.HttpCode]" --output text >> /tmp/awscli.tmp;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
TargetGroupName    Protocol  Port  VpcId                  HealthProtocol  HealthPort    HealthEnabled  HealthInterval  HealthTimeout  HealthyCount  UnhealthyCount  HealthCheckPath  TargetType  HttpCode
fk-test-alb-80     HTTP      80    vpc-0fee138d3e0deef81  HTTP            traffic-port  True           30              5              5             2               /                instance    200
fk-test-alb-80-ip  HTTP      80    vpc-0fee138d3e0deef81  HTTP            traffic-port  True           30              5              5             2               /                ip          200
fk-test-nlb        TCP       80    vpc-0fee138d3e0deef81  TCP             traffic-port  True           30              10             3             3               None             ip          None
fk-test-target     TCP       80    vpc-0fee138d3e0deef81  TCP             traffic-port  True           30              10             3             3               None             instance    None
fk-test-target-ip  TCP       80    vpc-0fee138d3e0deef81  TCP             traffic-port  True           30              10             3             3               None             ip          None
test               HTTP      80    vpc-0905a1f615df30db6  HTTP            traffic-port  True           30              5              5             2               /                instance    200

ELBの属性情報

ELBの属性情報は、aws elbv2 describe-load-balancer-attributesコマンドで確認できます。 このコマンドを実行する際にELBのARNを指定する必要があるため、aws elbv2 describe-load-balancersコマンドを使い ELBのARNを取得し、for文でループさせています。

コマンド

echo "LoadBalancer,Item,Value" > /tmp/awscli.tmp;\
for elb_arn in $(aws elbv2 describe-load-balancers --query "LoadBalancers[].LoadBalancerArn" --output text)
do
  elb_arn_esc=$(echo ${elb_arn#*/} | sed 's/\//\\\//g')
  aws elbv2 describe-load-balancer-attributes --load-balancer-arn $elb_arn --query "Attributes[]" --output text | sed -e s/^/$elb_arn_esc,/g -e 's/\t/,/g' >> /tmp/awscli.tmp
done;\
column -s, -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
LoadBalancer                               Item                                             Value
net/fk-test-nlb-internet/42f1bb9bf168c50f  access_logs.s3.enabled                           false
net/fk-test-nlb-internet/42f1bb9bf168c50f  access_logs.s3.prefix
net/fk-test-nlb-internet/42f1bb9bf168c50f  deletion_protection.enabled                      false
net/fk-test-nlb-internet/42f1bb9bf168c50f  access_logs.s3.bucket
net/fk-test-nlb-internet/42f1bb9bf168c50f  load_balancing.cross_zone.enabled                false
app/fk-test-alb-internet/1fbcfd4f6e1b176c  access_logs.s3.enabled                           false
app/fk-test-alb-internet/1fbcfd4f6e1b176c  access_logs.s3.bucket
app/fk-test-alb-internet/1fbcfd4f6e1b176c  access_logs.s3.prefix
app/fk-test-alb-internet/1fbcfd4f6e1b176c  idle_timeout.timeout_seconds                     60
app/fk-test-alb-internet/1fbcfd4f6e1b176c  deletion_protection.enabled                      false
app/fk-test-alb-internet/1fbcfd4f6e1b176c  routing.http2.enabled                            true
app/fk-test-alb-internet/1fbcfd4f6e1b176c  routing.http.drop_invalid_header_fields.enabled  false
app/fk-test-alb-internet/1fbcfd4f6e1b176c  routing.http.desync_mitigation_mode              defensive
app/fk-test-alb-internet/1fbcfd4f6e1b176c  waf.fail_open.enabled                            false

ターゲットグループの属性情報

ターゲットグループの属性情報は、aws elbv2 describe-target-group-attributesコマンドで確認できます。 このコマンドを実行する際にターゲットグループのARNが必要なため、aws elbv2 describe-target-groupsコマンドを使い ターゲットグループのARN取得し、for文でループさせています。

コマンド

echo "TargetGroup,Item,Value" > /tmp/awscli.tmp;\
for target_group_arn in $(aws elbv2 describe-target-groups --query "TargetGroups[].TargetGroupArn" --output text)
do
  target_group_arn_esc=$(echo ${target_group_arn#*/} | sed 's/\//\\\//g')
  aws elbv2 describe-target-group-attributes --target-group-arn ${target_group_arn} --query "Attributes[]" --output text | sed -e s/^/$target_group_arn_esc,/g -e 's/\t/,/g' >> /tmp/awscli.tmp
done;\
column -s, -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
TargetGroup                         Item                                                 Value
fk-test-alb-80/3312c80bfe7df040     stickiness.enabled                                   false
fk-test-alb-80/3312c80bfe7df040     deregistration_delay.timeout_seconds                 300
fk-test-alb-80/3312c80bfe7df040     stickiness.type                                      lb_cookie
fk-test-alb-80/3312c80bfe7df040     stickiness.lb_cookie.duration_seconds                86400
fk-test-alb-80/3312c80bfe7df040     slow_start.duration_seconds                          0
fk-test-alb-80/3312c80bfe7df040     load_balancing.algorithm.type                        round_robin
fk-test-alb-80-ip/0c590818775e2702  stickiness.enabled                                   false
fk-test-alb-80-ip/0c590818775e2702  deregistration_delay.timeout_seconds                 300
fk-test-alb-80-ip/0c590818775e2702  stickiness.type                                      lb_cookie
fk-test-alb-80-ip/0c590818775e2702  stickiness.lb_cookie.duration_seconds                86400
fk-test-alb-80-ip/0c590818775e2702  slow_start.duration_seconds                          0
fk-test-alb-80-ip/0c590818775e2702  load_balancing.algorithm.type                        round_robin
fk-test-nlb/33e72b413099213f        proxy_protocol_v2.enabled                            false
fk-test-nlb/33e72b413099213f        stickiness.enabled                                   false
fk-test-nlb/33e72b413099213f        deregistration_delay.timeout_seconds                 300
fk-test-nlb/33e72b413099213f        stickiness.type                                      source_ip
fk-test-nlb/33e72b413099213f        deregistration_delay.connection_termination.enabled  false

ヘルスチェック設定の確認

ヘルスチェック設定を確認するには、aws elbv2 describe-target-healthコマンドで確認できます。 このコマンドを実行する際にターゲットグループのARNが必要なため、aws elbv2 describe-target-groupsコマンドを使い ターゲットグループのARN取得し、for文でループさせています。

コマンド

echo "TargetGroup,Target,TargetPort,HealthCheckPort,State,Reason,Description" > /tmp/awscli.tmp;\
for target_group_arn in $(aws elbv2 describe-target-groups --query "TargetGroups[].TargetGroupArn" --output text)
do
  target_group_arn_esc=$(echo ${target_group_arn#*/} | sed 's/\//\\\//g')
  aws elbv2 describe-target-health --target-group-arn ${target_group_arn} --query "TargetHealthDescriptions[].[Target.Id,Target.Port,HealthCheckPort,TargetHealth.State,TargetHealth.Reason,TargetHealth.Description]" --output text | sed -e s/^/$target_group_arn_esc,/g -e 's/\t/,/g' >> /tmp/awscli.tmp
done;\
column -s, -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
TargetGroup                         Target               TargetPort  HealthCheckPort  State      Reason                     Description
fk-test-alb-80/3312c80bfe7df040     i-0808672558492fde8  80          80               unused     Target.InvalidState        Target is in the stopped state
fk-test-alb-80/3312c80bfe7df040     i-0f6126b7aeedfabd6  80          80               unused     Target.InvalidState        Target is in the stopped state
fk-test-alb-80-ip/0c590818775e2702  10.88.0.59           80          80               unhealthy  Target.Timeout             Request timed out
fk-test-target/7d263b6ee70fc2a5     i-0808672558492fde8  80          80               unused     Target.NotInUse            Target group is not configured to receive traffic from the load balancer
fk-test-target-ip/56ceab16f7d2e5b4  10.88.1.83           80          80               unhealthy  Target.FailedHealthChecks  Health checks failed
fk-test-target-ip/56ceab16f7d2e5b4  10.88.0.59           80          80               unhealthy  Target.FailedHealthChecks  Health checks failed

タグ情報

ELBのタグ情報を取得するには、aws elbv2 describe-tagsコマンドを利用します。 このコマンドを実行する際にELBのARNを指定する必要があるため、aws elbv2 describe-load-balancersコマンドを使い、 ELBのARNを取得し、for文でループさせています。

コマンド

echo "LoadBalancer,Key[1],Value[1],Key[2],Value[2],Key[3],Value[3],Key[4],Value[4],Key[5],Value[5],Key[6],Value[6],Key[7],Value[7],Key[8],Value[8],Key[9],Value[9],Key[10],Value[10]" > /tmp/awscli.tmp
for elb_arn in $(aws elbv2 describe-load-balancers --query "LoadBalancers[].LoadBalancerArn" --output text)
do
  elb_arn_esc=$(echo ${elb_arn#*/} | sed 's/\//\\\//g')
  aws elbv2 describe-tags --resource-arns ${elb_arn} --query "TagDescriptions[].[
Tags[0].Key,Tags[0].Value,\
Tags[1].Key,Tags[1].Value,\
Tags[2].Key,Tags[2].Value,\
Tags[3].Key,Tags[3].Value,\
Tags[4].Key,Tags[4].Value,\
Tags[5].Key,Tags[5].Value,\
Tags[6].Key,Tags[6].Value,\
Tags[7].Key,Tags[7].Value,\
Tags[8].Key,Tags[8].Value,\
Tags[9].Key,Tags[9].Value]" --output text | sed -e s/^/$elb_arn_esc,/g -e 's/\t/,/g' >> /tmp/awscli.tmp
done;\
column -s, -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
LoadBalancer                               Key[1]  Value[1]  Key[2]  Value[2]  Key[3]  Value[3]              Key[4]  Value[4]  Key[5]  Value[5]  Key[6]  Value[6]  Key[7]  Value[7]  Key[8]  Value[8]  Key[9]  Value[9]  Key[10]  Value[10]
net/fk-test-nlb-internet/42f1bb9bf168c50f  ENV     Dev       System  fk        Name    fk-test-nlb-internet  None    None      None    None      None    None      None    None      None    None      None    None      None     None
net/fk-test-nlb-internal/74dabe51f4ee8d56  None    None      None    None      None    None                  None    None      None    None      None    None      None    None      None    None      None    None      None     None

ALBにAWS WAFが関連付けられているか確認

ELBのコマンドではないですが、aws wafv2 get-web-acl-for-resourceコマンドを利用し、 ALBがAWS WAFに関連付けられているか確認することができます。 このコマンドを実行する際にELBのARNが必要なため、aws elbv2 describe-load-balancersコマンドを使い、 ELBのARNを取得し(grepを使いALBのみにフィルター)、for文でループさせています。

コマンド

echo "LoadBalancer,WebAclName,Description,WebAclARN" > /tmp/awscli.tmp;\
aws elbv2 describe-load-balancers --query "LoadBalancers[].LoadBalancerArn" --output text | tr "\t" "\n" | grep "loadbalancer/app/" | while read alb_arn
do
  alb_arn_esc=$(echo ${elb_arn#*/} | sed 's/\//\\\//g')
  aws wafv2 get-web-acl-for-resource --resource-arn $alb_arn --query "[WebACL.Name,WebACL.Description,WebACL.ARN]" --output text | sed -e s/^/$alb_arn_esc,/g -e 's/\t/,/g' >> /tmp/awscli.tmp
done;\
column -s, -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
LoadBalancer                               WebAclName  Description  WebAclARN
app/fk-test-alb-internal/076bd34f675a10a7  cdcd        weerere      arn:aws:wafv2:ap-northeast-1:xxxxxxxxxxxxxx:regional/webacl/cdcd/79fa4d07-2606-4210-9a5c-e338115e0e52
app/fk-test-alb-internal/076bd34f675a10a7  test        test         arn:aws:wafv2:ap-northeast-1:xxxxxxxxxxxxxx:regional/webacl/test/a568c381-e7df-4c32-a0a4-8f6f4337d290

おまけ

ルールの設定も出力してみました。

  • 1ルール、1個の条件だけ出力されます。(条件を複数していても表示されません。) ※queryを変更すれば表示可能。
  • アクションの固定レスポンスに改行コードが入っていると表示が崩れます。(基本入ってると思いますが...)
  • アクションの転送先は1個だけ表示されます。(複数指定している場合、表示されません。)

ルール設定(CSVファイルに出力)

csv_file=alb-nlb_rules.csv;\
echo "Listener_arn,Priority,Field,HostHeaderConfig,HttpHeaderConfig,PathPatternConfig,SourceIpConfig,QueryStringConfig_Key,QueryStringConfig_Value,HttpRequestMethodConfig,Type,TargetGroupArn,MessageBody,StatusCode,ContentType,Protocol,Port,Host,Path,Query,StatusCode,UserPoolArn,UserPoolClientId,UserPoolDomain,SessionCookieName,Scope,SessionTimeout,OnUnauthenticatedRequest"  > $csv_file;\
for elb_arn in $(aws elbv2 describe-load-balancers --query "LoadBalancers[].LoadBalancerArn" --output text)
do
  for listener_arn in $(aws elbv2 describe-listeners --load-balancer-arn $elb_arn --query "Listeners[].ListenerArn" --output text)
  do
  listener_arn_esc=$(echo $listener_arn | sed 's/\//\\\//g')
  aws elbv2 describe-rules --listener-arn $listener_arn --query "Rules[].[Priority,\
  Conditions[0].Field,\
  Conditions[0].HostHeaderConfig.Values[0],\
  Conditions[0].HttpHeaderConfig.Values[0],\
  Conditions[0].PathPatternConfig.Values[0],\
  Conditions[0].SourceIpConfig.Values[0],\
  Conditions[0].QueryStringConfig.Values[0].Key,
  Conditions[0].QueryStringConfig.Values[0].Value,\
  Conditions[0].HttpRequestMethodConfig.Values[0],\
  Actions[0].Type,\
  Actions[0].TargetGroupArn,\
  Actions[0].FixedResponseConfig.MessageBody,\
  Actions[0].FixedResponseConfig.StatusCode,\
  Actions[0].FixedResponseConfig.ContentType,\
  Actions[0].RedirectConfig.Protocol,\
  Actions[0].RedirectConfig.Port,\
  Actions[0].RedirectConfig.Host,\
  Actions[0].RedirectConfig.Path,\
  Actions[0].RedirectConfig.Query,\
  Actions[0].RedirectConfig.StatusCode,\
  Actions[0].AuthenticateCognitoConfig.UserPoolArn,\
  Actions[0].AuthenticateCognitoConfig.UserPoolClientId,\
  Actions[0].AuthenticateCognitoConfig.UserPoolDomain,\
  Actions[0].AuthenticateCognitoConfig.SessionCookieName,\
  Actions[0].AuthenticateCognitoConfig.Scope,\
  Actions[0].AuthenticateCognitoConfig.SessionTimeout,\
  Actions[0].AuthenticateCognitoConfig.OnUnauthenticatedRequest]" --output text | sed -e s/^/$listener_arn_esc,/g -e 's/\t/,/g' >> $csv_file
  done
done
  • 実行結果 f:id:swx-fukushima:20201228125354p:plain f:id:swx-fukushima:20201228125421p:plain

おわりに

今回は、ALBおよびNLB関連の情報を取得するコマンドをご紹介いたしました。

福島 和弥 (記事一覧)

2019/10 入社

AWS CLIが好きです。

AWS資格12冠。2023 Japan AWS Partner Ambassador/APN ALL AWS Certifications Engineer。