AWS Configの高度なクエリでOrganizations環境のリソースを検索してみた

組織で設定したAWS Configアグリゲータに対して高度なクエリを実行することで、組織全体のリソースを検索する
2023.04.19

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

みなさんこんにちは、杉金です。 AWS Configの高度なクエリを使ったことはありますか? SQL文を使ってAWSリソースを検索してくれるものなのですが、アグリゲータを指定した検索ができます。 つまり、Organizations(組織)でConfigアグリゲータを設定していると、組織全体のリソースを高度なクエリで検索できます。今回はその方法をご紹介します。

前提条件

Configアグリゲータで組織のConfigを集約していることが前提条件となります。設定方法は以下のブログが参考になります。

高度なクエリで検索

高度なクエリの使い方ですが、AWS Configから高度なクエリを選びます。すでに用意されているクエリを選択するか、「新しいクエリ」からクエリエディタを起動します。

最も重要なポイントとしてはアグリゲータを指定することです。クエリエディタの左側にあるクエリスコープから組織のアグリゲータを選択します。

あとはクエリを入力して「実行」ボタンを押すだけです。組織全体のリソース検索として、3つほどクエリのサンプルをご紹介します。

  • 組織全体のRDSインスタンスのバージョン一覧取得
  • 組織全体の指定EC2インスタンスタイプ一覧取得
  • 組織全体のLambda関数のバージョン一覧取得

サンプルその1:組織全体のRDSインスタンスのバージョン一覧取得

組織全体のRDSインスタンスとそのバージョン一覧を取得するクエリと実行結果のサンプルです。

クエリ

SELECT
  accountId,
  awsRegion,
  configuration.dBInstanceIdentifier,
  configuration.engine,
  configuration.engineVersion,
  configuration.autoMinorVersionUpgrade
WHERE
  resourceType = 'AWS::RDS::DBInstance'

実行結果

組織全体で使用しているRDSインスタンスのバージョンを調べたいときに使えます。古いエンジンバージョンのサポート終了のアナウンスがあったときにこのクエリは役立ちそうです。補足情報として、マイナーバージョンの自動アップグレードが有効かも合わせて取得しており、バージョン列の右側にある「true」となっているものがそれです。

サンプルその2:組織全体の指定EC2インスタンスタイプ一覧取得

組織全体から特定のインスタンスタイプを対象としたインスタンスの検索を行います。下記の例ではOR条件を使って2つのインスタンスタイプを条件に一覧を取得しています。

クエリ

SELECT
  accountId,
  awsRegion,
  resourceId,
  configuration.instanceType
WHERE
  resourceType = 'AWS::EC2::Instance'
  AND configuration.instanceType = 't2.nano'
  OR configuration.instanceType = 't2.micro'

実行結果

LIKEを使った特定のインスタンスファミリー(T2)の検索もできます。

SELECT
  accountId,
  awsRegion,
  resourceId,
  configuration.instanceType
WHERE
  resourceType = 'AWS::EC2::Instance'
  AND configuration.instanceType LIKE 't2.%'

実行結果は、上記画像と類似しているため割愛します。 古いインスタンスタイプの存在チェックやコスト戦略を立てるのに使えそうです。

サンプルその3:組織全体のLambda関数のバージョン一覧取得

組織全体のLambda関数とそのバージョン一覧を取得するクエリと実行結果のサンプルです。

クエリ

SELECT
  resourceId,
  configuration.runtime,
  configuration.lastModified
WHERE
  resourceType = 'AWS::Lambda::Function'

実行結果

左端列のマスクしている部分にLambda関数名が入ります。ランタイムのサポート終了であったり、脆弱性のニュースが出たときに影響あるかの確認などで使えそうです。 特定のランタイムを検索する場合は、以下のようにAND条件で探したいランタイムを指定します。

SELECT
  resourceId,
  configuration.runtime,
  configuration.lastModified
WHERE
  resourceType = 'AWS::Lambda::Function'
  AND configuration.runtime = 'python3.8'

実行結果は、上記画像と類似しているため割愛します。

検索できるリソースタイプ

すべてのリソースタイプが検索できるわけではありません。検索可能なリソースタイプについては以下のリンクに記載されています。取得可能なプロパティもここで確認できます。

制限事項

JOINやASなどクエリに使用できないものがあります。使用不可能なキーワードを含めた制限事項は以下のドキュメントをご確認ください。

どのようにクエリを投げるかは、以下のブログが参考になります。

すでに用意されているクエリについて

すでに用意されているクエリについて、参考情報として本記事後半に表形式でまとめてみました。

結果をダウンロードできる

これが本当にありがたい機能なのですが、結果をCSVまたはJSONでダウンロードできます。

料金

高度なクエリ自体は追加コストなしで使用できます。

高度なクエリは、中国リージョンを除く全ての AWS 公開リージョン、および AWS GovCloud (米国) で、追加料金なしでご利用いただけます。

AWS Resource Explorerとの違い

リソース検索といえばAWS Resource Explorerがありますが、違いを簡単に表してみました。

  • AWS Resource Explorer
    • 検索フォームから検索ワードを入力して検索
    • すばやくリソースを検索するのに向いている
    • 本記事執筆時点ではマルチアカウントのリソース検索はできない。
  • AWS Config 高度なクエリ
    • SQL文を使用したクエリを投げられる。CountやSUMなどの集計関数も使用できる。
    • アグリゲータを指定することでマルチアカウントのリソース検索ができる。

こちらですが、2023年11月のアップデートにより、AWS Resource Explorerもマルチアカウントのリソース検索に対応しました。詳細は以下のブログをご確認ください。

