29

I´m running an EC2 instance through AWS Elastic Beanstalk. Unfortunately it has the incorrect timezone - it´s 2 hours earlier than it should be, because timezone is set to UTC. What I need is GMT+1.

Is there a way to set up the .ebextensions configuration, in order to force the EC2 instance to use the right timezone?

tomraithel
  • 868
  • 2
  • 12
  • 19
  • 8
    It is generally best practice to leave servers set to UTC and manage time zones in application code. http://stackoverflow.com/questions/2532729/daylight-saving-time-and-time-zone-best-practices – Matt Johnson-Pint Aug 20 '14 at 16:11

10 Answers10

35

Yes, you can.

Just create a file /.ebextensions/00-set-timezone.config with following content

commands:
  set_time_zone:
    command: ln -f -s /usr/share/zoneinfo/Australia/Sydney /etc/localtime

This is assuming your are using default Amazon Linux AMI image. If you use some other Linux distribution, just change the command to whatever it requires to set timezone in that Linux.

victorx
  • 2,446
  • 1
  • 20
  • 31
  • 1
    Hi thanks for this, I just wanted to point out it doesn't update the time zone for PHP, and time zone isn't an API configuration option for the PHP container on Beanstalk. So using UTC still seems easier. – Acyra Feb 11 '15 at 13:46
  • I'm not familiar with PhP, so I really can't tell. But it works for me in my Node.js container in Beanstalk. – victorx Feb 12 '15 at 21:32
  • 2
    Do not do that. This will also have impact of some log files which get used by AWS Healthd which reports back to AWS Beanstalk Health when you have Extended Monitoring enabled. When setting Timezone different than UTC, Extended Monitoring will not work anymore (P99, P90, P75, 2xx, 3xx will not show up under Health) – Thomas Fritz Sep 19 '17 at 15:52
  • It didn't worked out for our PHP project.The @JesúsDiaz answer nailed it. – Rafael Barros Sep 19 '18 at 19:24
  • @xwk do you know how to do this in Windows Server based system? – Nishān Wickramarathna Nov 27 '18 at 06:29
18

This is a response from the aws Support Business and this works!

---- Original message ----

How can I change the timezone of an enviroment or rather to the instances of the enviroment in Elastic Beasntalk to UTC/GMT -3 hours (Buenos Aires, Argentina)? I´m currently using Amazon Linux 2016.03. Thanks in advance for your help. Regards.

---------- Response ----------

Hello,Thank you for contacting AWS support regarding modifying your Elastic Beanstalk instances time zone to use UTC/GMT -3 hours (Buenos Aires, Argentina), please see below on steps on how to perform this modification.

The below example shows how to modify timezone for Elastic Beanstalk environment using .ebextensions for Amazon Linux OS:

  1. Create .ebextensions folder in the root of your application
  2. Create a .config file for example 00-set-timezone.config file and add the below content in yaml formatting.

    container_commands:
      01changePHP:
        command: sed -i '/PHP_DATE_TIMEZONE/ s/UTC/America\/Argentina\/Buenos_Aires/' /etc/php.d/environment.ini
      01achangePHP:
        command: sed -i '/aws.php_date_timezone/ s/UTC/America\/Argentina\/Buenos_Aires/' /etc/php.d/environment.ini
      02change_AWS_PHP:
        command: sed -i '/PHP_DATE_TIMEZONE/ s/UTC/America\/Argentina\/Buenos_Aires/' /etc/httpd/conf.d/aws_env.conf
      03php_ini_set:
        command: sed -i '/date.timezone/ s/UTC/America\/Argentina\/Buenos_Aires/' /etc/php.ini
    commands:
      01remove_local:
        command: "rm -rf /etc/localtime"
      02link_Buenos_Aires:
        command: "ln -s /usr/share/zoneinfo/America/Argentina/Buenos_Aires /etc/localtime"
      03restart_http:
        command: sudo service httpd restart
    
  3. Deploy application to Elastic Beanstalk including the .ebextensions and the timezone will change as per the above.

I hope that helps

Regards!

Jesús Díaz
  • 344
  • 4
  • 5
6

If you are running windows in your eb environment... .

create a folder named .ebextensions in the root of your project..

inside that folder create a file named timezone.config

in that file add the following :

