Jonny Cundall
Jonny Cundall

Reputation: 2622

aws get time according to cloud

I'm not seeing any answers for this when googling, so it's likely I'm asking a silly question, but here goes!

I want to call the aws api to get what time my cloud services think it is, because that appears to be different to my local server time (by a small but significant amount).

For a bit more context, I am running an automated test which needs to check that a new object is created in S3 as a result of the system under test working. The object which is created is given a timestamp in its name by AWS, based on the amazon server time. I use this timestamp to to distinguish the object from all the other objects in the bucket as it will be the only one with a time after the start of my test run. Unfortunately the time it gets given by amazon ends up being a few seconds before the test run started, because my server time is ahead of amazon's.

So if I could get the 'cloud' start time at the start of my test and compare that I won't have this problem.

EDIT

In case anyone has the same problem, I used this quick and dirty workaround in c#, in the spirit of the accepted answer but not quite as rigourous.

public async Task<DateTime> EstimateInternetTime()
{
    var httpClient = new HttpClient();
    var response = await httpClient.GetAsync("http://google.com");
    return response.Headers.Date.Value.UtcDateTime;
 }

Upvotes: 1

Views: 2114

Answers (1)

Matt Houser
Matt Houser

Reputation: 36073

There isn't a "cloud time", thus no way to ensure your timestamps match exactly to those used in AWS.

The best you can do is synchronize your clock using NTP to a public NTP server.

Amazon does have their own pool of NTP servers which you can use:

  • 0.amazon.pool.ntp.org
  • 1.amazon.pool.ntp.org
  • 3.amazon.pool.ntp.org
  • 2.amazon.pool.ntp.org

In many cases, EC2 instances are already configured to use these servers.

See the AWS documentation about configuring NTP on your Linux servers:

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

Upvotes: 3

Related Questions