AWS Systems Manager を雑に触った

かつて EC2 Systems Manager(SSM) と呼ばれていたものの機能拡張版。 いろいろ機能はあるもののざっと見た感じ、下記あたりは使えなくもないような気がしました。

  • Run Command
  • State Manager
  • Parameter Store

Run Command

SSM でいちばん有名なやつ、EC2 インスタンスにエージェントを入れておけばマネジメントコンソールや AWS CLI 経由でコマンドが実行できます。

aws ssm send-command \
  --region ap-northeast-1 \
  --document-name AWS-RunShellScript \
  --targets Key=tag:Env,Values=test \
  --timeout-seconds 600 \
  --max-concurrency 50 \
  --max-errors 0 \
  --parameters '{
    "commands":["yum -y install awscli"],
    "workingDirectory":["/tmp"],
    "executionTimeout":["3600"]
  }'

aws ssm list-commands --region ap-northeast-1 --max-items 1 | jq '.Commands[].CommandId' -r
#=> 63842a22-71a4-4018-bc80-eba3120ea7c3

aws ssm list-command-invocations --region ap-northeast-1 --command-id 63842a22-71a4-4018-bc80-eba3120ea7c3
aws ssm list-command-invocations --region ap-northeast-1 --details --command-id 63842a22-71a4-4018-bc80-eba3120ea7c3 |
  jq -r '.CommandInvocations[] | "### " + .InstanceId + "\n" + .CommandPlugins[].Output'
#=> ### i-07c712d6fbcb5a9e1
#=> Loaded plugins: fastestmirror
#=> Loading mirror speeds from cached hostfile
#=>  * base: ftp.iij.ad.jp
#=>  * epel: ftp.iij.ad.jp
#=>  * extras: ftp.iij.ad.jp
#=>  * updates: ftp.iij.ad.jp
#=> Package awscli-1.11.133-1.el7.noarch already installed and latest version
#=> Nothing to do

RunCommand を使ってシェルっぽくコマンドを実行するアイデアもあるようです。

State Manager

本来の用途は、インスタンスをあるべき状態に維持するために定期的な処理を行なう、ものらしいですが簡易 cron として使えそうです。

aws ssm create-association \
  --region ap-northeast-1 \
  --name AWS-RunShellScript \
  --association-name oreore-job \
  --targets Key=tag:Env,Values=test \
  --schedule-expression 'rate(30 minutes)' \
  --parameters '{
    "commands":["logger oreore-job"],
    "workingDirectory":["/tmp"],
    "executionTimeout":["60"]
  }'
sudo tail -n +1 -f /var/log/messages | grep oreore-job
#=> Mar  7 04:34:03 localhost logger: oreore-job
#=> Mar  7 04:34:04 localhost amazon-ssm-agent: "logger oreore-job"
#=> Mar  7 05:04:03 localhost amazon-ssm-agent: "logger oreore-job"
#=> Mar  7 05:04:03 localhost logger: oreore-job

Parameter Store

それなりにセキュアな KVS です。類似のプロダクトとしては HashiCorp Vault とかでしょうか。

aws ssm put-parameter \
  --region ap-northeast-1 \
  --type SecureString \
  --overwrite \
  --name /oreore/himitu \
  --value naisyo

type で SecureString を指定すると自動的に KMS の暗号化キーで暗号化されます。

aws ssm get-parameter \
  --region ap-northeast-1 \
  --name /oreore/himitu \
  --query Parameter.Value \
  --output text
#=> ABCDEF0123456789...

aws ssm get-parameter \
  --region ap-northeast-1 \
  --name /oreore/himitu \
  --with-decryption \
  --query Parameter.Value \
  --output text
#=> naisyo

単に aws ssm get-parameter で値を取り出して使えるだけではなく、Run Command でパラメータの値を参照できたりします。 ただし、その場合は SecureString は使えません。

aws ssm put-parameter \
  --region ap-northeast-1 \
  --type String \
  --overwrite \
  --name /oreore/koukai \
  --value yametokyayokatta

aws ssm send-command \
  --region ap-northeast-1 \
  --document-name AWS-RunShellScript \
  --targets Key=tag:Env,Values=test \
  --parameters '{"commands":["echo {{ssm:/oreore/koukai}}"]}'

aws ssm list-command-invocations --region ap-northeast-1 --details --max-items 1 |
  jq -r '.CommandInvocations[].CommandPlugins[].Output'
#=> yametokyayokatta

さいごに

その他の機能はよくわからない&使わ無さそうに思いました。

SSM エージェント自体は Amazon Linux なら最初から入っている(らしい)し、CentOS でも rpm からサッと入れられるので、とりあえず入れておくだけ入れておいてもいいかもしれません。