Reputation: 6831
I have been struggling on this.
I implemented a very simple HTTP server on two EC2 instances behind an ELB. The HTTP server only reads the cookie from the header, and send back response with the same cookie in the response header.
At the client, I use
curl --cookie "mychannelid=mytest; Expires=Sat, 27-Jun-2017 02:48:17 GMT" --header "Accept-Language: en" "http://myelb.elb.amazonaws.com:8000/test"
Also, on the elb, I enabled application stickiness with cookie name mychannelid. But when I ran this system, I still got 50/50 server assignment. How can I implement this?
Another information is that, if generate the cookie at the HTTP server, and send back to the client, also, I need to access the HTTP server with firefox, then I can get what I want. If I access the HTTP server with curl, I can't. I don't know why, I checked the packets, the cookies are in the header.
Upvotes: 2
Views: 2666
Reputation: 14250
When using Application-Controlled Session Affinity, the ELB will technically not decide the stickness based on your Application cookie, instead, it will assign it's own AWSELB
cookie, similar to the duration based cookie, but following the same expiration date as your application cookie. So whenever the browser (or any other client) comes back to the ELB, it should send both Cookies and not only the Application one.
First, we need a regular curl
, not sending cookies like you did with --cookie "mychannelid=mytest"
but instead, saving the cookies received to a file called cookies.txt
:
curl -c cookies.txt -v http://example.com/cookie.php
(note that cookie.php
on the server needs to set the application cookie, for me, I called it STICKINESSID
and set the value to tomtest31mar1030
)
Curl output showing cookies being returned:
...
* Added cookie STICKINESSID="tomtest31mar1030" for domain example.com, path /, expire 1459525228
< Set-Cookie: STICKINESSID=tomtest31mar1030; expires=Fri, 01-Apr-2016 15:40:28 GMT
* Added cookie AWSELB="85937D8F08DC213046BA84053FDD6504FB203E36C25DA455A6835969F6D49F8DF9878D0F954A8EC75B8EE2AE208ECFE0AC2CE02E2EDE8DD014D2F17F4C80301EDBC4AE5CC3EBD730BD3127FF8DAAD636359B592B73" for domain example.com, path /, expire 1459525228
< Set-Cookie: AWSELB=85937D8F08DC213046BA84053FDD6504FB203E36C25DA455A6835969F6D49F8DF9878D0F954A8EC75B8EE2AE208ECFE0AC2CE02E2EDE8DD014D2F17F4C80301EDBC4AE5CC3EBD730BD3127FF8DAAD636359B592B73;PATH=/;EXPIRES=Fri, 01-Apr-2016 15:40:28 GMT
...
i.e. a file cookies.txt
was created with both STICKINESSID
and AWSELB
cookies in it:
$ cat cookies.txt
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
example.com FALSE / FALSE 1459525228 STICKINESSID tomtest31mar1030
example.com FALSE / FALSE 1459525228 AWSELB 85937D8F08DC213046BA84053FDD6504FB203E36C25DA455A6835969F6D49F8DF9878D0F954A8EC75B8EE2AE208ECFE0AC2CE02E2EDE8DD014D2F17F4C80301EDBC4AE5CC3EBD730BD3127FF8DAAD636359B592B73
Now, having the both cookies needed to keep the stickiness, let's test sending them in the request:
$ for i in {1..20}; do curl --silent -b cookies.txt http://example.com/cookie.php | grep Instance; done
Instance Id: <b>i-xxxxxxxx</b><br/>Zone: <b>eu-west-1b</b><br/>Internal IP: <b>10.0.0.21</b><br/>
Instance Id: <b>i-xxxxxxxx</b><br/>Zone: <b>eu-west-1b</b><br/>Internal IP: <b>10.0.0.21</b><br/>
Instance Id: <b>i-xxxxxxxx</b><br/>Zone: <b>eu-west-1b</b><br/>Internal IP: <b>10.0.0.21</b><br/>
Instance Id: <b>i-xxxxxxxx</b><br/>Zone: <b>eu-west-1b</b><br/>Internal IP: <b>10.0.0.21</b><br/>
Instance Id: <b>i-xxxxxxxx</b><br/>Zone: <b>eu-west-1b</b><br/>Internal IP: <b>10.0.0.21</b><br/>
Instance Id: <b>i-xxxxxxxx</b><br/>Zone: <b>eu-west-1b</b><br/>Internal IP: <b>10.0.0.21</b><br/>
Instance Id: <b>i-xxxxxxxx</b><br/>Zone: <b>eu-west-1b</b><br/>Internal IP: <b>10.0.0.21</b><br/>
...
(this assumes http://example.com/cookie.php outputs some debugging information where you can see the IP of the instance behind the ELB, so you can test the stickiness)
This shows, that stickiness is preserved across multiple requests. Try the same test without -b cookies.txt
to test without stickiness.
Upvotes: 3