---
AWSTemplateFormatVersion: '2010-09-09'
Description: Handel-created PostgreSQL RDS instance

Parameters:
  DBUsername:
    NoEcho: true
    Description: The username of the database
    Type: String
  DBPassword:
    NoEcho: true
    Description: The password of the database
    Type: String

Resources:
  Instance:
    Type: AWS::RDS::DBInstance
    Properties:
      AllocatedStorage: {{storageGB}}
      AllowMajorVersionUpgrade: true
      AutoMinorVersionUpgrade: true
      DBInstanceClass: {{instanceType}}
      DBInstanceIdentifier: {{dbName}}
      DBName: {{databaseName}}
      DBParameterGroupName: !Ref ParameterGroup
      DBSubnetGroupName: {{dbSubnetGroup}}
      Engine: postgres
      EngineVersion: {{postgresVersion}}
      MasterUsername: !Ref DBUsername
      MasterUserPassword: !Ref DBPassword
      {{#if multiAZ}}
      MultiAZ: true
      {{/if}}
      Port: {{dbPort}}
      PubliclyAccessible: false
      StorageType: {{storageType}}
      Tags:
      {{#if tags}}
      {{#each tags}}
      - Key: {{@key}}
        Value: {{this}}
      {{/each}}
      {{/if}}
      - Key: Name
        Value: {{dbName}}
      VPCSecurityGroups:
      - {{dbSecurityGroupId}}
  ParameterGroup:
    Type: AWS::RDS::DBParameterGroup
    Properties:
      Description: {{description}}
      Family: {{parameterGroupFamily}}
      {{#if parameterGroupParams}}
      Parameters:
        {{#each parameterGroupParams}}
        {{@key}}: '{{this}}'
        {{/each}}
      {{/if}}
      Tags:
      {{#if tags}}
      {{#each tags}}
      - Key: {{@key}}
        Value: {{this}}
      {{/each}}
      {{/if}}
      - Key: Name
        Value: {{dbName}}
Outputs:
  DatabaseAddress:
    Description: The address of the RDS database
    Value: !GetAtt Instance.Endpoint.Address
  DatabasePort:
    Description: The port of the RDS database
    Value: !GetAtt Instance.Endpoint.Port
  DatabaseName:
    Description: The name of the database
    Value: {{databaseName}}
