-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf-template.yml
195 lines (195 loc) · 5.01 KB
/
cf-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
AWSTemplateFormatVersion: 2010-09-09
Parameters:
LambdaName:
Type: String
Description: Name of your lambda.
LambdaInterval:
Type: String
Description: Interval of your lambda (in minutes).
S3BucketName:
Type: String
Description: S3 bucket name containing lambda code.
S3CodeArchivePath:
Type: String
Description: Lambda code path inside selected S3 bucket.
AESKey:
Type: String
Description: AES key that code needs to decrypt config file.
ConfigFile:
Type: String
Description: S3 public path to config file.
Resources:
IAMRoleInstance:
Type: 'AWS::IAM::Role'
Properties:
RoleName: !Join
- ''
- - !Ref LambdaName
- '-role'
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
Effect: Allow
Action:
- 'sts:AssumeRole'
Principal:
Service:
- lambda.amazonaws.com
Policies:
- PolicyName: root
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: 'arn:aws:logs:*:*:*'
- Effect: Allow
Action:
- 's3:Get*'
- 's3:PutObject'
Resource: !Join
- ''
- - 'arn:aws:s3:::'
- !Ref S3BucketName
- '/*'
KMSKeyInstance:
Type: 'AWS::KMS::Key'
Properties:
Description: !Join
- ''
- - 'A key to encrypt environment variables of lambda: '
- !Ref LambdaName
KeyPolicy:
Version: 2012-10-17
Id: key-default-1
Statement:
- Sid: Allow administration of the key
Effect: Allow
Principal:
AWS:
- !Join
- ''
- - 'arn:aws:iam::'
- !Ref 'AWS::AccountId'
- ':role/'
- !Join
- ''
- - !Ref LambdaName
- '-role'
- !Join
- ''
- - 'arn:aws:iam::'
- !Ref 'AWS::AccountId'
- ':root'
Action:
- 'kms:*'
Resource: '*'
Tags:
- Key: Creator
Value: Cloudformation Template
DependsOn:
- IAMRoleInstance
KMSKeyAliasInstance:
Type: 'AWS::KMS::Alias'
Properties:
AliasName: !Join
- ''
- - 'alias/'
- !Ref LambdaName
- '-key'
TargetKeyId: !Ref KMSKeyInstance
DependsOn:
- KMSKeyInstance
LambdaInstance:
Type: 'AWS::Lambda::Function'
Properties:
Code:
S3Bucket: !Ref S3BucketName
S3Key: !Ref S3CodeArchivePath
FunctionName: !Ref LambdaName
Description: 'Lambda, created from stack, to monitor endpoints.'
Environment:
Variables:
AES_KEY: !Ref AESKey
CONFIG_FILE: !Ref ConfigFile
Role: !Join
- ''
- - 'arn:aws:iam::'
- !Ref 'AWS::AccountId'
- ':role/'
- !Join
- ''
- - !Ref LambdaName
- '-role'
Handler: script.main
Runtime: python3.6
MemorySize: 192
Timeout: '30'
KmsKeyArn: !GetAtt
- KMSKeyInstance
- Arn
DependsOn:
- IAMRoleInstance
- KMSKeyInstance
CloudWatchEvent:
Type: 'AWS::Events::Rule'
Properties:
Description: !Join
- ''
- - 'Rule for '
- !Ref LambdaName
- ' lambda.'
Name: !Join
- ''
- - !Ref LambdaName
- '-rule'
ScheduleExpression: !Join
- ''
- - 'cron(*/'
- !Ref LambdaInterval
- ' * * * ? *)'
State: ENABLED
Targets:
-
Arn: !GetAtt
- LambdaInstance
- Arn
Id: !Join
- ''
- - 'ruleTarget'
- !Ref LambdaName
DependsOn:
- LambdaInstance
CloudWatchEventPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref LambdaName
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
SourceArn: !GetAtt
- CloudWatchEvent
- Arn
Outputs:
LambdaName:
Description: Name of created AWS Lambda function.
Value: !GetAtt
- LambdaInstance
- Arn
LambdaAttachedRoleName:
Description: Name of attached role to AWS Lambda function.
Value: !GetAtt
- IAMRoleInstance
- Arn
LambdaAttachedKMSKey:
Description: Name of attached KMS Key to AWS Lambda function.
Value: !GetAtt
- KMSKeyInstance
- Arn
CloudWatchRule:
Description: Name of CloudWatch rule attached to AWS Lambda function.
Value: !GetAtt
- CloudWatchEvent
- Arn