|
| 1 | +AWSTemplateFormatVersion: '2010-09-09' |
| 2 | +Description: Registration for mobile notifications |
| 3 | + |
| 4 | +Parameters: |
| 5 | + Stage: |
| 6 | + Type: String |
| 7 | + AllowedValues: |
| 8 | + - CODE |
| 9 | + - PROD |
| 10 | + Description: Environment name |
| 11 | + Postgres13InstanceType: |
| 12 | + Type: String |
| 13 | + Description: The instance type of Postgres13 database |
| 14 | + AllocatedStorage: |
| 15 | + Type: String |
| 16 | + Description: The amount of storage to allocate in GiB |
| 17 | + VpcId: |
| 18 | + Description: ID of the Notification VPC |
| 19 | + Type: AWS::EC2::VPC::Id |
| 20 | + PrivateVpcSubnets: |
| 21 | + Description: Private Subnets to use in VPC |
| 22 | + Type: List<AWS::EC2::Subnet::Id> |
| 23 | + VPCSecurityGroup: |
| 24 | + Type: AWS::EC2::SecurityGroup::Id |
| 25 | + Description: The default security group of the VPC |
| 26 | + MasterUserName: |
| 27 | + Type: String |
| 28 | + Description: The root user name to create in the database |
| 29 | + MasterPasswordReference: |
| 30 | + Type: String |
| 31 | + Description: The SSM key and version of the parameter i.e /notifications/CODE/registration.db.masterpassword:1 |
| 32 | + AlarmTopic: |
| 33 | + Type: String |
| 34 | + Description: The ARN of the SNS topic to send all the cloudwatch alarms to |
| 35 | + |
| 36 | +Mappings: |
| 37 | + StageVariables: |
| 38 | + CODE: |
| 39 | + AlarmActionsEnabled: FALSE |
| 40 | + LowerCaseStage: code |
| 41 | + PROD: |
| 42 | + AlarmActionsEnabled: TRUE |
| 43 | + LowerCaseStage: prod |
| 44 | + |
| 45 | +Resources: |
| 46 | + DBSecurityGroup: |
| 47 | + Type: AWS::EC2::SecurityGroup |
| 48 | + Properties: |
| 49 | + GroupName: !Sub registrations-db-${Stage} |
| 50 | + GroupDescription: !Sub Security group allowing VPC only traffic |
| 51 | + SecurityGroupIngress: |
| 52 | + - SourceSecurityGroupId: !Ref VPCSecurityGroup |
| 53 | + FromPort: 5432 |
| 54 | + IpProtocol: tcp |
| 55 | + ToPort: 5432 |
| 56 | + - SourceSecurityGroupId: !ImportValue NotificationsDefaultSecurityGroup-NotificationsMainSecurityGroup |
| 57 | + FromPort: 5432 |
| 58 | + IpProtocol: tcp |
| 59 | + ToPort: 5432 |
| 60 | + Tags: |
| 61 | + - Key: Stage |
| 62 | + Value: !Ref Stage |
| 63 | + - Key: Stack |
| 64 | + Value: mobile-notifications |
| 65 | + - Key: App |
| 66 | + Value: registrations-db |
| 67 | + VpcId: !Ref VpcId |
| 68 | + |
| 69 | + PrivateRegistrationPostgres13DB: |
| 70 | + Type: AWS::RDS::DBInstance |
| 71 | + DependsOn: PrivateRegistrationDBSubnetGroup |
| 72 | + DeletionPolicy: Retain |
| 73 | + Properties: |
| 74 | + AllocatedStorage: !Ref AllocatedStorage |
| 75 | + AutoMinorVersionUpgrade: True |
| 76 | + CACertificateIdentifier: rds-ca-rsa2048-g1 |
| 77 | + CopyTagsToSnapshot: True |
| 78 | + DBInstanceClass: !Ref Postgres13InstanceType |
| 79 | + DBInstanceIdentifier: !Sub notifications-registrations-db-private-pg13-${Stage} |
| 80 | + DBName: !Sub registrations${Stage} |
| 81 | + DBSubnetGroupName: !Sub notifications-registrations-db-subnet-group-private-${Stage} |
| 82 | + Engine: postgres |
| 83 | + EngineVersion: 13.6 |
| 84 | + MasterUsername: !Ref MasterUserName |
| 85 | + MasterUserPassword: !Sub "{{resolve:ssm-secure:${MasterPasswordReference}}}" |
| 86 | + MultiAZ: True |
| 87 | + Port: 5432 |
| 88 | + PreferredMaintenanceWindow: Sun:03:45-Sun:04:30 |
| 89 | + PubliclyAccessible: False |
| 90 | + StorageEncrypted: True |
| 91 | + StorageType: gp2 |
| 92 | + Tags: |
| 93 | + - Key: Stage |
| 94 | + Value: !Ref Stage |
| 95 | + - Key: Stack |
| 96 | + Value: mobile-notifications |
| 97 | + - Key: App |
| 98 | + Value: registrations-db |
| 99 | + - Key: devx-backup-enabled |
| 100 | + Value: true |
| 101 | + VPCSecurityGroups: |
| 102 | + - !Ref DBSecurityGroup |
| 103 | + |
| 104 | + PrivateRegistrationDBSubnetGroup: |
| 105 | + Type: AWS::RDS::DBSubnetGroup |
| 106 | + Properties: |
| 107 | + DBSubnetGroupName: !Sub notifications-registrations-db-subnet-group-private-${Stage} |
| 108 | + DBSubnetGroupDescription: Access to the notification VPC |
| 109 | + SubnetIds: !Ref PrivateVpcSubnets |
| 110 | + |
| 111 | + DiskspaceAlarmAlert: |
| 112 | + Type: AWS::CloudWatch::Alarm |
| 113 | + Properties: |
| 114 | + AlarmActions: [!Ref AlarmTopic] |
| 115 | + OKActions: [!Ref AlarmTopic] |
| 116 | + AlarmName: |
| 117 | + !Sub notifications-registrations-db-private-${Stage}-alarm-low-diskspace-alert |
| 118 | + AlarmDescription: Notifications registrations rds database has gone below the storage threshold |
| 119 | + Namespace: AWS/RDS |
| 120 | + MetricName: FreeStorageSpace |
| 121 | + Dimensions: |
| 122 | + - Name: DBInstanceIdentifier |
| 123 | + Value: !Sub |
| 124 | + - 'notifications-registrations-db-private-pg13-${LowerCaseStage}' |
| 125 | + - LowerCaseStage: !FindInMap [StageVariables, !Ref Stage, LowerCaseStage] |
| 126 | + Statistic: Minimum |
| 127 | + ComparisonOperator: LessThanOrEqualToThreshold |
| 128 | + Threshold: 26843545600 #this is measured in bytes so equates to 26.8GB |
| 129 | + Period: 60 |
| 130 | + EvaluationPeriods: 1 |
| 131 | + TreatMissingData: missing |
| 132 | + ActionsEnabled: !FindInMap [StageVariables, !Ref Stage, AlarmActionsEnabled] |
| 133 | + |
| 134 | + RAMAlarmAlert: |
| 135 | + Type: AWS::CloudWatch::Alarm |
| 136 | + Properties: |
| 137 | + AlarmActions: [!Ref AlarmTopic] |
| 138 | + OKActions: [!Ref AlarmTopic] |
| 139 | + AlarmName: |
| 140 | + !Sub notifications-registrations-db-private-${Stage}-alarm-low-ram-alert |
| 141 | + AlarmDescription: Notifications registrations rds database has gone below the freeable memory threshold |
| 142 | + Namespace: AWS/RDS |
| 143 | + MetricName: FreeableMemory |
| 144 | + Dimensions: |
| 145 | + - Name: DBInstanceIdentifier |
| 146 | + Value: !Sub |
| 147 | + - 'notifications-registrations-db-private-pg13-${LowerCaseStage}' |
| 148 | + - LowerCaseStage: !FindInMap [StageVariables, !Ref Stage, LowerCaseStage] |
| 149 | + Statistic: Minimum |
| 150 | + ComparisonOperator: LessThanOrEqualToThreshold |
| 151 | + Threshold: 250000000 #this is measured in bytes so equates to 250MB |
| 152 | + Period: 60 |
| 153 | + EvaluationPeriods: 1 |
| 154 | + TreatMissingData: missing |
| 155 | + ActionsEnabled: !FindInMap [StageVariables, !Ref Stage, AlarmActionsEnabled] |
| 156 | + |
| 157 | + BurstBalanceAlarmAlert: |
| 158 | + Type: AWS::CloudWatch::Alarm |
| 159 | + Properties: |
| 160 | + AlarmName: |
| 161 | + !Sub notifications-registrations-db-private-${Stage}-alarm-low-burst-balance-alert |
| 162 | + AlarmDescription: The burst balance for the notifications registrations rds database has gone below the threshold, please think about scaling the database to allow for recovery time. |
| 163 | + Namespace: AWS/RDS |
| 164 | + MetricName: BurstBalance |
| 165 | + Dimensions: |
| 166 | + - Name: DBInstanceIdentifier |
| 167 | + Value: !Sub |
| 168 | + - 'notifications-registrations-db-private-pg13-${LowerCaseStage}' |
| 169 | + - LowerCaseStage: !FindInMap [StageVariables, !Ref Stage, LowerCaseStage] |
| 170 | + Statistic: Average |
| 171 | + ComparisonOperator: LessThanOrEqualToThreshold |
| 172 | + Threshold: 30 #this unit is percentage |
| 173 | + Period: 60 |
| 174 | + EvaluationPeriods: 1 |
| 175 | + AlarmActions: [!Ref AlarmTopic] |
| 176 | + OKActions: [!Ref AlarmTopic] |
| 177 | + ActionsEnabled: !FindInMap [StageVariables, !Ref Stage, AlarmActionsEnabled] |
| 178 | + |
| 179 | +Outputs: |
| 180 | + DBUrl: |
| 181 | + Value: !GetAtt PrivateRegistrationPostgres13DB.Endpoint.Address |
0 commit comments