EC2インスタンスには、Amazon SSM Agent をインストール済みです。
また、EC2インスタンスには AdministratorAccess ポリシーを設定しています。
今回は、awscli で試します。
下記のマニュアルを参考にしました。
1.RunCommad が使用できるECインスタンスを確認
ssmエージェントが動いてるEC2インスタンスを確認します。
下記のコマンドを実行して、表示されたインスタンスは、RunCommand が使用できます。
[root@centos0702 codedeploy]# aws ssm describe-instance-information --output text --query "InstanceInformationList[*]" 2.0.617.1 ip-10-0-10-133.ec2.internal 10.0.10.133 i-0640ff2f366174074 True 1484800160.37 Online CentOS Linux 6.7 (Final) EC2Instance 2.0.617.1 ip-10-0-11-52.ec2.internal 10.0.11.52 i-083f2235b506dfd49 True 1484800159.83 Online CentOS Linux 6.7 (Final) EC2Instance
インスタンスIDを指定して詳細を見たい場合は下記コマンドです。
[root@centos0702 codedeploy]# aws ssm describe-instance-information --instance-information-filter-list key=InstanceIds,valueSet=i-0640ff2f366174074 { "InstanceInformationList": [ { "IsLatestVersion": true, "ComputerName": "ip-10-0-10-133.ec2.internal", "PingStatus": "Online", "InstanceId": "i-0640ff2f366174074", "IPAddress": "10.0.10.133", "ResourceType": "EC2Instance", "AgentVersion": "2.0.617.1", "PlatformVersion": "6.7 (Final)", "PlatformName": "CentOS", "PlatformType": "Linux", "LastPingDateTime": 1484800160.367 } ] }
2.コマンドを実行する
インスタンスIDを指定して ifconfig コマンドを実行してみます。
[root@centos0702 codedeploy]# aws ssm send-command --instance-ids i-0640ff2f366174074 --document-name "AWS-RunShellScript" --comment "IP config" --parameters commands=ifconfig
{
"Command": {
"Comment": "IP config",
"Status": "Pending",
"MaxErrors": "0",
"Parameters": {
"commands": [
"ifconfig"
]
},
"ExpiresAfter": 1484804322.856,
"ServiceRole": "",
"DocumentName": "AWS-RunShellScript",
"TargetCount": 1,
"OutputS3BucketName": "",
"NotificationConfig": {
"NotificationArn": "",
"NotificationEvents": [],
"NotificationType": ""
},
"CompletedCount": 0,
"Targets": [],
"StatusDetails": "Pending",
"ErrorCount": 0,
"OutputS3KeyPrefix": "",
"RequestedDateTime": 1484800722.856,
"CommandId": "3a812382-f60f-430b-9bfe-54f26a3bea4c",
"InstanceIds": [
"i-0640ff2f366174074"
],
"MaxConcurrency": "50"
}
}
コマンドが正常終了したかステータスを確認します。
上記の CommandId を指定します。
[root@centos0702 codedeploy]# aws ssm list-commands --command-id 3a812382-f60f-430b-9bfe-54f26a3bea4c
{
"Commands": [
{
"Comment": "IP config",
"Status": "Success",
"MaxErrors": "0",
"Parameters": {
"commands": [
"ifconfig"
]
},
"ExpiresAfter": 1484804322.856,
"ServiceRole": "",
"DocumentName": "AWS-RunShellScript",
"TargetCount": 1,
"OutputS3BucketName": "",
"NotificationConfig": {
"NotificationArn": "",
"NotificationEvents": [],
"NotificationType": ""
},
"CompletedCount": 1,
"Targets": [],
"StatusDetails": "Success",
"ErrorCount": 0,
"OutputS3KeyPrefix": "",
"RequestedDateTime": 1484800722.856,
"CommandId": "3a812382-f60f-430b-9bfe-54f26a3bea4c",
"InstanceIds": [
"i-0640ff2f366174074"
],
"MaxConcurrency": "50"
}
]
}
Status が Success なので ifconfig コマンドの実行は正常終了したようです。
ifconfig コマンドの標準出力をみたい場合は以下のとおり。
Output が ifconfig の標準出力です。
[root@centos0702 codedeploy]# aws ssm list-command-invocations --command-id 3a812382-f60f-430b-9bfe-54f26a3bea4c --details
{
"CommandInvocations": [
{
"Comment": "IP config",
"Status": "Success",
"CommandPlugins": [
{
"Status": "Success",
"ResponseStartDateTime": 1484800723.395,
"StandardErrorUrl": "",
"OutputS3BucketName": "",
"OutputS3Region": "us-east-1",
"OutputS3KeyPrefix": "",
"ResponseCode": 0,
"Output": "eth0 Link encap:Ethernet HWaddr 0A:48:F5:B1:B3:96 \n inet addr:10.0.10.133 Bcast:10.0.10.255 Mask:255.255.255.0\n UP BROADCAST RUNNING MULTICAST MTU:9001 Metric:1\n RX packets:1682 errors:0 dropped:0 overruns:0 frame:0\n TX packets:1963 errors:0 dropped:0 overruns:0 carrier:0\n collisions:0 txqueuelen:1000 \n RX bytes:429330 (419.2 KiB) TX bytes:323357 (315.7 KiB)\n Interrupt:160 \n\nlo Link encap:Local Loopback \n inet addr:127.0.0.1 Mask:255.0.0.0\n UP LOOPBACK RUNNING MTU:65536 Metric:1\n RX packets:0 errors:0 dropped:0 overruns:0 frame:0\n TX packets:0 errors:0 dropped:0 overruns:0 carrier:0\n collisions:0 txqueuelen:0 \n RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)\n\n",
"ResponseFinishDateTime": 1484800723.402,
"StatusDetails": "Success",
"StandardOutputUrl": "",
"Name": "aws:runShellScript"
}
],
"ServiceRole": "",
"InstanceId": "i-0640ff2f366174074",
"DocumentName": "AWS-RunShellScript",
"NotificationConfig": {
"NotificationArn": "",
"NotificationEvents": [],
"NotificationType": ""
},
"StatusDetails": "Success",
"StandardOutputUrl": "",
"StandardErrorUrl": "",
"InstanceName": "",
"CommandId": "3a812382-f60f-430b-9bfe-54f26a3bea4c",
"RequestedDateTime": 1484800722.962
}
]
}
3. 複数のインスタンでコマンドを実行
TAGを指定して、複数のインスタンスでコマンドを実行してみます。
下記は、Groupタグに "demo" が設定されたインスタンスでコマンドを実行します。
[root@centos0702 codedeploy]# aws ssm send-command --targets "Key=tag:Group,Values=demo" --document-name "AWS-RunShellScript" --comment "hostname" --parameters "commands=uname -n" { "Command": { "Comment": "hostname", "Status": "Pending", "MaxErrors": "0", "Parameters": { "commands": [ "uname -n" ] }, "ExpiresAfter": 1484806220.112, "ServiceRole": "", "DocumentName": "AWS-RunShellScript", "TargetCount": 0, "OutputS3BucketName": "", "NotificationConfig": { "NotificationArn": "", "NotificationEvents": [], "NotificationType": "" }, "CompletedCount": 0, "Targets": [ { "Values": [ "demo" ], "Key": "tag:Group" } ], "StatusDetails": "Pending", "ErrorCount": 0, "OutputS3KeyPrefix": "", "RequestedDateTime": 1484802620.112, "CommandId": "725c31fa-eb3c-4aed-bee5-568492969d52", "InstanceIds": [], "MaxConcurrency": "50" } }
コマンド実行結果の詳細は以下のとおり。
[root@centos0702 codedeploy]# aws ssm list-command-invocations --command-id 725c31fa-eb3c-4aed-bee5-568492969d52 --details { "CommandInvocations": [ { "Comment": "hostname", "Status": "Success", "CommandPlugins": [ { "Status": "Success", "ResponseStartDateTime": 1484802620.708, "StandardErrorUrl": "", "OutputS3BucketName": "", "OutputS3Region": "us-east-1", "OutputS3KeyPrefix": "", "ResponseCode": 0, "Output": "ip-10-0-10-133.ec2.internal\n", "ResponseFinishDateTime": 1484802620.712, "StatusDetails": "Success", "StandardOutputUrl": "", "Name": "aws:runShellScript" } ], "ServiceRole": "", "InstanceId": "i-0640ff2f366174074", "DocumentName": "AWS-RunShellScript", "NotificationConfig": { "NotificationArn": "", "NotificationEvents": [], "NotificationType": "" }, "StatusDetails": "Success", "StandardOutputUrl": "", "StandardErrorUrl": "", "InstanceName": "ip-10-0-10-133.ec2.internal", "CommandId": "725c31fa-eb3c-4aed-bee5-568492969d52", "RequestedDateTime": 1484802620.349 }, { "Comment": "hostname", "Status": "Success", "CommandPlugins": [ { "Status": "Success", "ResponseStartDateTime": 1484802620.577, "StandardErrorUrl": "", "OutputS3BucketName": "", "OutputS3Region": "us-east-1", "OutputS3KeyPrefix": "", "ResponseCode": 0, "Output": "ip-10-0-11-52.ec2.internal\n", "ResponseFinishDateTime": 1484802620.581, "StatusDetails": "Success", "StandardOutputUrl": "", "Name": "aws:runShellScript" } ], "ServiceRole": "", "InstanceId": "i-083f2235b506dfd49", "DocumentName": "AWS-RunShellScript", "NotificationConfig": { "NotificationArn": "", "NotificationEvents": [], "NotificationType": "" }, "StatusDetails": "Success", "StandardOutputUrl": "", "StandardErrorUrl": "", "InstanceName": "ip-10-0-11-52.ec2.internal", "CommandId": "725c31fa-eb3c-4aed-bee5-568492969d52", "RequestedDateTime": 1484802620.252 } ] }
4. AWSコンソールでコマンド履歴を見る
AWSコンソールでコマンド履歴をみると下図のとおり。
コマンドの出力内容を見たい場合は、[出力の表示]をクリックします。