Ubuntu EC2 configure Amazon Time Sync Service for Amazon AWS
Having problems keeping your server time in sync? or a project requiring accurate timing? If your using a Amazon EC2 instance you can swap to using Chrony and Amazon Time Sync using the below commands.
Code is provided as is, I cannot be helf responsible for any issues which may occur
# Disable timesyncd
systemctl stop systemd-timesyncd
systemctl disable systemd-timesyncd
# Install chrony
apt install chrony -y
# Edit chrony config
sed -i '1s/^/server 169.254.169.123 prefer iburst minpoll 4 maxpoll 4\n/' /etc/chrony/chrony.conf
# Restart chrony
Today I ran into an issue where a Laravel project hosted on an Amazon EC2 instance was creating records, but the created_at time stamps where in the wrong order. For example
1st record -> 12/12/2020 10:30:20
2nd record -> 12/12/2020 10:30:10
Record two occured after the 1st record but had a timestamp indicating otherwise. After some research I found the time sync service on the server from a fresh install was “timesyncd”, and had up to a 30 minute delay between syncing with the time server, this could allow the server to become out of sync and in this case, the time sync updated during the period between the two records being created, causing the 2nd record to have a timestamp before the first record.
Chrony & Amazon Time Sync
Chrony is a time sync service based on an improved NTP technology, lucky amazon offers a time sync service which had very regular check intervals minimising the chances of the servers time becomine out of sync. I create a simple script below which can be used on a fresh EC2 server with Ubuntu to swap to Chrony and implement the Amazon Time Sync config.
Read more about Amazon Time Sync Service on the Amazon AWS website