0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Steampipeで攻めるAWS環境の可視化 ─ 単一アカウントでの実行編

Posted at

はじめに

下記記事で、steampipeを使うことで、AWS環境のリソース情報をダッシュボード化することができると知ったので、その検証をやっていきます。
下記の記事は、3年前の記事の為、導入手順が異なりますので、注意が必要です。

Steampipeとは?

Steampipe は、AWS をはじめとしたクラウド環境の情報を SQL を使って見える化できる オープンソースツールです。
通常、AWS のリソース状況を知る際は、マネジメントコンソール や AWS CLI を使って、必要な情報をその都度取りいくことなるのですが、Steampipe を使用することで、SQL クエリを実行するだけで欲しい情報を一括で取得・分析 できます。
また、Powerpipe と組み合わせることで、SQL で集めたデータを ダッシュボード形式で可視化 することもできるようになります。このページでは、その具体的な方法を紹介します。
※元記事では、ダッシュボード生成もSteampipeコマンドで生成できていたが、現時点ではPowerpipeに移行している。

前提条件

  • 実行環境は、AWS 環境内の VPC 上に立てた EC2 サーバで実行するものとする
  • サーバの OS 情報は以下のとおり
[root@ip-10-192-0-236 ~]# cat /etc/amazon-linux-release
Amazon Linux release 2023.8.20250818 (Amazon Linux)
  • 当該サーバには、ReadOnlyAccess ポリシーをアタッチした IAM ロール を割り当て済み

実行手順

(1)steampipe環境のセットアップ

1. ec2-userに切り替える
※steampipeは、rootユーザでの実行はサポートされていないので、必ず一般ユーザで実行する必要がある。今回はec2-userで実行する。

[root@ip-10-192-0-236 ~]# su - ec2-user

2. steampipeのインストール

[ec2-user@ip-10-192-0-236 ~]$ sudo /bin/sh -c "$(curl -fsSL https://steampipe.io/install/steampipe.sh)"

3. steampipeのバージョン確認

[ec2-user@ip-10-192-0-236 ~]$ steampipe -v
Steampipe v2.1.0

4. AWS用のプラグインをインストール

[ec2-user@ip-10-192-0-236 ~]$ steampipe plugin install steampipe aws

steampipe                      [====================================================================] Done
aws                            [====================================================================] Done

Installed plugin: aws@latest v1.23.0
Documentation:    https://hub.steampipe.io/plugins/turbot/aws

Installed plugin: steampipe@latest v1.0.0
Documentation:    https://hub.steampipe.io/plugins/turbot/steampipe

5. steampipeの定義ファイルを修正する。
デフォルトの設定では、バージニア北部の情報しかとってこない設定になっているので、「東京」「バージニア北部」に書き換える

~/.steampipe/config/aws.spc
connection "aws" {
  plugin  = "aws"
  regions = ["us-east-1"]
}

↓下記に変更

connection "aws_Multi_region" {
  plugin  = "aws"
  regions = ["ap-northeast-1", "us-east-1"]
}

6. クエリの実行
「steampipe query」で、対話モード (インタラクティブモード)に切り替え、そこでクエリを実行してます。
例として、VPC環境の情報をクエリした結果を記載します。※私の環境なので、一部マスクして表示してます。

[ec2-user@ip-10-192-0-236 ~]$ steampipe query
Welcome to Steampipe v2.1.0
For more information, type .help
> select
  tags ->> 'Name' as name,
  region,
  vpc_id,
  cidr_block,
  dhcp_options_id,
  is_default
from
  aws_vpc ;
