Create/modify AWS ALB rules using AWS CLI version 2

To Nha Notes | March 17, 2021, 10:11 p.m.

Installation

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
./aws/install -i /usr/local/aws-cli -b /usr/local/bin

Validate installation by command: aws --version

See more detail of installation

Create ALB rule using path condition and return fixed response

aws elbv2 create-rule \
    --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2 \
    --priority 10 \
    --conditions file:///path/conditions-pattern.json \
    --actions file:///path/actions-fixed-response.json \
    --profile dev

Example of /path/conditions-pattern.json

[
    {
        "Field": "path-pattern",
        "PathPatternConfig": {
            "Values": ["/images/*"]
        }
    }
]

Example of /path/actions-fixed-response.json

[
    {
        "Type": "fixed-response",
        "FixedResponseConfig": {
            "MessageBody": "Hello world",
            "StatusCode": "200",
            "ContentType": "text/plain"
        }
    }
]

See more document and other examples of create-rule

Modify ALB rule using path condition and forward to a target group

aws elbv2 modify-rule \
    --actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067 \
    --conditions Field=path-pattern,Values='/images/*'
    --rule-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener-rule/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2/9683b2d02a6cabee

See more document and other examples of modify-rule