ELB 関連で、よく使うコマンドのメモです。
ELBの作成
以下の例ではリスナーを2つもったVPC内部用のELBを作成しています。
複数のセキュリティグループに所属するようにし、タグを設定しています。
ELB_SUBNET=subnet-xxxx ELB_SECURITY_GROUP_ID="sg-xxxx sg-yyyy" TAG_SYSTEM=website TAG_ENV=Develop elb_nm=elb01 aws elb create-load-balancer --load-balancer-name ${elb_nm} \ --listeners "Protocol=HTTP,LoadBalancerPort=80,InstanceProtocol=HTTP,InstancePort=80" \ "Protocol=TCP,LoadBalancerPort=8000,InstanceProtocol=TCP,InstancePort=8000" \ --subnets ${ELB_SUBNET} \ --scheme internal \ --security-groups ${ELB_SECURITY_GROUP_ID} \ --tags "Key=Name,Value=${elb_nm}" "Key=System,Value=${TAG_SYSTEM}" "Key=Env,Value=${TAG_ENV}" \ --output table
ヘルスチェックの設定
以下の例では、ELB名を指定して、ヘルスチェックを設定しています。
aws elb configure-health-check \ --load-balancer-name ${elb_nm} \ --health-check Target=HTTP:80/app/health,Interval=15,UnhealthyThreshold=3,HealthyThreshold=2,Timeout=5 \ --output table
ELB属性の設定
以下の例では、ELB名を指定して、ELBのアクセスログなど各種属性を設定しています。
なお、ELBのアクセスログをS3に出力するには、S3バケットポリシーを設定する必要があります。
S3_BUCKET=example-blue21 S3_BUCKET_ELBLOG="log/website" aws elb modify-load-balancer-attributes \ --load-balancer-name ${elb_nm} \ --load-balancer-attributes "{\"CrossZoneLoadBalancing\":{\"Enabled\":true},\"AccessLog\":{\"Enabled\":true,\"S3BucketName\":\"${S3_BUCKET}\",\"EmitInterval\":5,\"S3BucketPrefix\":\"${S3_BUCKET_ELBLOG}\"},\"ConnectionDraining\":{\"Enabled\":true,\"Timeout\":300},\"ConnectionSettings\":{\"IdleTimeout\":60}}" \ --output table
ELBにEC2インスタンスを登録/削除
以下の例では、ELB名を指定して、EC2インスタンスを登録/削除しています。
# EC2インスタンスを登録 aws elb register-instances-with-load-balancer --instance $INSTANCE_ID --load-balancer-name $elb_nm --output table # EC2インスタンスを削除 aws elb deregister-instances-from-load-balancer --instance $INSTANCE_ID --load-balancer-name $elb_nm --output table
ELBの状態確認
以下の例では、ELB名を指定して、状態を確認しています。
aws elb describe-instance-health --load-balancer-name $elb_nm --output table
ELBの削除
以下の例では、ELB名を指定してELBを削除しています。
aws elb delete-load-balancer --load-balancer-name ${elb_nm} --output table