【AWS CLI】Config Aggregatorの情報取得編

記事タイトルとURLをコピーする

こんにちは。AWS CLIが好きな福島です。

はじめに

今回は、Config Aggregator関連のコマンドをご紹介いたします。

利用するコマンド,サブコマンド

まず、AWS CLIの構造は以下の通りです。

aws <command> <subcommand> [options and parameters]

上記を前提に今回使う <command>,<subcommand>は、以下の通りです。

<command>

  • configservice

<subcommand>

  • describe-configuration-aggregators
    ⇒Config Aggregatorの情報を取得できます。

  • describe-aggregate-compliance-by-config-rules
    ⇒Config AggregatorからConfig Ruleの一覧を確認できます。

  • get-aggregate-compliance-details-by-config-rule
    ⇒Config AggregatorからConfig Ruleの詳細を確認できます。

では、ここから実際のコマンドを記載いたします。

describe-configuration-aggregators

①Aggregator名,AggregatorのARN

  • 実行コマンド
aws configservice describe-configuration-aggregators \
--query "ConfigurationAggregators[].\
[ConfigurationAggregatorName,\
ConfigurationAggregatorArn]" \
--output text

実行結果

agli-fk       arn:aws:config:ap-northeast-1:XXXXXXXXXXXX:config-aggregator/config-aggregator-y4s1ow3b

describe-aggregate-compliance-by-config-rules

本コマンドを実行するためには、Config Aggregator名を引数に指定する必要があるため、事前に環境変数を定義します。

  • 実行例
AGGREGATOR_NAME="fk-test-aggregator"

①ConfigRule名,準拠状況,非準拠のリソース数,上限値,アカウントID,リージョン

  • 実行コマンド
aws configservice describe-aggregate-compliance-by-config-rules \
--configuration-aggregator-name $AGGREGATOR_NAME \
--query "AggregateComplianceByConfigRules[].\
[ConfigRuleName,\
Compliance.ComplianceType,\
Compliance.ComplianceContributorCount.CappedCount,\
Compliance.ComplianceContributorCount.CapExceeded,\
AccountId,\
AwsRegion]" \
--output text | column -t

表示が見づらかったため、columnコマンドでタブ区切りで表示しています。

  • 実行結果
desired-instance-type                                                         NON_COMPLIANT      8     False  123456789012  ap-northeast-1
ec2-managedinstance-applications-required                                     NON_COMPLIANT      2     False  123456789012  ap-northeast-1
securityhub-access-keys-rotated-1492aed1                                      NON_COMPLIANT      5     False  123456789012  ap-northeast-1
securityhub-acm-certificate-expiration-check-1c825612                         NON_COMPLIANT      6     False  123456789012  ap-northeast-2
securityhub-alb-http-drop-invalid-header-enabled-4adede58                     INSUFFICIENT_DATA  None  None  123456789012  ap-northeast-2
securityhub-alb-http-to-https-redirection-check-f6612ba5                      INSUFFICIENT_DATA  None  None  123456789012  ap-northeast-2
securityhub-api-gw-associated-with-waf-2f718f0e                               NON_COMPLIANT      9     False  123456789012  ap-northeast-3
securityhub-api-gw-cache-encrypted-ec6fd28f                                   INSUFFICIENT_DATA  None  None  123456789012  ap-northeast-3
securityhub-api-gw-execution-logging-enabled-718bec9a                         NON_COMPLIANT      9     False  123456789012  ap-northeast-3
  • 補足 上限値とは、非準拠リソース数の上限値のことを指し、上限を超えている場合、Trueとなります。

②非準拠(NON_COMPLIANT)のリソースのみ表示

aws configservice describe-aggregate-compliance-by-config-rules \
--configuration-aggregator-name $AGGREGATOR_NAME \
--query "AggregateComplianceByConfigRules[].\
[ConfigRuleName,\
Compliance.ComplianceType,\
Compliance.ComplianceContributorCount.CappedCount,\
Compliance.ComplianceContributorCount.CapExceeded,\
AccountId,\
AwsRegion]" \
--filters "ComplianceType=NON_COMPLIANT" \
--output text | column -t
desired-instance-type                                                         NON_COMPLIANT      8     False  123456789012  ap-northeast-1
ec2-managedinstance-applications-required                                     NON_COMPLIANT      2     False  123456789012  ap-northeast-1
securityhub-access-keys-rotated-1492aed1                                      NON_COMPLIANT      5     False  123456789012  ap-northeast-1
securityhub-acm-certificate-expiration-check-1c825612                         NON_COMPLIANT      6     False  123456789012  ap-northeast-2
securityhub-api-gw-associated-with-waf-2f718f0e                               NON_COMPLIANT      9     False  123456789012  ap-northeast-3
securityhub-api-gw-execution-logging-enabled-718bec9a                         NON_COMPLIANT      9     False  123456789012  ap-northeast-3

get-aggregate-compliance-details-by-config-rule

①ConfigRule名,リソースタイプ,リソースID,準拠状況,チェック時間

本コマンドを実行するためには、Config Aggregator名,Config Rule名,アカウントID,リージョンを引数に指定する必要があるため、事前に環境変数を定義します。

AGGREGATOR_NAME="fk-test-aggregator"
CONFIG_RULE_NAME="securityhub-mfa-enabled-for-iam-console-access-0b3d825b"
ACCOUNT_ID="123456789012"
REGION=ap-northeast-1
  • 実行コマンド
