forked from org-formation/org-formation-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cross-account-role-with-alarm.yml
124 lines (105 loc) · 3.61 KB
/
cross-account-role-with-alarm.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
AWSTemplateFormatVersion: '2010-09-09-OC'
# Include file that contains Organization Section.
# The Organization Section describes Accounts, Organizational Units, etc
Organization: !Include ../organization.yml
# Any Binding that does not explicitly specify a region will default to this.
# Value can be either string or list
DefaultOrganizationBindingRegion: eu-central-1
Parameters:
resourcePrefix:
Type: String
Default: my
roleName:
Type: String
rolePolicyArns:
Type: CommaDelimitedList
cloudTrailLogGroup:
Type: String
requireMfa:
Type: String
AllowedValues: [ 'true', 'false' ]
Default: false
alarmSubscriptionProtocol:
Type: String
AllowedValues: [ 'email', 'https' ]
Default: email
alarmSubscriptionEndpoint:
Type: String
Default: !GetAtt AWSAccount.RootEmail
# Section contains a named set of Bindings.
# Bindings determine what resources are deployed where
# These bindings can be !Ref'd from the Resources in the resource section
OrganizationBindings:
AssumeRoleBinding:
Account: !Ref SharedUsersAccount
RoleBinding:
Account: '*'
ExcludeAccount: !Ref SharedUsersAccount
Resources:
Role:
Type: AWS::IAM::Role
OrganizationBinding: !Ref RoleBinding
Properties:
ManagedPolicyArns: !Ref rolePolicyArns
RoleName: !Ref roleName
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: sts:AssumeRole
Principal:
AWS: Fn::EnumTargetAccounts AssumeRoleBinding '${account}' # role can only be assumed from SharedUsersAccount
Condition:
Bool:
aws:MultiFactorAuthPresent: ['true', !Ref requireMfa]
AssumeRolePolicy:
Type: AWS::IAM::ManagedPolicy
OrganizationBinding: !Ref AssumeRoleBinding
Properties:
ManagedPolicyName: !Sub '${resourcePrefix}-${roleName}-assume-role-policy'
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: sts:AssumeRole
Resource: Fn::EnumTargetAccounts RoleBinding 'arn:aws:iam::${account}:role/${roleName}'
RoleAlarm:
Type: AWS::CloudWatch::Alarm
OrganizationBinding: !Ref RoleBinding
Properties:
AlarmName: !Sub '${roleName} used on account: ${AWSAccount.Alias}'
AlarmDescription: !Sub 'Alarm on usage of ''${AWSAccount.Alias}'' in this account'
MetricName: !Sub '${resourcePrefix}-${roleName}-event-count'
Namespace: CloudTrailMetrics
Statistic: Sum
Period: 10
EvaluationPeriods: 1
Threshold: 1
TreatMissingData: notBreaching
AlarmActions:
- !Ref AlarmNotificationTopic
ComparisonOperator: GreaterThanOrEqualToThreshold
ProductionSupportRoleLoginsFilter:
Type: AWS::Logs::MetricFilter
OrganizationBinding: !Ref RoleBinding
Properties:
LogGroupName: !Ref cloudTrailLogGroup
FilterPattern: !Sub
- '{
($.eventName = "AssumeRole") &&
($.requestParameters.roleArn = "${RoleArn}")
}'
- { RoleArn: !GetAtt Role.Arn }
MetricTransformations:
- MetricValue: '1'
MetricNamespace: CloudTrailMetrics
MetricName: !Sub '${resourcePrefix}-${roleName}-event-count'
AlarmNotificationTopic:
Type: AWS::SNS::Topic
OrganizationBinding: !Ref RoleBinding
Properties:
DisplayName: !Sub 'Notifies when alarm on usage of ${roleName} goes off'
TopicName: !Sub '${resourcePrefix}-assume-${roleName}-alarm-notification'
Subscription:
- Endpoint: !Ref alarmSubscriptionEndpoint
Protocol: !Ref alarmSubscriptionProtocol