-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathaws_auto_terminate_idle_emr_cft.yaml
108 lines (108 loc) · 3.71 KB
/
aws_auto_terminate_idle_emr_cft.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
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
AWSTemplateFormatVersion: 2010-09-09
Description: >-
AWS CloudFormation Template to deploy the framework of Automatic Termination
of Idle EMR Clusters by creating resources like CloudWatch event, IAM role and
Lambda function. You will be billed for the AWS resources used if you create a
stack from this template.
Parameters:
MaxIdleTimeInMinutes:
Description: Maximum Idle Time in Minutes for Any EMR Cluster.
Type: Number
MinValue: '1'
ConstraintDescription: Must be greater than 0.
LambdaCodeS3Bucket:
Description: S3 Bucket Name having Lambda Function Code.
Type: String
LambdaCodeS3BucketKey:
Description: S3 Bucket Key having Lambda Function Code.
Type: String
CloudWatchEventScheduleExpression:
Description: >-
CloudWatch Event Schedule Expression in the form of either Rate Function
(e.g., rate(5 minutes)) or Cron Expression (e.g., cron(0/5 * * * ? *)).
Type: String
Resources:
TerminateIdleEMRLambdaIAMRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: iam-role-terminate-idle-emr
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Sid: ''
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
Policies:
- PolicyName: TerminateIdleEMRLambdaFunctionPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'elasticmapreduce:ListSteps'
- 'elasticmapreduce:ListClusters'
- 'elasticmapreduce:SetTerminationProtection'
- 'elasticmapreduce:TerminateJobFlows'
- 'logs:CreateLogStream'
- 'logs:CreateLogGroup'
- 'logs:PutLogEvents'
Resource: '*'
TerminateIdleEMRLambdaFunction:
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: lambda-function-terminate-idle-emr
Description: Lambda Function based on Python 3.7 to Terminate Idle EMR Clusters.
Environment:
Variables:
MAX_IDLE_TIME_IN_MINUTES: !Ref MaxIdleTimeInMinutes
Code:
S3Bucket: !Ref LambdaCodeS3Bucket
S3Key: !Ref LambdaCodeS3BucketKey
Handler: aws_auto_terminate_idle_emr.lambda_handler
Role: !GetAtt
- TerminateIdleEMRLambdaIAMRole
- Arn
Runtime: python3.7
Timeout: 900
MemorySize: 128
Tags:
- Key: Name
Value: terminate-idle-emr-lambda-function
TerminateIdleEMRCloudWatchEventRule:
Type: 'AWS::Events::Rule'
Properties:
Name: cw-scheduled-trigger-terminate-idle-emr
Description: >-
CloudWatch Event Triggered on Schduled Basis to Trigger Idle EMR
Clusters Termination Lambda Function.
ScheduleExpression: !Ref CloudWatchEventScheduleExpression
State: ENABLED
Targets:
- Arn: !GetAtt
- TerminateIdleEMRLambdaFunction
- Arn
Id: TargetTerminateIdleEMR
TerminateIdleEMRLambdaInvokePermission:
Type: 'AWS::Lambda::Permission'
Properties:
FunctionName: !GetAtt
- TerminateIdleEMRLambdaFunction
- Arn
Action: 'lambda:InvokeFunction'
Principal: events.amazonaws.com
SourceArn: !GetAtt
- TerminateIdleEMRCloudWatchEventRule
- Arn
Outputs:
TerminateIdleEMRLambdaFunctionARN:
Value: !GetAtt
- TerminateIdleEMRLambdaFunction
- Arn
Description: ARN for Terminate Idle EMR Lambda Function.
TerminateIdleEMRCloudWatchEventRuleARN:
Value: !GetAtt
- TerminateIdleEMRCloudWatchEventRule
- Arn
Description: ARN for Terminate Idle EMR CloudWatch Event Rule.