30
votes

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?

10
It is generally best practice to leave servers set to UTC and manage time zones in application code. stackoverflow.com/questions/2532729/…Matt Johnson-Pint

10 Answers

36
votes

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.

18
votes

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!

8
votes

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

5
votes

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
2
votes

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.

1
votes

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
votes

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.

0
votes

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.

0
votes

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
votes

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.