マネージド型ルールで評価できない場合は、Lambda関数で評価することができる。
Lambda関数のサンプルはGithubにあるので、流用すると良いです。
<設定例>
・RDSのインスタンスタイプが指定のタイプか
作成されているDBインスタンスが「db.t2.micro」以外だったら「非準拠」となる。
・ルール
名前:rds-desired-instance
管理されるルールの名前:下記に登録するLambda関数
・トリガー
設定変更時:チェックする
変更範囲:リソース
・リソース
リソースタイプ:AWS RDS DBInstance
・パラメータ
キー:DBInstance
値:db.t2.micro
Pythonで作成サンプルを流用
Lambda関数のロールは下記をアタッチしました。
AwsConfigから受取ったeventのJSONデータを弄っているだけなので、RDSへのアクセス権限は不要っぽいです。
・AWSConfigRulesExecutionRole
・CloudWatchLogsFullAccess
#
# This file made available under CC0 1.0 Universal (https://creativecommons.org/publicdomain/zero/1.0/legalcode)
#
# Ensure all RDS DB Instances are of a Given Type
# Description: Checks that all RDS DB instances are of the type specified
#
# Trigger Type: Change Triggered
# Scope of Changes: RDS::DBInstance
# Required Parameter: DBInstance
# Example Value: db.t2.small
#
# See https://aws.amazon.com/ec2/instance-types/ for more instance types
import boto3
import json
def is_applicable(config_item, event):
status = config_item['configurationItemStatus']
event_left_scope = event['eventLeftScope']
test = ((status in ['OK', 'ResourceDiscovered']) and
event_left_scope == False)
return test
def evaluate_compliance(config_item, rule_parameters):
if (config_item['resourceType'] != 'AWS::RDS::DBInstance'):
return 'NOT_APPLICABLE'
elif (config_item['configuration']['dBInstanceClass'] in
rule_parameters['DBInstance']):
return 'COMPLIANT'
else:
return 'NON_COMPLIANT'
def lambda_handler(event, context):
invoking_event = json.loads(event['invokingEvent'])
rule_parameters = json.loads(event['ruleParameters'])
compliance_value = 'NOT_APPLICABLE'
if is_applicable(invoking_event['configurationItem'], event):
compliance_value = evaluate_compliance(
invoking_event['configurationItem'], rule_parameters)
config = boto3.client('config')
response = config.put_evaluations(
Evaluations=[
{
'ComplianceResourceType': invoking_event['configurationItem']['resourceType'],
'ComplianceResourceId': invoking_event['configurationItem']['resourceId'],
'ComplianceType': compliance_value,
'OrderingTimestamp': invoking_event['configurationItem']['configurationItemCaptureTime']
},
],
ResultToken=event['resultToken'])