この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
前回の記事ではAWSマネジメントコンソールを使用してデフォルトセキュリティグループを無効化する設定方法を書きました。今回はCloudFormation Stacksetsを利用して、全リージョンで無効化を行う方法を書きたいと思います。
StacksetsでStackを全リージョンに作成する
Stacksetsを利用して全リージョンに展開していきます。Stacksetsについてはわかりやすい記事がすでにあるので、そちらを参考に設定してみてください。肝心の部分を他の記事へのリンクで済ませてしまいますが、全く同じことを書くことになるのでここでは書きません。
一発でGuardDutyを全リージョン有効化して通知設定するテンプレート作った
SSM Automationを実行するIAM Roleを作成
ConfigルールとIAM Roleを作成するテンプレートを別々にしました。というのも、IAMはグローバルリソースですのでリージョン毎に作成しないためです。次のテンプレートをStacksetsにて任意のリージョンで実行します。
---
AWSTemplateFormatVersion: '2010-09-09'
Description: VPC default security group closed role.
Resources:
revokeSecurityGroupRole:
Type: AWS::IAM::Role
Properties:
RoleName: RevokeSecurityGroupRole
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Principal:
Service: ssm.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: RevokeSecurityGroupPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
Effect: Allow
Action:
- ec2:RevokeSecurityGroupIngress
- ec2:RevokeSecurityGroupEgress
- ec2:DescribeSecurityGroups
Resource: "*"
Configルールを作成する
次のテンプレートをStacksetsにて全リージョンで実行します。
---
AWSTemplateFormatVersion: '2010-09-09'
Description: VPC default security group closed.
Resources:
document:
Type: AWS::SSM::Document
Properties:
DocumentType: Automation
Content:
description: Revoke all security group rules.
schemaVersion: "0.3"
assumeRole: "{{ AutomationAssumeRole }}"
parameters:
GroupId:
type: String
description: (Required) Security Group ID
allowedPattern: ^([s][g]\-)([0-9a-f]){1,}$
AutomationAssumeRole:
type: String
description: (Optional) The ARN of the role that allows Automation to perform the actions on your behalf.
default: ""
mainSteps:
- name: GetSecurityGroupInfo
action: aws:executeAwsApi
onFailure: Abort
inputs:
Service: ec2
Api: DescribeSecurityGroups
GroupIds: ["{{GroupId}}"]
outputs:
- Name: IpPermissionsIngress
Selector: $.SecurityGroups[0].IpPermissions
Type: MapList
- Name: IpPermissionsEgress
Selector: $.SecurityGroups[0].IpPermissionsEgress
Type: MapList
- name: RevokeAllIngress
action: aws:executeAwsApi
onFailure: Continue
inputs:
Service: ec2
Api: RevokeSecurityGroupIngress
GroupId: "{{GroupId}}"
IpPermissions: "{{GetSecurityGroupInfo.IpPermissionsIngress}}"
- name: RevokeAllEgress
action: aws:executeAwsApi
onFailure: Continue
inputs:
Service: ec2
Api: RevokeSecurityGroupEgress
GroupId: "{{GroupId}}"
IpPermissions: "{{GetSecurityGroupInfo.IpPermissionsEgress}}"
configRule:
Type: AWS::Config::ConfigRule
Properties:
ConfigRuleName: vpc-default-security-group-closed
Description: Checks that the default security group of any Amazon Virtual Private Cloud (VPC) does not allow inbound or outbound traffic. The rule is non-compliant if the default security group has one or more inbound or outbound traffic.
Scope:
ComplianceResourceTypes:
- AWS::EC2::SecurityGroup
Source:
Owner: AWS
SourceIdentifier: VPC_DEFAULT_SECURITY_GROUP_CLOSED
remediation:
Type: AWS::Config::RemediationConfiguration
Properties:
ConfigRuleName: !Ref configRule
TargetType: SSM_DOCUMENT
TargetId: !Ref document
Parameters:
AutomationAssumeRole:
StaticValue:
Values:
- !Sub arn:aws:iam::${AWS::AccountId}:role/RevokeSecurityGroupRole
GroupId:
ResourceValue:
Value: RESOURCE_ID
Automatic: true
MaximumAutomaticAttempts: 5
RetryAttemptSeconds: 60
オプトインの必要なリージョンもありますので、人によってはリージョン数が異なっていることもあると思いますが、選択したリージョンでデフォルトセキュリティグループが無効になっていると思います。
さいごに
個人的にはAutomationドキュメントでAWS APIが実行できるようになったことがポイントで、これにより色んな可能性があると思います。
どなたかの参考になれば幸いです。