結果のエクスポートはAWS Resource Explorerでも可能です。

参考:用意されているクエリ一覧

すでに用意されているクエリの名前と説明を表にしました。

クエリ名 説明
Active DynamoDB tables List all active DynamoDB tables
Availability zones for Load Balancer List all availability zones of a Load Balancer "arn:aws:elasticloadbalancing:12345"
Compliance status of Config rules of a conformance pack List of all Config rules with their compliance status that are part of conformance pack "conformance-pack-12345"
Conformance packs compliance status List of all conformance packs with their compliance status
Count by compliant Count number of resources, group by Config rule compliance status
Count EC2 Instances Count EC2 instances, group by instance type
Count Non-compliant Count number of non-compliant resources, group by resource type, sort by count in descending order
Count of compliant and non-compliant rules of a conformance pack Count of compliant and non-compliant rules for conformance pack "conformance-pack-12345"
Currently running EC2 Instances List all EC2 instances that are currently running
Currently stopped EC2 Instances List all EC2 instances that are currently stopped
DynamoDB tables with disabled SSE List all DynamoDB tables where server-side encryption is disabled
EC2 Instances attached to Volume List all EC2 Instances that are attached to an particular EC2 volume
EC2 Instances by AMI List all EC2 Instances with a particular AMI
EC2 Instances by Network Interface List all EC2 Instances that contains a particular Network Interface
EC2 Instances by Security Group List all EC2 Instances that are part of a particular Security Group
EC2 Instances by Subnet List all EC2 Instances that are in a particular subnet
EC2 Instances by type List all EC2 instances of instance type "t2.micro"
EC2 Instances with a private DNS name List all EC2 instances that have a particular public DNS name
EC2 instances with a public DNS name List all EC2 instances that have a particular public DNS name
EC2 Instances with monitoring state List all EC2 instances that have monitoring state "disabled"
EC2 Instances with User initiated state transition List all instances that have a StateTransitionReason as "User Initiated"
EC2 Instances within a VPC List all EC2 Instances contained in a particular VPC
EC2 Internet Gateway with state available List all EC2 Internet Gateway that are in available state
EC2 NetworkInterfaces by EC2 Instance List all EC2 Network Interfaces attached to EC2 instance "i-12345"
EC2 NetworkInterfaces by IP address List all EC2 Network Interfaces with private IP address "1.1.1.1"
EC2 NetworkInterfaces in-use List all EC2 Network Interfaces that are in-use
EC2 Volumes by size (GB) List all EC2 Volumes that have a size "8 GB"
EC2 Volumes by type List all EC2 Volumes that are of type "gp2"
Elastic IPs by Network Interface List all EC2 Elastic IPs associated with EC2 Network Interface "eni-12345"
Elastic IPs by pool List all EC2 Elastic IPs in the pool "test-123"
Elastic IPs with private IP address List all EC2 Elastic IPs with private IP address "1.1.1.1"
Encrypted EC2 Volumes List all EC2 Volumes that are encrypted
IAM Policy by IAM User List all IAM Policies that are attached to an IAM User
IAM User by IAM Policy List all IAM Users that are attached to IAM policy "IAMUserChangePassword"
IAM Users by creation date List all IAM users created between date "2018-12-01T00:00" "and date "2019-02-28T00:00"
In-use EC2 volumes List all EC2 Volumes that are in-use
Lambda Function using nodejs6 List all Lambda functions using runtime "nodejs6"
Load Balancers by Security Group List all Load Balancers that are associated with Security group "sg-12345"
Load Balancers by Subnet List all Load Balancers that are attached to subnet "subnet-12345"
Load Balancers by VPC List all Load Balancers that are contained in VPC "vpc-12345"
Non-compliant resources List all resources that are non-compliant for one or more Config rules
Not Encrypted EC2 Volumes List all EC2 Volumes that are not encrypted
Publicly accessible RDS List all RDS DB Instances that are publicly accessible
Publicly accessible RDS DBInstances List all RDS DBInstances that are publicly accessible
RDS DBInstances by backup (days) List all RDS DBInstances that have a backup retention period "less than 7 days"
RDS DBInstances by database engine List all RDS DBInstances that have a database engine "mysql"
RDS DBInstances by DBInstanceClass List all RDS DBInstances that have a DBInstanceClass "db.t2.small"
RDS DBInstances by Security Group List all RDS DBInstances that are part of Security Group "sg-12345"
RDS DBInstances by storage type List all RDS DBInstances that have a storage type "aurora"
RDS DBInstances by StorageEncrypted List all RDS DBInstances that have storage encrypted "true"
RDS with database engine List all RDS instances running database engine "MySQL", version "5.6.35"
S3 Buckets by versioning state List S3 buckets that have bucket versioning "disabled"
S3 buckets with no SSE List all S3 buckets where server-side encryption is disabled
Security group sg-12345 List all resources that are related to security group "sg-12345"
Security group supporting protocol List all Security Groups that support the protocol "tcp"
Subnets for Load Balancer List all subnets of a Load Balancer "arn:aws:elasticloadbalancing:12345"
Tagged with CostCenter 12345 List all resources that are tagged with "CostCenter" equal to "12345"
Unused EBS List all EBS volumes that are not in use

最後に

本記事を読んでいただき、ありがとうございます。高度なクエリを使って、組織全体のリソース検索を試してみました。実際のプロジェクトでも活用するシーンがあったのでこの機能は便利だなと実感しています。 高度なクエリで検索できるリソースタイプが今後増えることを期待しつつ、今後も役に立つ機能があればご紹介していきます。

参考情報