AWS ConfigをAWS CLIから紐解く

2015.07.07

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

森永です。

絶賛AWS Configちゃん普及活動継続中です。 今回は、CLIからAWS Configを紐解いていこうと思います。

CLIを使ったことがあることはご存知かと思いますが、基本的なサービスはaws サービス名としてコマンドを実行します。
しかし、AWS ConfigはConfigなんてありきたりな名前にしてしまったが為に、aws configserviceというコマンドになってしまっています。ご注意下さい。

CLIコマンド

Configuration Recorder

put-configuration-recorder

「Configuration Recorder」を作成します。 あらかじめ、適切な権限設定がなされたIAM Roleを作成しておく必要があります。

aws configservice put-configuration-recorder --configuration-recorder name=<Configuration Recorderの名前>,roleARN=<IAM RoleのARN>

全AWSリソースの構成情報を記録する際に必要な権限設定をしたポリシーは以下の通りです。
AWS Configでは特定のAWSリソースだけを記録することが可能です。
その場合はそれに必要な権限だけに制限することも可能です。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "appstream:Get*",
        "autoscaling:Describe*",
        "cloudformation:DescribeStacks",
        "cloudformation:DescribeStackEvents",
        "cloudformation:DescribeStackResource",
        "cloudformation:DescribeStackResources",
        "cloudformation:GetTemplate",
        "cloudformation:List*",
        "cloudfront:Get*",
        "cloudfront:List*",
        "cloudtrail:DescribeTrails",
        "cloudtrail:GetTrailStatus",
        "cloudwatch:Describe*",
        "cloudwatch:Get*",
        "cloudwatch:List*",
        "directconnect:Describe*",
        "dynamodb:GetItem",
        "dynamodb:BatchGetItem",
        "dynamodb:Query",
        "dynamodb:Scan",
        "dynamodb:DescribeTable",
        "dynamodb:ListTables",
        "ec2:Describe*",
        "elasticache:Describe*",
        "elasticbeanstalk:Check*",
        "elasticbeanstalk:Describe*",
        "elasticbeanstalk:List*",
        "elasticbeanstalk:RequestEnvironmentInfo",
        "elasticbeanstalk:RetrieveEnvironmentInfo",
        "elasticloadbalancing:Describe*",
        "elastictranscoder:Read*",
        "elastictranscoder:List*",
        "iam:List*",
        "iam:Get*",
        "kinesis:Describe*",
        "kinesis:Get*",
        "kinesis:List*",
        "opsworks:Describe*",
        "opsworks:Get*",
        "route53:Get*",
        "route53:List*",
        "redshift:Describe*",
        "redshift:ViewQueriesInConsole",
        "rds:Describe*",
        "rds:ListTagsForResource",
        "s3:Get*",
        "s3:List*",
        "sdb:GetAttributes",
        "sdb:List*",
        "sdb:Select*",
        "ses:Get*",
        "ses:List*",
        "sns:Get*",
        "sns:List*",
        "sqs:GetQueueAttributes",
        "sqs:ListQueues",
        "sqs:ReceiveMessage",
        "storagegateway:List*",
        "storagegateway:Describe*",
        "trustedadvisor:Describe*"
      ],
      "Effect": "Allow",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:PutObject*"
      ],
      "Resource": [
        "arn:aws:s3:::<S3バケット名>/AWSLogs/<アカウントID>/*"
      ],
      "Condition": {
        "StringLike": {
          "s3:x-amz-acl": "bucket-owner-full-control"
        }
      }
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetBucketAcl"
      ],
      "Resource": "arn:aws:s3:::<バケット名>"
    },
    {
      "Effect": "Allow",
      "Action": "sns:Publish",
      "Resource": "<SNSのARN>"
    }
  ]
}

start-configuration-recorder

「Configuration Recorder」をスタートします。 後述する「Delivery Channel」を設定していないとスタートできません。

aws configservice start-configuration-recorder --configuration-recorder-name <Configuration Recorderの名前>

stop-configuration-recorder

「Configuration Recorder」をストップします。

aws configservice stop-configuration-recorder --configuration-recorder-name <Configuration Recorderの名前>

describe-configuration-recorders

「Configuration Recorder」の詳細を表示します。