aws configservice get-aggregate-compliance-details-by-config-rule \
--configuration-aggregator-name $AGGREGATOR_NAME \
--config-rule-name $CONFIG_RULE_NAME \
--account-id $ACCOUNT_ID \
--aws-region $REGION \
--query "AggregateEvaluationResults[].\
[EvaluationResultIdentifier.EvaluationResultQualifier.ConfigRuleName,\
EvaluationResultIdentifier.EvaluationResultQualifier.ResourceType,\
EvaluationResultIdentifier.EvaluationResultQualifier.ResourceId,\
ComplianceType,ConfigRuleInvokedTime,\
AccountId,\
AwsRegion]" \
--output text
  • 実行結果
securityhub-mfa-enabled-for-iam-console-access-0b3d825b AWS::IAM::User  AIDAT6MJ7L4PS3UWCLULX   COMPLIANT       2021-10-18T11:58:03.670000+09:00  123456789012  ap-northeast-1
securityhub-mfa-enabled-for-iam-console-access-0b3d825b AWS::IAM::User  AIDAT6MJ7L4P6RCJASE4F   NON_COMPLIANT   2021-10-18T11:58:03.670000+09:00  123456789012  ap-northeast-1
securityhub-mfa-enabled-for-iam-console-access-0b3d825b AWS::IAM::User  AIDAT6MJ7L4PRN37VSHPL   NON_COMPLIANT   2021-10-18T11:58:03.670000+09:00  123456789012  ap-northeast-1
securityhub-mfa-enabled-for-iam-console-access-0b3d825b AWS::IAM::User  AIDAT6MJ7L4PXYE7XLVXV   NON_COMPLIANT   2021-10-18T11:58:03.670000+09:00  123456789012  ap-northeast-1

②全Config Aggregatorから取得できるConfig Ruleの①

上記①では事前に諸々の変数を定義しましたが、②は事前定義不要です。

  • 実行コマンド
aws configservice describe-configuration-aggregators --query "ConfigurationAggregators[].[ConfigurationAggregatorName]" --output text | while read aggregator_name
do 
   aws configservice describe-aggregate-compliance-by-config-rules \
   --configuration-aggregator-name $aggregator_name \
    --query "AggregateComplianceByConfigRules[].\
   [ConfigRuleName,\
   AccountId,\
   AwsRegion]" \
   --output text | while read line
   do
      config_rule_name=$(echo $line | awk '{print $1}'); \
      account_id=$(echo $line | awk '{print $2}'); \
      region=$(echo $line | awk '{print $3}'); \
      echo $config_rule_name $account_id $region
      aws configservice get-aggregate-compliance-details-by-config-rule \
      --configuration-aggregator-name $aggregator_name \
      --config-rule-name $config_rule_name \
      --account-id $account_id \
      --aws-region $region \
      --query "AggregateEvaluationResults[].\
      [EvaluationResultIdentifier.EvaluationResultQualifier.ConfigRuleName,\
      EvaluationResultIdentifier.EvaluationResultQualifier.ResourceType,\
      EvaluationResultIdentifier.EvaluationResultQualifier.ResourceId,\
      ComplianceType,ConfigRuleInvokedTime,\
      AccountId,\
      AwsRegion]" \
      --output text
   done
done
  • 実行結果
desired-instance-type   AWS::EC2::Instance      i-0b56103f063dfd717     NON_COMPLIANT   2021-09-23T16:45:28.118000+09:00  123456789012  ap-northeast-1
desired-instance-type   AWS::EC2::Instance      i-0f6126b7aeedfabd6     NON_COMPLIANT   2021-07-16T15:01:25.949000+09:00  123456789012  ap-northeast-1
ec2-managedinstance-applications-required       AWS::SSM::ManagedInstanceInventory      i-06703f886cc49c540     NON_COMPLIANT   2021-09-14T14:54:10.400000+09:00  123456789012  ap-northeast-1
ec2-managedinstance-applications-required       AWS::SSM::ManagedInstanceInventory      i-0808672558492fde8     NON_COMPLIANT   2021-10-01T19:07:29.155000+09:00  123456789012  ap-northeast-2
securityhub-access-keys-rotated-1492aed1        AWS::IAM::User  AIDAT6MJ7L4P6FECH7VVF   COMPLIANT       2021-10-18T11:58:03.011000+09:00  123456789012  ap-northeast-2
securityhub-access-keys-rotated-1492aed1        AWS::IAM::User  AIDAT6MJ7L4P6RCJASE4F   COMPLIANT       2021-10-18T11:58:03.011000+09:00  123456789012  ap-northeast-3
securityhub-access-keys-rotated-1492aed1        AWS::IAM::User  AIDAT6MJ7L4PR5JPPJPWG   COMPLIANT       2021-10-18T11:58:03.011000+09:00  123456789012  ap-northeast-3
securityhub-access-keys-rotated-1492aed1        AWS::IAM::User  AIDAT6MJ7L4PRN37VSHPL   COMPLIANT       2021-10-18T11:58:03.011000+09:00  123456789012  ap-northeast-3

終わりに

今回は、Config Aggregatorの情報を取得するコマンドをご紹介いたしました。 Config Aggregatorの情報は、マネジメントコンソールからの確認が大変なため、ぜひ使っていただけますと幸いです。

福島 和弥 (記事一覧)

2019/10 入社

AWS CLIが好きです。

AWS資格12冠。2023 Japan AWS Partner Ambassador/APN ALL AWS Certifications Engineer。