■ はじめに
イベントリブンな自動化ツールである StackStorm は、aws pack(拡張機能)をインストールすることによって、AWSを連携して様々な操作ができるようになります。
この記事では、StackStormのコマンドの実行によりEC2 インスタンスの情報を取得する例をご紹介します。
■ 準備
StackStorm のインストール
今回は、ansible でインストールした環境を利用します。
(参考)
tekunabe.hatenablog.jp
Pack のインストールなど
以下のページを参考にして、Pack のアクセスキーなどの設定を行います。
GitHub - StackStorm-Exchange/stackstorm-aws: st2 content pack containing Amazon Web Services integrations.
アクセスキーなどの認証情報の取扱いには十分ご注意ください。
■ st2 run コマンドで情報取得
それでは、いくつかの例で EC2 インスタンスの情報を取得してみます。
対象リージョンのすべての EC2 インスタンス
特にフィルターを指定せず、対象リージョンのすべてのインスタンスの情報を取得する場合は、以下のようなコマンドを実行します。
・実行コマンド
st2 run aws.ec2_describe_instances
・情報取得結果例
id: xxxxxxxxxxxxx
status: succeeded
parameters: None
result:
exit_code: 0
result:
- Reservations:
- Groups: []
Instances:
- AmiLaunchIndex: 0
Architecture: x86_64
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
AttachTime: '2018-03-12T13:22:08+00:00'
DeleteOnTermination: true
Status: attached
VolumeId: vol-xxx
ClientToken: 'xxx'
EbsOptimized: false
EnaSupport: false
Hypervisor: xen
ImageId: ami-77152012
InstanceId: i-xxx
InstanceType: t2.medium
KeyName: xxx
LaunchTime: '2018-03-20T00:29:23+00:00'
Monitoring:
State: disabled
NetworkInterfaces:
- Attachment:
AttachTime: '2018-03-12T13:22:07+00:00'
AttachmentId: eni-attach-xxx
DeleteOnTermination: true
DeviceIndex: 0
Status: attached
Description: ''
Groups:
- GroupId: sg-xxx
GroupName: Cisco Cloud Services Router -CSR- 1000V - AX Pkg- Max Performance-16-7-1-AutogenByAWSMP-
Ipv6Addresses: []
MacAddress: 02:30:07:xx:xx:xx
NetworkInterfaceId: eni-xxx
OwnerId: xxx
PrivateDnsName: ip-172-31-6-206.us-east-2.compute.internal
PrivateIpAddress: 172.31.6.206
PrivateIpAddresses:
- Primary: true
PrivateDnsName: ip-172-31-6-206.us-east-2.compute.internal
PrivateIpAddress: 172.31.6.206
SourceDestCheck: true
Status: in-use
SubnetId: subnet-xxx
VpcId: vpc-xxx
Placement:
AvailabilityZone: us-east-2a
GroupName: ''
Tenancy: default
PrivateDnsName: ip-172-31-6-206.us-east-2.compute.internal
PrivateIpAddress: 172.31.6.206
ProductCodes:
- ProductCodeId: xxx
ProductCodeType: marketplace
PublicDnsName: ''
RootDeviceName: /dev/xvda
RootDeviceType: ebs
SecurityGroups:
- GroupId: sg-xxx
GroupName: Cisco Cloud Services Router -CSR- 1000V - AX Pkg- Max Performance-16-7-1-AutogenByAWSMP-
SourceDestCheck: true
State:
Code: 80
Name: stopped
StateReason:
Code: Client.UserInitiatedShutdown
Message: 'Client.UserInitiatedShutdown: User initiated shutdown'
StateTransitionReason: User initiated (2018-03-20 00:58:19 GMT)
SubnetId: subnet-xxx
Tags:
- Key: Name
Value: xxx
VirtualizationType: hvm
VpcId: vpc-xxx
OwnerId: xxx
ReservationId: rxxx
(略) 2つめ以降のインスタンス情報が続く
ResponseMetadata:
HTTPHeaders:
content-type: text/xml;charset=UTF-8
date: Tue, 20 Mar 2018 14:38:03 GMT
server: AmazonEC2
transfer-encoding: chunked
vary: Accept-Encoding
HTTPStatusCode: 200
RequestId: xxx-xxx-xxx-xxx
RetryAttempts: 0
stderr: ''
stdout: ''
実行中(instance-state-name
が running
)のインスタンスの情報を取得する場合は、以下のようなコマンドを実行します。
・実行コマンド
st2 run aws.ec2_describe_instances Filters='[{"Name":"instance-state-name","Values":["running"]}]'
その他のフィルタ
その他のフィルタについては、以下のコマンドを実行して確認します。
st2 action get aws.ec2_describe_instances
(抜粋)
| "Filters": { |
| "type": "array", |
| "description": "One or more filters. affinity - |
| The affinity setting for an instance running on a Dedicated |
| Host (default | host). architecture - The instance |
| architecture (i386 | x86_64). association.public-ip - The |
| address of the Elastic IP address (IPv4) bound to the |
| network interface. association.ip-owner-id - The owner of |
| the Elastic IP address (IPv4) associated with the network |
| interface. association.allocation-id - The allocation ID |
| returned when you allocated the Elastic IP address (IPv4) |
| for your network interface. association.association-id - |
| The association ID returned when the network interface was |
| associated with an IPv4 address. availability-zone - The |
| Availability Zone of the instance. block-device-mapping |
| .attach-time - The attach time for an EBS volume mapped to |
| the instance, for example, 2010-09-15T17:15:20.000Z. |
例えば、インスタンスID i-dummy1
と i-dummy2
の情報を取得る場合は、以下のようなコマンドを実行します。
st2 run aws.ec2_describe_instances Filters='[{"Name":"instance-id", "Values":["i-dummy1", "i-dummy2"]}]'
-j
オプションを使用すると、JSON形式で出力できます。
・コマンド実行例(Fitlers
などの他のオプションとも併用可)
st2 run aws.ec2_describe_instances -j
■ まとめ
StackStorm で EC2 インスタンスの情報を取得することができました。
aws pack ではほかにも様々な機能があります。
aciont の一覧は st2 action list -p aws
で確認することができます。
今回は、手動でのコマンド実行で操作しましたが、StackStormが持つイベントドリブンな仕組みを活用することで、利用用途が広がると思います。