This repository has been archived by the owner on Dec 2, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yml
executable file
·212 lines (203 loc) · 6.37 KB
/
template.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: >
AWS SAM project to deploy an IAM role creation service API
Metadata:
"AWS::ServerlessRepo::Application":
Name: role-creation-service
Description: >
AWS SAM project to deploy an IAM role creation service API
Includes: API Gateway, Lambda
Author: smoketurner
SpdxLicenseId: MIT
LicenseUrl: LICENSE
ReadmeUrl: README.md
Labels:
[
"organization",
"org",
"apigateway",
"apigw",
"lambda",
"iam",
"account",
"sqs",
]
HomePageUrl: https://github.com/smoketurner/sam-iam-role
SemanticVersion: 1.0.0
SourceCodeUrl: https://github.com/smoketurner/sam-iam-role
Parameters:
ExecutionRoleName:
Type: String
Default: RoleCreationExecution
Description: Name of the IAM role in the target account
Globals:
Function:
Runtime: python3.8
CodeUri: src/handlers/
MemorySize: 1024 # megabytes
Timeout: 5 # seconds
Environment:
Variables:
EXECUTION_ROLE_NAME: !Ref ExecutionRoleName
RCS_LOG_LEVEL: DEBUG
Resources:
RoleCreationAdmin:
Type: "AWS::IAM::Role"
Properties:
RoleName: RoleCreationAdmin
Description: !Sub "DO NOT DELETE - Used by Role Creation Service. Created by CloudFormation ${AWS::StackId}"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: "lambda.amazonaws.com"
Action: "sts:AssumeRole"
Policies:
- PolicyName: RoleCreationAdminPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: "sts:AssumeRole"
Resource: !Sub "arn:${AWS::Partition}:iam::*:role/${ExecutionRoleName}"
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
CreateRolesLambdaFunction:
Type: "AWS::Serverless::Function"
Properties:
Handler: create_roles.lambda_handler
Description: Create new IAM roles Lambda handler
Role: !GetAtt RoleCreationAdmin.Arn
EvaluatePolicyLambdaFunction:
Type: "AWS::Serverless::Function"
Properties:
Handler: evaluate_policy.lambda_handler
Description: Evaluate IAM policy Lambda handler
Role: !GetAtt RoleCreationAdmin.Arn
VerifyRolesLambdaFunction:
Type: "AWS::Serverless::Function"
Properties:
Handler: verify_roles.lambda_handler
Description: Verify IAM roles Lambda handler
Role: !GetAtt RoleCreationAdmin.Arn
StatesExecutionRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Principal:
Service: !Sub states.${AWS::Region}.amazonaws.com
Effect: Allow
Action: "sts:AssumeRole"
Policies:
- PolicyName: StatesExecutionPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: "states:StartExecution"
Resource: "*"
- Effect: Allow
Action: "lambda:InvokeFunction"
Resource:
- !GetAtt "CreateRoleLambdaFunction.Arn"
- !GetAtt "EvaluatePolicyLambdaFunction.Arn"
- !GetAtt "VerifyRoleLambdaFunction.Arn"
Condition:
ArnLike:
"aws:SourceArn": !Sub "arn:${AWS::Partition}:states:${AWS::Region}:${AWS::AccountId}:stateMachine:*"
StateMachine:
Type: "AWS::StepFunctions::StateMachine"
Properties:
DefinitionString: !Sub |-
{
"Comment": "Role Creation Process",
"StartAt": "IsValidRequestType",
"States": {
"IsValidRequestType": {
"Type": "Choice",
"Choices": [
{
"Or": [
{
"Variable": "$.type",
"StringEquals": "iam"
},
{
"Variable": "$.type",
"StringEquals": "sns_topic"
},
{
"Variable": "$.type",
"StringEquals": "s3_bucket"
},
{
"Variable": "$.type",
"StringEquals": "vpce"
}
],
"Next": "EvaluatePolicy"
}
],
"Default": "InvalidType"
},
"EvaluatePolicy": {
"Type": "Task",
"Resource": "${EvaluatePolicyLambdaFunction.Arn}",
"TimeoutSeconds": 60,
"Next": "IsCompliant"
},
"InvalidType": {
"Type": "Fail"
},
"IsCompliant": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.result",
"StringEquals": "COMPLIANT",
"Next": "CreateRoles"
},
{
"Variable": "$.result",
"StringEquals": "UNSUPPORTED",
"Next": "UnsupportedType"
}
],
"Default": "NonCompliant"
},
"CreateRoles": {
"Type": "Task",
"Resource": "${CreateRolesLambdaFunction.Arn}",
"Next": "VerifyRoles",
"TimeoutSeconds": 60
},
"VerifyRoles": {
"Type": "Task",
"Resource": "${VerifyRolesLambdaFunction.Arn}",
"Retry": [
{
"ErrorEquals": ["RetryError"],
"IntervalSeconds": 10,
"BackoffRate": 1.0,
"MaxAttempts": 500
}
],
"TimeoutSeconds": 60,
"Next": "RolesCreated"
},
"NonCompliant": {
"Type": "Fail"
},
"UnsupportedType": {
"Type": "Fail"
},
"RolesCreated": {
"Type": "Succeed"
}
}
}
RoleArn: !GetAtt StatesExecutionRole.Arn