One address serves the shop, its API and a maintenance page. A listener's rules decide where each request goes.
Rules
Each rule has conditions, actions and a priority. The listener checks rules from the lowest number and uses the first that matches; if none do, its default action applies.
LISTENER=$(aws elbv2 describe-listeners --load-balancer-arn $LB --query "Listeners[0].ListenerArn" --output text)aws elbv2 create-rule --listener-arn $LISTENER --priority 10 --conditions 'Field=path-pattern,Values=/api,/api/*' --actions Type=forward,TargetGroupArn=$API_TGThe single quotes keep the shell from expanding *.
Which paths a pattern matches
| Path | /api* | /api/* | /api and /api/* |
|---|---|---|---|
| /api | yes | no | yes |
| /api/items | yes | yes | yes |
| /apiary | yes | no | no |
Field=host-header,Values=api.market.example matches the host instead (curl -H "Host: api.market.example" tests it). An action can also answer by itself:
aws elbv2 create-rule --listener-arn $LISTENER --priority 20 --conditions Field=path-pattern,Values=/admin --actions 'Type=fixed-response,FixedResponseConfig={StatusCode=503,ContentType=text/html,MessageBody=<h1>Back soon</h1>}'Type=redirect,RedirectConfig={Host=market.example,StatusCode=HTTP_301} sends the browser elsewhere.
aws elbv2 describe-rules --listener-arn $LISTENER --query "Rules[].[Priority,RuleArn]" --output tableA wrong rule is changed in place with aws elbv2 modify-rule --rule-arn $RULE --conditions 'Field=path-pattern,Values=/api,/api/*'.
Stickiness
A cart kept in one server's memory is lost when the next request lands elsewhere. Stickiness pins a browser to one target with the AWSALB cookie, here for an hour:
aws elbv2 modify-target-group-attributes --target-group-arn $TG --attributes Key=stickiness.enabled,Value=true Key=stickiness.lb_cookie.duration_seconds,Value=3600curl -s -c jar http://$DNS/ keeps the cookie, and curl -s -b jar http://$DNS/ sends it back: the same server answers each time. The cost: load spreads unevenly, and new servers only get new browsers. A shop that keeps carts in a database needs no stickiness.