-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yml
85 lines (80 loc) · 2.51 KB
/
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
AWSTemplateFormatVersion: "2010-09-09"
Description: Example of how to auto scale down resources to safe cost
Transform: AWS::Serverless-2016-10-31
Parameters:
ScaleDownOffHours:
Type: String
Default: "false"
Conditions:
ConfigureScaleDownOffHours: !Equals [ "true", !Ref ScaleDownOffHours ]
Resources:
DatabaseSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: DB Security Group
DatabaseCluster:
Type: AWS::RDS::DBCluster
Properties:
Engine: aurora-postgresql
EngineVersion: "14"
MasterUsername: postgres
ManageMasterUserPassword: true
Database:
Type: AWS::RDS::DBInstance
Properties:
Engine: aurora-postgresql
DBInstanceClass: "db.t4g.medium"
DBClusterIdentifier: !Ref DatabaseCluster
PubliclyAccessible: 'true'
ScaleDownOffHoursStateMachine:
Condition: ConfigureScaleDownOffHours
Type: AWS::Serverless::StateMachine
Properties:
Events:
ScaleDown:
Type: ScheduleV2
Properties:
ScheduleExpressionTimezone: America/Chicago
ScheduleExpression: "cron(0 17 * * ? *)"
ScaleUp:
Type: EventBridgeRule
Properties:
Pattern:
source: [ "aws.cloudformation" ]
account: [ !Ref AWS::AccountId ]
detail-type: [ "CloudFormation Stack Status Change" ]
detail:
stack-id: [ !Ref AWS::StackId ]
status-details:
status: [ "UPDATE_IN_PROGRESS" ]
Policies:
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- rds:StopDBCluster
- rds:StartDBCluster
Resource:
- !GetAtt DatabaseCluster.DBClusterArn
Definition:
StartAt: DetermineDirection
States:
DetermineDirection:
Type: Choice
Choices:
- Variable: "$$.Execution.Input.source"
StringEquals: aws.scheduler
Next: ScaleDown
Default: ScaleUp
ScaleUp:
Type: Task
Resource: "arn:aws:states:::aws-sdk:rds:startDBCluster"
Parameters:
DbClusterIdentifier: !Ref DatabaseCluster
End: true
ScaleDown:
Type: Task
Resource: "arn:aws:states:::aws-sdk:rds:stopDBCluster"
Parameters:
DbClusterIdentifier: !Ref DatabaseCluster
End: true