-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpenny_pincher_cfn.yml
executable file
·111 lines (105 loc) · 3.66 KB
/
penny_pincher_cfn.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
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
LambdaIAMRoleArn:
Description: 'Takes ARN of IAM role which we are attaching to lambda'
Type: 'String'
AccountName:
Description: '(Optional)- Takes account name as input'
Type: 'String'
ReportingPlatform:
Description: 'Takes comma seperated reporting values, allowed values are S3, Slack, Email'
Type: 'String'
Default: 's3'
SlackChannelName:
Description: '(If you have chosen slack as a reporting platform)Takes slack channel name as input. Keep the defaults in case slack notifications are not required'
Type: 'String'
Default: '-'
WebhookUrl:
Description: '(If you have chosen slack as a reporting platform)Takes slack webhook url as input . Keep the defaults in case slack notifications are not required'
Type: 'String'
Default: '-'
SenderEmailAddress:
Type: 'String'
Description: '(If you have chosen email as a reporting platform) Takes the email id from which the mail containing cost report will be sent. Keep the defaults in case email notifications are not required'
RecipientEmailAddress:
Description: '(If you have chosen email as a reporting platform) Takes email id to which the mail containing cost report will be sent. Keep the defaults in case email notifications are not required'
Type: 'String'
SESRegion:
Description: '(Optional) Takes the AWS region abbreviation (e.g. ap-south-1 for Mumbai) in which SES is configured'
Type: 'String'
Default: 'us-east-1'
ReportBucket:
Description: '(Optional)- Takes existing S3 bucket to store reports'
Type: "String"
Default: '-'
Conditions:
VerifyToEmail: !Equals
- !Ref RecipientEmailAddress
- ""
IfToEmailNotNull: !Not
- Condition: VerifyToEmail
VerifyFromEmail: !Equals
- !Ref SenderEmailAddress
- ""
IfFromEmailNotNull: !Not
- Condition: VerifyFromEmail
Resources:
LambdaFunction:
Type: 'AWS::Lambda::Function'
Description: 'Generates Cost report with all the idle resources information and sends it to email or slack or both'
Properties:
FunctionName: !Join
- "-"
- - "Penny-Pincher-Lambda"
- !Select
- 0
- !Split
- "-"
- !Select
- 2
- !Split
- "/"
- !Ref "AWS::StackId"
Architectures:
- x86_64
Runtime: 'python3.8'
Role: !Ref LambdaIAMRoleArn
Handler: 'main.cfnresponsefun'
Code:
S3Bucket: penny-pincher-s3-bucket
S3Key: 'penny.zip'
PackageType: 'Zip'
Timeout: 900
MemorySize: 1024
Environment:
Variables:
ACCOUNT_NAME: !Ref AccountName
REPORTING_PLATFORM: !Ref ReportingPlatform
CHANNEL_NAME: !Ref SlackChannelName
FROM_ADDRESS: !Ref SenderEmailAddress
TO_ADDRESS: !Ref RecipientEmailAddress
SES_REGION: !Ref SESRegion
REPORT_BUCKET: !Ref ReportBucket
WEBHOOK_URL: !Ref WebhookUrl
TriggerPennyPincher:
Type: Custom::TriggerPennyPincher
DependsOn: LambdaFunction
Properties:
ServiceToken: !GetAtt LambdaFunction.Arn
CronToTriggerLambda:
Type: AWS::Events::Rule
Properties:
Description: "ScheduleRule"
Name: LambdaTrigger
ScheduleExpression: rate(14 days)
State: ENABLED
Targets:
- Id: PennypincherLambdafunction
Arn: !GetAtt LambdaFunction.Arn
PermissionForEventsToInvokeLambda:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref LambdaFunction
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn: !GetAtt CronToTriggerLambda.Arn