CloudFormation を利用して Inspector V2 を有効化する方法を教えてください
困っていること
CloudFormation を利用して Inspector V2 の EC2 および ECR スキャン機能を有効化したいと考えています。
上記を実現するために必要なテンプレートの記載方法を教えてください。
どう対応すればいいの?
2025/7/30 現在の仕様として、CloudFormation では Inspector V2 を有効化するためのリソースプロパティが提供されていない状況です。[1]
そのため、CloudFormation の機能のみでの Inspector V2 有効化は実現できません。
もし CloudFormation 経由で Inspector V2 を有効化したい場合は、カスタムリソース[2]の利用が必要です。
具体的には Inspector V2 の Enable API を呼び出す Lambda 関数を作成し、CloudFormation のカスタムリソース定義にて上記関数を呼び出すようにする、といった実装を行わなければなりません。
サンプルとしては以下の様になります。
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Enable Inspector V2'
#----------------------------------------------
# Parameters Section
#----------------------------------------------
Parameters:
EnableEC2:
Type: String
Default: 'true'
AllowedValues: ['true', 'false']
EnableECR:
Type: String
Default: 'true'
AllowedValues: ['true', 'false']
#----------------------------------------------
# Resources Section
#----------------------------------------------
Resources:
InspectorV2EnablerRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: InspectorV2EnablePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- inspector2:Enable
- inspector2:Disable
- inspector2:GetConfiguration
- iam:CreateServiceLinkedRole
Resource: '*'
- Effect: Allow
Action:
- iam:CreateServiceLinkedRole
Resource: 'arn:aws:iam::*:role/aws-service-role/inspector2.amazonaws.com/AWSServiceRoleForAmazonInspector2*'
Condition:
StringEquals:
'iam:AWSServiceName': 'inspector2.amazonaws.com'
InspectorV2EnablerFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: InspectorV2Enabler
Runtime: python3.9
Handler: index.lambda_handler
Role: !GetAtt InspectorV2EnablerRole.Arn
Timeout: 60
Code:
ZipFile: |
import json
import boto3
import cfnresponse
def lambda_handler(event, context):
try:
inspector_client = boto3.client('inspector2')
request_type = event['RequestType']
properties = event['ResourceProperties']
# Get resource types to enable
resource_types = []
if properties.get('EnableEC2') == 'true':
resource_types.append('EC2')
if properties.get('EnableECR') == 'true':
resource_types.append('ECR')
if request_type == 'Create' or request_type == 'Update':
if resource_types:
response = inspector_client.enable(
resourceTypes=resource_types
)
print(f"Enabled Inspector V2 for: {resource_types}")
print(f"Response: {response}")
elif request_type == 'Delete':
if resource_types:
response = inspector_client.disable(
resourceTypes=resource_types
)
print(f"Disabled Inspector V2 for: {resource_types}")
print(f"Response: {response}")
cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
except Exception as e:
print(f"Error: {str(e)}")
cfnresponse.send(event, context, cfnresponse.FAILED, {})
InspectorV2CustomResource:
Type: AWS::CloudFormation::CustomResource
Properties:
ServiceToken: !GetAtt InspectorV2EnablerFunction.Arn
EnableEC2: !Ref EnableEC2
EnableECR: !Ref EnableECR