LoginSignup
4
2

More than 5 years have passed since last update.

Workspaces を SSM のRun Command で操作

Last updated at Posted at 2018-09-05

概要

Workspaces の管理操作をRun Commandで行う
オンプレのWindowsサーバーにSSM-agentをインストールする手順とほぼ同じです。

流れ

  • SSM でアクティベーション作成
  • Workspaces に ssm-agent をインストール
  • Run Command 実行

SSM でアクティベーション作成

AWS マネージメントコンソールを開き、"Systems Manager"のサービスをクリックし、左側メニューの"アクティベーション"をクリックし、画面右上の"アクティベーションの作成"をクリックする

スクリーンショット 0030-09-05 20.12.45.png

設定内容を入れ、画面右下の"アクティベーションの作成"をクリックする
アクティベーション名:test-ws1(任意)
インスタンス数制限:5(任意)
アクティベーションの有効期限: 2018-09-08 T00:00+09:00 (任意)

スクリーンショット 0030-09-05 20.14.33.png

アクティベーションが作成される。Activation CodeとActivation IDをエディタなどにコピーしておく
※特にActivation Codeは注意書きされてる通りもう表示されない

スクリーンショット 0030-09-05 20.15.35.png

CLI

CLIでももちろん実施可能。以下はサンプルです。

aws ssm create-activation --default-instance-name MyWebServers --iam-role RunCommandServiceRole --registration-limit 10 --region us-east-2

参考資料

Workspaces に ssm-agent インストール

ssm-agentのインストールとアクティベーションを行う
以下の値を置換し、全体をコピーしてpowershellのターミナルに貼り付ける
アクティベーション作成時に取得したactivateion codeとactivation id、利用しているリージョンの値を入れる
activation-code に xxxxxxxxxxx
activation-id に yyyyyyyyyy
region に ap-northeast-1

$code = "activation-code"
$id = "activation-id"
$region = "region"
$dir = $env:TEMP + "\ssm"
New-Item -ItemType directory -Path $dir -Force
cd $dir
(New-Object System.Net.WebClient).DownloadFile("https://amazon-ssm-$region.s3.amazonaws.com/latest/windows_amd64/AmazonSSMAgentSetup.exe", $dir + "\AmazonSSMAgentSetup.exe")
Start-Process .\AmazonSSMAgentSetup.exe -ArgumentList @("/q", "/log", "install.log", "CODE=$code", "ID=$id", "REGION=$region") -Wait
Get-Content ($env:ProgramData + "\Amazon\SSM\InstanceData\registration")
Get-Service -Name "AmazonSSMAgent"

一箇所権限の問題でエラーが出てますが、Workspacesのため許可されてない部分かもしれません。get系のコマンドなのでそのまま進めます。

スクリーンショット 0030-09-05 22.07.48.png

AWS マネージメントコンソールの systems manager の画面で、アクティベーションされ workspaces のノードが登録されたことを確認する

スクリーンショット 0030-09-05 20.28.45.png

参考資料

Run Command 実行

AWS マネージメントコンソールの systems manager の画面で、左側のメニューから Run Commandをクリックし、画面右上の"コマンドを実行"をクリックする

スクリーンショット 0030-09-05 20.29.07.png

コマンドドキュメントの"AWS-RunPowerShellScript"にチェックを入れる

スクリーンショット 0030-09-05 20.29.44.png

次に"AWS-RunPowerShellScript"のパラメータを入力していく

実行するPowershellコマンドに

New-Item d:\hogehoge -itemType Directory

スクリーンショット 0030-09-05 22.19.16.png

ターゲットに先程登録したWorkspacesのノードを選択する

スクリーンショット 0030-09-05 20.35.42.png

画面右下の"実行"をクリックする

スクリーンショット 0030-09-05 20.35.53.png

実行状況。今回だとすぐ終わるので"成功"と出ている

スクリーンショット 0030-09-05 22.22.54.png

Workspaces上にもディレクトリができています。

スクリーンショット 0030-09-05 22.25.39.png

管理者がリモート操作で再起動させるなども出来ますね。

Restart-Computer -Force

ログオフはこれか

(Get-WmiObject -Class Win32_OperatingSystem -ComputerName .).Win32Shutdown(0)

Powershellのさまざまなコマンドが打てるのと、RunCommandで実行できるコマンドドキュメントも作成済のものが用意されていています。

CLI

CLIでももちろん実施可能。以下はサンプルです。

Send-SSMCommand -InstanceId 'Instance-ID' -DocumentName AWS-RunPowerShellScript -Comment 'listing services on the instance' -Parameter @{'commands'=@('Get-Service')}
aws ssm send-command --document-name "AWS-RunShellScript" --comment "listing services" --instance-ids "Instance-ID" --parameters commands="service --status-all" --region us-west-2 --output text

参考ドキュメント

4
2
0

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