-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
87 lines (78 loc) · 3.08 KB
/
template.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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
SAM Template that allow you to schedule a CRON job to perform START, STOP or REBOOT for
EC2 INSTANCES that match TAG_KEY and TAG_VALUE
Refer: https://github.com/devops-made-easy/ec2-cron
Globals:
Function:
Timeout: 300
Parameters:
TagKey:
Type: String
Description: Name of the Tag Key associated to EC2 instance. Example=Name
TagValue:
Type: String
Description: Value of the Tag Key associated to EC2 instance. Example=Demo
Action:
Type: String
AllowedValues: [ "START" , "STOP", "REBOOT" ]
Description: Action to be performed on associated EC2 instance. Allowed Values are "START" or "STOP" or "REBOOT".
CronSchedule:
Type: String
Description: Refer https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html
Metadata:
AWS::ServerlessRepo::Application:
Name: ec2-cron
Description: SAM Template that allow you to schedule a CRON job(cloudwatch rule) to perform START, STOP or REBOOT for EC2 INSTANCES for given tag key and its value.
Author: devops-made-easy
SpdxLicenseId: MIT
LicenseUrl: LICENSE
ReadmeUrl: README.md
Labels: ['devops-made-easy', "ec2-start-stop", "savings", "auto-start-stop-reboot", "ec2", "cron", "scheduler", "cronjob", "cloudwatch", "automation"]
HomePageUrl: https://github.com/devops-made-easy/ec2-cron
SemanticVersion: 1.0.5
SourceCodeUrl: https://github.com/devops-made-easy/ec2-cron
Resources:
EC2CronFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ec2_cron/
Handler: app.lambda_handler
Runtime: python3.8
Environment:
Variables:
TAG_KEY: !Ref TagKey
TAG_VALUE: !Ref TagValue
ACTION: !Ref Action
Policies:
- Statement:
- Effect: "Allow"
Action:
- "ec2:RebootInstances"
- "ec2:StartInstances"
- "ec2:StopInstances"
Resource: !Sub "arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:instance:*"
- Effect: "Allow"
Action:
- "ec2:DescribeInstances"
Resource: '*'
Events:
CloudWatchEvent:
Type: Schedule
Properties:
Schedule:
Ref: CronSchedule
#Outputs:
# # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# # Find out more about other implicit resources you can reference within SAM
# # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
# HelloWorldApi:
# Description: "API Gateway endpoint URL for Prod stage for Hello World function"
# Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
# HelloWorldFunction:
# Description: "Hello World Lambda Function ARN"
# Value: !GetAtt HelloWorldFunction.Arn
# HelloWorldFunctionIamRole:
# Description: "Implicit IAM Role created for Hello World function"
# Value: !GetAtt HelloWorldFunctionRole.Arn