aws configservice describe-configuration-recorders

describe-configuration-recorder-status

「Configuration Recorder」の状態を表示します。

aws configservice describe-configuration-recorder-status

まずは、「Configuration Recorder」に関するコマンドです。
「Configuration Recorder」は、AWSリソースの構成情報を記録するための主体です。

こいつにdescribe-*list-*などの権限をもったIAM Roleを設定してやることで、AWS ConfigはAWSリソースの構成情報を記録することが出来ます。

ただし、「Configuration Recorder」だけでは構成情報を配置できません。
次に説明する「Delivery Channel」を設定してあげる必要があります。

Delivery Channel

put-delivery-channel

「Delivery Channel」を作成します。
前述した「Configuration Recorder」を設定していないと作成できません。
あらかじめ作成しておいたS3バケットとSNSトピックを指定します。
ただし、SNSトピックについては指定しなくても構いません。

aws configservice put-delivery-channel --delivery-channel name=<Delivery Channelの名前>,s3BucketName=<S3バケット名>,snsTopicARN=<SNSのARN>

describe-delivery-channels

「Delivery Channel」の詳細を表示します。

aws configservice describe-delivery-channels

describe-delivery-channel-status

「Delivery Channel」の状態を表示します。

aws configservice describe-delivery-channel-status

delete-delivery-channel

「Delivery Channel」を削除します。

aws configservice

次に、「Delivery Channel」に関するコマンドです。
「Delivery Channnel」と言うのは、AWS ConfigがデータをDeliverする先の情報です。

具体的に言うと、AWS ConfigはS3にスナップショットを置き、SNSで構成変更を通知します。
どのS3バケットに配置するか、どのSNSトピックで通知を行うかを設定したものをまとめて「Delivery Channel」といいます。

共通

subscribe

「Configuration Recorder」と「Delivery Channnel」を作成します。
基本的に「put-configuration-recorder」と「put-delivery-channel」を組み合わせたものですが、指定したS3バケットやSNSトピックがない場合は作成してくれます。
特にこだわりがなければこちらで作ってしまうのが楽でいいですね。

aws configservice subscribe --iam-role <IAM RoleのARN> --s3-bucket <S3バケット名> --sns-topic <SNSのトピック名 or SNSのARN>

get-status

「Configuration Recorder」と「Delivery Channnel」の状態を表示します。

aws configservice get-status

deliver-config-snapshot

AWSリソースの構成情報のスナップショットを取得します。 「Configuration Recorder」で構成情報を取得し、「Delivery Channnel」で指定したS3バケットに構成情報のスナップショットを投げます。
取得開始時と取得完了時(または失敗時)に「Delivery Channnel」で指定したSNSから通知が来ます。

「Configuration Recorder」をスタートしていたら6時間毎に自動でスナップショットを取得するようです。

aws configservice deliver-config-snapshot --delivery-channel-name config-oregon

get-resource-config-history

構成情報の履歴を取得します。
特に指定しなければ直近7日分の構成情報を表示します。

aws configservice get-resource-config-history --resource-type <AWSリソースタイプ> --resource-id <AWSリソースID>

対応しているAWSリソースタイプは以下の通りです。

  • AWS::EC2::NetworkInterface
  • AWS::EC2::VPNGateway
  • AWS::EC2::SecurityGroup
  • AWS::EC2::InternetGateway
  • AWS::EC2::Instance
  • AWS::EC2::CustomerGateway
  • AWS::EC2::Volume
  • AWS::EC2::VPC
  • AWS::EC2::NetworkAcl
  • AWS::EC2::Subnet
  • AWS::EC2::EIP
  • AWS::CloudTrail::Trail
  • AWS::EC2::VPNConnection
  • AWS::EC2::RouteTable

さいごに

今回はCLIからAWS Configちゃんを紐解きました。
AWSのサービスは基本的にAPIでできているので、APIやCLIからサービスのことを勉強すると結構色々と見えてきます。

ひとつ疑問点として、get-resource-config-historyで取得する情報にも、スナップショットにも変更箇所については記述がないことです。
それぞれの差分をとっているのでしょうが、差分だけを表示できたらなぁ…なんてことを考えています。

そろそろAWS Config有効化しましたよね?