-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
123 lines (115 loc) · 3.68 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
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
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: SAM Template for Lambda that cleans up old Lambda versions
Parameters:
NumberOfVersionsToKeep:
Type: Number
Description: Number of latest Lambda versions to keep per Lambda function
AWSRegions:
Type: CommaDelimitedList
Description: Comma delimited list of AWS regions to apply scheduled Lambda clean ups to
AllowedValues:
- us-east-1
- us-east-2
- us-west-1
- us-west-2
- ap-east-1
- ap-southeast-3
- ap-south-1
- ap-northeast-3
- ap-northeast-2
- ap-southeast-1
- ap-southeast-2
- ap-northeast-1
- ca-central-1
- eu-central-1
- eu-west-1
- eu-west-2
- eu-south-1
- eu-west-3
- eu-north-1
AlarmsSNSTopic:
Type: String
Description: SNS topic ARN to send cloudwatch alerts to
Default: ""
Conditions:
AlarmsSNSTopicExists: !Not
- !Equals
- !Ref AlarmsSNSTopic
- ""
Resources:
# ----------------------------------------------------------------------------
# AWS::Serverless::Function
# ----------------------------------------------------------------------------
LambdaVersionCleaner:
Type: AWS::Serverless::Function
Properties:
FunctionName: lambda-version-cleaner
CodeUri: ./functions/lambda-version-cleaner
Handler: app.lambda_handler
MemorySize: 128
Timeout: 600
Role: !GetAtt LambdaVersionCleanerIAMRole.Arn
Runtime: python3.11
Environment:
Variables:
NUM_VERSIONS_TO_KEEP: !Ref NumberOfVersionsToKeep
REGIONS: !Join [",", !Ref AWSRegions]
Events:
Daily:
Type: Schedule
Properties:
Schedule: rate(7 days)
# ----------------------------------------------------------------------------
# AWS::IAM::Role
# ----------------------------------------------------------------------------
LambdaVersionCleanerIAMRole:
Type: AWS::IAM::Role
Properties:
RoleName: lambda-version-cleaner-role
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: AllowLambdaServiceToAssumeRole
Effect: Allow
Action:
- sts:AssumeRole
Principal:
Service:
- lambda.amazonaws.com
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: AllowLambdaActions
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- lambda:ListAliases
- lambda:ListFunctions
- lambda:ListVersionsByFunction
- lambda:DeleteFunction
Resource: "*"
# ----------------------------------------------------------------------------
# AWS::CloudWatch::Alarm
# ----------------------------------------------------------------------------
LambdaVersionCleanerInvocationAlarm:
Condition: AlarmsSNSTopicExists
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: !Sub "${LambdaVersionCleaner} | Failed invocation | infr-3"
AlarmDescription: Alarm for Lambda function invocation failure
Namespace: AWS/Lambda
MetricName: Errors
Dimensions:
- Name: FunctionName
Value: !Ref LambdaVersionCleaner
ComparisonOperator: GreaterThanThreshold
EvaluationPeriods: 1
Period: 300
Statistic: SampleCount
Threshold: 1
AlarmActions:
- !Sub arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${AlarmsSNSTopic}
TreatMissingData: missing