-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservicelinkedrole.yaml
68 lines (64 loc) · 2.32 KB
/
servicelinkedrole.yaml
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
AWSTemplateFormatVersion: "2010-09-09"
Resources:
EnsuredServiceRoleLambdaRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: lambdaaddservicerole
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: "*"
- Effect: Allow
Action:
- iam:CreateServiceLinkedRole
Resource: "*"
EnsuredServiceRole:
Type: "AWS::Lambda::Function"
Properties:
Runtime: python3.7
Handler: index.handler
Timeout: 300
Role: !GetAtt EnsuredServiceRoleLambdaRole.Arn
Code:
ZipFile: |
import json
import cfnresponse
import botocore
import boto3
import logging
import traceback
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)
client = boto3.client("iam")
def handler(event, context):
try:
LOGGER.info('Event structure: %s', event)
service = event['ResourceProperties']['Service']
if event['RequestType'] == 'Create' or event['RequestType'] == 'Update':
try:
client.create_service_linked_role(AWSServiceName = service)
except client.exceptions.InvalidInputException as error:
LOGGER.info('Captured expected exception: %s', error)
# Return the service as our physical ID, that way if the service field is
# changed, we will handle correctly
cfnresponse.send(event, context, cfnresponse.SUCCESS, {}, service)
elif event['RequestType'] == 'Delete':
# Don't delete the service linked role, as we just want to assure it exists
cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
except Exception as e:
LOGGER.error(e)
cfnresponse.send(event, context, cfnresponse.FAILED, {})
traceback.print_exc()