+-----------------------------+----------------+-----------------------+------------------+------------------------+------------+
| name                        | region         | vpc_id                | cidr_block       | dhcp_options_id        | is_default |
+-----------------------------+----------------+-----------------------+------------------+------------------------+------------+
| test-dev-vpc                | ap-northeast-1 | vpc-0XXXXXXXXXXXXXXXX | 10.0.0.0/16      | dopt-0XXXXXXXXXXXXXXXX | false      |
| VPC-3                       | ap-northeast-1 | vpc-0XXXXXXXXXXXXXXXX | 10.3.0.0/16      | dopt-0XXXXXXXXXXXXXXXX | false      |
| VPC-hub                     | ap-northeast-1 | vpc-0XXXXXXXXXXXXXXXX | 10.10.0.0/16     | dopt-0XXXXXXXXXXXXXXXX | false      |
| TESTvpc                     | ap-northeast-1 | vpc-0XXXXXXXXXXXXXXXX | 10.2.0.0/21      | dopt-0XXXXXXXXXXXXXXXX | false      |
| <null>                      | ap-northeast-1 | vpc-0XXXXXXXXXXXXXXXX | 172.31.0.0/16    | dopt-0XXXXXXXXXXXXXXXX | true       |
| VPC-4                       | ap-northeast-1 | vpc-0XXXXXXXXXXXXXXXX | 10.3.0.0/16      | dopt-0XXXXXXXXXXXXXXXX | false      |
| test-sbx-vpc                | ap-northeast-1 | vpc-0XXXXXXXXXXXXXXXX | 10.0.0.0/16      | dopt-0XXXXXXXXXXXXXXXX | false      |
| VPC-2                       | ap-northeast-1 | vpc-0XXXXXXXXXXXXXXXX | 10.2.0.0/16      | dopt-0XXXXXXXXXXXXXXXX | false      |
| test2-VPC                   | ap-northeast-1 | vpc-0XXXXXXXXXXXXXXXX | 192.168.215.0/24 | dopt-0XXXXXXXXXXXXXXXX | false      |
| test-test-vpc               | ap-northeast-1 | vpc-0XXXXXXXXXXXXXXXX | 10.0.0.0/16      | dopt-0XXXXXXXXXXXXXXXX | false      |
| test-network-vpc            | ap-northeast-1 | vpc-0XXXXXXXXXXXXXXXX | 10.1.0.0/16      | dopt-0XXXXXXXXXXXXXXXX | false      |
| test-dev-VPC                | ap-northeast-1 | vpc-0XXXXXXXXXXXXXXXX | 10.192.0.0/16    | dopt-0XXXXXXXXXXXXXXXX | false      |
| VPC-1                       | ap-northeast-1 | vpc-0XXXXXXXXXXXXXXXX | 10.1.0.0/16      | dopt-0XXXXXXXXXXXXXXXX | false      |
| <null>                      | us-east-1      | vpc-0XXXXXXXXXXXXXXXX | 172.31.0.0/16    | dopt-0XXXXXXXXXXXXXXXX | true       |
+-----------------------------+----------------+-----------------------+------------------+------------------------+------------+

上記のクエリは、「aws_vpc」というテーブルから情報をとってきてます。
指定できるテーブルの一覧は、下記コマンドで確認できます。

> .tables

各テーブル内のカラム情報は、「 .inspect テーブル名」で確認できます。(下記に、例として、VPC用のテーブルに対して、クエリした結果をのせときます)

> .inspect aws_vpc
+---------------------------------+---------+----------------------------------------------------------------------------------+
| column                          | type    | description                                                                      |
+---------------------------------+---------+----------------------------------------------------------------------------------+
| _ctx                            | jsonb   | Steampipe context in JSON form.                                                  |
| account_id                      | text    | The AWS Account ID in which the resource is located.                             |
| akas                            | jsonb   | Array of globally unique identifier strings (also known as) for the resource.    |
| arn                             | text    | The Amazon Resource Name (ARN) specifying the vpc.                               |
| cidr_block                      | cidr    | The primary IPv4 CIDR block for the VPC.                                         |
| cidr_block_association_set      | jsonb   | Information about the IPv4 CIDR blocks associated with the VPC.                  |
| dhcp_options_id                 | text    | Contains the ID of the set of DHCP options, associated with the VPC.             |
| instance_tenancy                | text    | The allowed tenancy of instances launched into the VPC.                          |
| ipv6_cidr_block_association_set | jsonb   | Information about the IPv6 CIDR blocks associated with the VPC.                  |
| is_default                      | boolean | Indicates whether the VPC is the default VPC.                                    |
| owner_id                        | text    | Contains ID of the AWS account that owns the VPC.                                |
| partition                       | text    | The AWS partition in which the resource is located (aws, aws-cn, or aws-us-gov). |
| region                          | text    | The AWS Region in which the resource is located.                                 |
| sp_connection_name              | text    | Steampipe connection name.                                                       |
| sp_ctx                          | jsonb   | Steampipe context in JSON form.                                                  |
| state                           | text    | Contains the current state of the VPC.                                           |
| tags                            | jsonb   | A map of tags for the resource.                                                  |
| tags_src                        | jsonb   | A list of tags that are attached with the VPC.                                   |
| title                           | text    | Title of the resource.                                                           |
| vpc_id                          | text    | The ID of the VPC.                                                               |
+---------------------------------+---------+----------------------------------------------------------------------------------+

上記で、steampipeのクエリ結果のイメージが湧いたところで、次章ではそのデータを使ってWEBダッシュボード化していきます。
ちなみに、steampipeのインタラクティブモードは、Ctrl + Dで抜けられます。

(2)クエリの検索結果のダッシュボード

1. powerpipeのインストール
steampipeのクエリ結果をダッシュボード化するには、「powerpipe」が必要になるので、そのインストールをおこないます。

[ec2-user@ip-10-192-0-236 ~]$ curl -sSL https://powerpipe.io/install/powerpipe.sh | sudo bash

2. powerpipeのバージョン確認

[ec2-user@ip-10-192-0-236 ~]$ powerpipe --version
Powerpipe v1.3.1

3. ダッシュボード用のワークフォルダを作成し、移動

[ec2-user@ip-10-192-0-236 ~]$ mkdir work1 && cd work1 && pwd
/home/ec2-user/work1

