LoginSignup
13

More than 5 years have passed since last update.

AWS CLIで時刻を扱うときの定型文

Last updated at Posted at 2015-04-21

非常に今更な気がしますが、よく忘れるので備忘録として記します。

時刻指定 is 何

AWS CLIでCloudWatchなどでデータを取得する際には開始時刻・終了時刻を指定する必要があります。
awscliのhelpでは以下のような記述があります。

       --start-time (timestamp)
          The time stamp to use for determining the first datapoint to return.
          The value specified is inclusive; results  include  datapoints  with
          the time stamp specified.

       --end-time (timestamp)
          The  time stamp to use for determining the last datapoint to return.
          The value specified is exclusive; results will include datapoints up
          to the time stamp specified.

使用例も書かれています。

EXAMPLES
       To get the CPU utilization per EC2 instance

       The following example uses the get-metric-statistics command to get the
       CPU utilization for an EC2 instance with  the  ID  i-abcdef.  For  more
       examples  using  the  get-metric-statistics command, see Get Statistics
       for a Metric in the Amazon CloudWatch Developer Guide.

          aws cloudwatch get-metric-statistics --metric-name CPUUtilization --start-time 2014-04-08T23:18:00 --end-time 2014-04-09T23:18:00 --period 3600 --namespace AWS/EC2 --statistics Maximum --dimensions Name=InstanceId,Value=i-abcdef

yyyy-mm-ddTHH:MM:SS とあります。実際にはこの後にTZも追加できます。

実際に取得してみる

Linuxで以下のような date を実行すると、上記の時刻指定で必要な結果が得られます。

$ date -Iseconds --date '6 minutes ago'
2015-04-21T11:40:39+0900

この形式でAWS CLIに引き渡せば解釈してくれます。上記検証を行ったLinuxサーバではJSTを採用してますので、末尾が +0900 となっています。
TZを調整してみると以下のように挙動が変わります。

$ TZ=UTC date -Iseconds --date '6 minutes ago'
2015-04-21T02:54:26+0000
$ TZ=Asia/Tokyo date -Iseconds --date '6 minutes ago'
2015-04-21T12:26:47+0900
$ TZ=US/Pacific date -Iseconds --date '6 minutes ago'
2015-04-20T20:27:49-0700

使用例

バッチ処理などで現在時刻から遡ってn分、などの指定を行う場合にご利用いただけます。

aws cloudwatch get-metric-statistics \
    --namespace AWS/RDS \
    --metric-name CPUUtilization \
    --start-time `date --iso-8601=seconds --date '6 minutes ago'` \
    --end-time `date --iso-8601=seconds --date '1 minutes ago'` \
    --period 300 \
    --statistics "Maximum" \
    --dimensions 'Name="DBInstanceIdentifier",Value="foobar"'

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13