LoginSignup
0
0

More than 5 years have passed since last update.

クロスアカウントでAWS Config Rulesを実行するメモ

Last updated at Posted at 2018-05-14

ほぼ参考リンク先の手順の通り

イメージ

あくまでイメージ。厳密には違うかも
temp.png

手順

前提

1. Lambdaファンクションの作成

(admin-account)
今回は↓のVPCフローログを評価するサンプルを用いる
https://github.com/awslabs/aws-config-rules/blob/master/python/vpc_flow_logs_enabled.py

STSでクロスアカウントでの実行を行うため、以下を追記/コメントアウトした。
arn:aws:iam::xxxxx:role/cross-account-config-role は後ほど作るmanaged-accountに用意するクロスアカウント用のIAMロールを指定する。

    sts_client = boto3.client('sts')
    res = sts_client.assume_role(
        RoleArn="arn:aws:iam::xxxxx:role/cross-account-config-role", 
        RoleSessionName="temp")
    access_key = res['Credentials']['AccessKeyId']
    secret_key = res['Credentials']['SecretAccessKey']
    sesstion_token = res['Credentials']['SessionToken']
    region = "ap-northeast-1"

    session = Session(access_key, secret_key, sesstion_token, region)
    config = session.client('config')

#    config = boto3.client('config')

2. Lambdaファンクションへの権限追加

(admin-account)
[managed-account-number]はハイフン抜きのAWSアカウント番号を指定

kure@ubuntu ~ % aws lambda add-permission \
  --function-name configruletest \
  --region ap-northeast-1 \
  --statement-id 1 \
  --action "lambda:InvokeFunction" \
  --principal config.amazonaws.com \
  --source-account [managed-account-number]
★確認
kure@ubuntu ~ % aws lambda get-policy --function-name configruletest

3.クロスアカウント用のIAMロール作成

(managed-account)
- クロスアカウント用のIAMロールを作成
- admin-accountのAWSアカウント番号を指定
- ポリシは "config:PutEvaluations" のみを許可
- admin-accountのLambdaに付与したIAMロールへの信頼関係を作成

cross-account.png

policy.png

信頼関係

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "[admin-account]のLambdaに付与したIAMロールのARN"
      },
      "Action": "sts:AssumeRole",
      "Condition": {}
    }
  ]
}

4. Config Rulesの作成

(managed-account)
- managed-accountでConfig Rulesを作成
- LambdaのARNはadmin-accountに作成したLambdaのARNを指定

参考

How to Centrally Manage AWS Config Rules across Multiple AWS Accounts | AWS DevOps Blog
https://aws.amazon.com/jp/blogs/devops/how-to-centrally-manage-aws-config-rules-across-multiple-aws-accounts/

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