4. カレントディレクトリで初期化を実施
初期化をすることで、mod.ppファイルというPowerpipe モジュールの設定ファイルが生成される。

[ec2-user@ip-10-192-0-236 work1]$ powerpipe mod init
Created mod definition file '/home/ec2-user/work1/mod.pp'

[ec2-user@ip-10-192-0-236 work1]$ ls -la
total 4
drwxr-xr-x. 2 ec2-user ec2-user  20 Sep  5 05:38 .
drwx------. 7 ec2-user ec2-user 153 Sep  5 05:38 ..
-rw-r--r--. 1 ec2-user ec2-user  34 Sep  5 05:38 mod.pp

5. ダッシュボード設定を定義
「aws_vpc_dashboard.sp」ファイルを作成し、そこでVPCの関連情報を定義してみました。

aws_vpc_dashboard.sp
dashboard "aws_network_overview" {
  title = "AWS Network Overview"

  # --- VPC ---
  container {
    width = 12
    table {
      title = "VPC"
      sql = <<-EOQ
        select
          tags ->> 'Name' as name,
          region,
          vpc_id,
          cidr_block,
          dhcp_options_id,
          is_default
        from
          aws_vpc ;
      EOQ
    }
  }

  # --- Subnets ---
  container {
    width = 12
    table {
      title = "Subnets"
      sql = <<-EOQ
        select
          tags ->> 'Name' as name,
          availability_zone,
          subnet_id,
          cidr_block,
          vpc_id,
          available_ip_address_count,
          region
        from
          aws_vpc_subnet
        order by
          region,
          vpc_id;
      EOQ
    }
  }

  # --- Route Tables ---
  container {
    width = 12
    table {
      title = "Route Tables"
      sql = <<-EOQ
        select
          coalesce(rtt.tags ->> 'Name', rtt.title, rtt.route_table_id, 'NONE') as name_tag,
          rtt.vpc_id,
          rtt.route_table_id,
          coalesce(
            (r.destination_cidr_block)::text,
            (r.destination_ipv6_cidr_block)::text,
            (r.destination_prefix_list_id)::text,
            'NONE'
          ) as network_range,
          coalesce(
            r.gateway_id,
            r.nat_gateway_id,
            r.transit_gateway_id,
            r.vpc_peering_connection_id,
            r.egress_only_internet_gateway_id,
            r.instance_id,
            r.network_interface_id,
            'NONE'
          ) as target,
          coalesce(
            (
              select string_agg(distinct (assoc ->> 'SubnetId'), ',')
              from jsonb_array_elements(rtt.associations) as assoc
              where assoc ? 'SubnetId' and (assoc ->> 'SubnetId') <> ''
            ),
            'NONE'
          ) as subnets,
          r.region,
          r.origin
        from
          aws_vpc_route_table rtt
        left join
          aws_vpc_route r
        on
          r.route_table_id = rtt.route_table_id
        order by
          r.region,
          name_tag;
      EOQ
    }
  }
}

7. Steampipe をバックエンドモードとして起動

[ec2-user@ip-10-192-0-236 work1]$ steampipe service start
Steampipe service is running:

Database:

  Host(s):            127.0.0.1, ::1, 10.192.0.236
  Port:               9193
  Database:           steampipe
  User:               steampipe
  Password:           ********* [use --show-password to reveal]
  Connection string:  postgres://steampipe@127.0.0.1:9193/steampipe

Managing the Steampipe service:

  # Get status of the service
  steampipe service status

  # View database password for connecting from another machine
  steampipe service status --show-password

  # Restart the service
  steampipe service restart

  # Stop the service
  steampipe service stop

8. Powerpipe の Web UIモードで起動

[ec2-user@ip-10-192-0-236 work1]$ powerpipe server --listen network
[ Wait    ] Starting WorkspaceEvents Server
[ Message ] WorkspaceEvents loaded
[ Ready   ] Dashboard server started on 9033 and listening on network
[ Message ] Visit http://localhost:9033
[ Message ] Press Ctrl+C to exit
[ Message ] Initialization complete

9. 該当のURLにアクセスできる環境からURLをたたく
WEB画面からユーザが定義したSQLの実行結果のAWSリソースを常に確認することができるようになります。これでパラメータシートの管理などをしなくてもよくなりそうで、いいですね。

http://IPaddress:9033

image.png

スクリーンショット 2025-09-05 151034.png

上記は、CSV出力や検索機能などもあって、ほんと便利なので、設定値のWEBポータル化を皆さんも是非試してみてください。

余談

今回、「~/.steampipe/config/aws.spc」で、AWSのクレデンシャル情報を設定していないので、デフォルト動作として、EC2にアタッチされているIAMロール内の権限の範囲内で検索できるようになってます。
ReadOnlyAccessを付与しているので、EC2が所属しているAWSアカウント内にある情報をとってきているということです。
steampipeの定義ファイルで他AWSアカウントの認証情報を設定すれば、その情報をとってくることができるので、次回はその内容について書いていこうと思います。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?