commands:
    set_time_zone:
        command: tzutil /s "Central Standard Time"

set the time zone as needed

screenshot

Adam Zeffer
  • 61
  • 1
  • 2
5

I'm using custom .ini file in php.d folder along with regular recommendations from http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-time.html#change_time_zone:

The sed command inserts (rewrites) only the first line of /etc/sysconfig/clock, since the second line (UTC=true) should be left alone, per the above AWS documentation.

# .ebextensions/02-timezone.config
files:
  /etc/php.d/webapp.ini:
    mode: "000644"
    owner: root
    group: root
    content: |
      date.timezone="Europe/Amsterdam"

commands:
  01_set_ams_timezone:
    command:
      - sed -i '1 s/UTC/Europe\/Amsterdam/g' /etc/sysconfig/clock
      - ln -sf /usr/share/zoneinfo/Europe/Amsterdam /etc/localtime
emjayess
  • 61
  • 5
Roman Zhuzha
  • 504
  • 5
  • 7
2

Changing the time zone of EC2 with Elastic Beanstalk is simple:

  1. Create a .ebextensions folder in the root
  2. Add a file with filename end with .config (timezone.config)

Inside the file

container_commands:
  time_zone:
    command: ln -f -s /usr/share/zoneinfo/America/Argentina/Buenos_Aires /etc/localtime

Then you have done. Note that the container_commands is different from commands, from the document it states:

commands run before the application and web server are set up and the application version file is extracted.

That's the reason of your time zone command doesn't work because the server hasn't started yet.

container_commands run after the application and web server have been set up and the application version file has been extracted, but before the application version is deployed.

nobody
  • 424
  • 1
  • 8
  • 18
1

If you are runing a java/Tomcat container, just put the JVM Option on the configuration.

-Duser.timezone=America/Sao_Paulo

Possibles values: timezones

0

These workarounds only fixes the timezone for applications. But when you have any system services like a cron run it looks at the /etc/sysconfig/clock and that is always UTC. If you tail the cron logs or aws-sqsd logs would will notice timestamps are still 2hrs behind - in my case. And a change to the clock setting would need a reboot into order to take effect - which is not an option to consider should you have autoscaling in place or should you want to use ebextensions to change the system clock's config.

Amazon is aware of this issue and I dont think they have resolved it yet.

Dele
  • 585
  • 5
  • 9
0

If your EB application is using the Java/Tomcat container, you can add the JVM timezone Option to the Procfile configuration. Example:

web: java -Duser.timezone=Europe/Berlin -jar application.jar

Make sure to add all configuration options before the -jar option, otherwise they are ignored.

Stefan
  • 1
  • 1
0

Moving to AWS Linux 2 was challenging. It took me a while to work out how to do this easily in .ebextensions.

I wrote the simple solution in another stackoverflow question .. but for anyone needing instant gratification .. add the following commands into the file .ebextensions/xxyyzz.config:

container_commands:
  01_set_bne:
    command: "sudo timedatectl set-timezone Australia/Brisbane"
    command: "sudo systemctl restart crond.service"
-3

Connect AMI(amazon linux instance) via putty or ssh and execute the commands below;

sudo rm /etc/localtime
sudo ln -sf /usr/share/zoneinfo/Europe/Istanbul /etc/localtime
sudo reboot

Explanation of the procedure above is simply;

  1. remove localtime,
  2. update the timezone,
  3. reboot

Please notify that I've changed my timezone to Turkey's localtime, you can find your timezone by listing zoneinfo directory with the command below;

ls /usr/share/zoneinfo

or just check timezone abbrevetaions via wikipedia;

http://en.wikipedia.org/wiki/Category:Tz_database

You can also check out the related Amazon AWS documentation;

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-time.html

Note: I'm not sure that if this is the best practice or not (probably not), however I've applied the procedure I've written above and it's working for me.

Levent Divilioglu
  • 9,139
  • 5
  • 49
  • 96
  • 4
    The issue with resolving the issue this way is that AWS Elastic Beanstalk is meant to handle Load Balancing and Auto Scaling based on thresholds you define. Updating a single instance one time will only temporarily solve the problem, as Auto scaling will build and burn down new instances as they are needed. – ajzeffer Dec 12 '14 at 13:59