Michaël Gallego

This is my blog. What can you expect here? Well... Zend Framework 2, Amazon AWS...

Twitter

Google+

LinkedIn

Github

Last.fm

Log visitors IP instead of ELB IP in an Elastic Beanstalk application

Amazon Elastic Beanstalk has some convenient features, like the ability to read Apache error and access logs, without having to log into the EC2 instance! However, if you are using this feature, you should have realized that the registered IP that appears in the access log is always the same: it’s your Elastic Load Balancer IP (this happens because all of your traffic is first routed to your ELB, which then dispatch it to your EC2 instances). This information is not particularly useful.

Hopefully, we can change that quite easily, thanks to the customizations features provided by Elastic Beanstalk.

In your .ebextensions folder, create a new file (for instance custom_log.config - the only requirement for Elastic Beanstalk config files is that they end with .config -), and copy-paste the following code:

files:
  "/etc/httpd/conf.d/custom_log.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

This will be executed once your environment is deployed, and it will create a new file that simply override the default Apache log format. The X-Forwarded-For value is a specific HTTP header whose value is filled by the Elastic Load Balancer.