AWSTemplateFormatVersion: "2010-09-09"

Parameters:
  SshKeyPairName:
    Type: AWS::EC2::KeyPair::KeyName
    Default: MyKeyPair

  DBInstanceIdentifier:
    Type: String
    Default: db2tpcc
  
  Db2DbName:
    Type: String
    Default: TPCC

  MasterUsername:
    Type: String
    Default: db2inst1

  MasterUserPassword:
    Type: String
    NoEcho: true

  Db2ServicePort:
    Type: String
    Default: 50001

  IbmCustomerId:
    Type: String
    Default: "" # "xxxxxxx"
  
  IbmSiteId:
    Type: String
    Default: "" # "xxxxxxxxxx"
  
  ClientIpCidr:
    Type: String
    Default: x.x.x.x/x
  
  UbuntuRootPassword:
    Type: String
    NoEcho: true

Resources:
#################################################
## CF100 Shared Resource
#################################################
  CF110CommonEc2ResourceStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://aws-blogs-artifacts-public.s3.amazonaws.com/DBBLOG-3840/CF110-Common-Ec2-Resource.yaml
      TimeoutInMinutes: 40
      #Parameters:
      #  EnvironmentName: !Ref EnvironmentName

#################################################
## CF200: Corporate VPC
#################################################
  CF210CorporateVpcStack:
    Type: AWS::CloudFormation::Stack
    #DeletionPolicy: Retain
    #UpdateReplacePolicy: Retain
    Properties:
      TemplateURL: https://aws-blogs-artifacts-public.s3.amazonaws.com/DBBLOG-3840/CF120-Vpc-Template.yaml
      TimeoutInMinutes: 40
      Parameters:
        EnvironmentName: Corporate
        VpcCidr: 10.1.0.0/16
        PublicSubnetAz1Cidr: 10.1.11.0/24
        PublicSubnetAz2Cidr: 10.1.12.0/24
        PrivateSubnetAz1Cidr: 10.1.21.0/24
        PrivateSubnetAz2Cidr: 10.1.22.0/24
        TgwSubnetAz1Cidr: 10.1.251.0/24
        TgwSubnetAz2Cidr: 10.1.252.0/24

#################################################
## CF310: CF310-Db2
#################################################
  CF310Db2Stack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://aws-blogs-artifacts-public.s3.amazonaws.com/DBBLOG-3840/CF310-Db2.yaml
      TimeoutInMinutes: 40
      Parameters:
        DBInstanceIdentifier: !Ref DBInstanceIdentifier
        Db2DbName: !Ref Db2DbName
        MasterUsername: !Ref MasterUsername
        MasterUserPassword: !Ref MasterUserPassword
        Db2ServicePort: !Ref Db2ServicePort
        IbmCustomerId: !Ref IbmCustomerId
        IbmSiteId: !Ref IbmSiteId
        VpcId: !GetAtt CF210CorporateVpcStack.Outputs.VpcId
        DbSubnetAz1: !GetAtt CF210CorporateVpcStack.Outputs.PrivateSubnetAz1
        DbSubnetAz2: !GetAtt CF210CorporateVpcStack.Outputs.PrivateSubnetAz2
        VpcCidr: !GetAtt CF210CorporateVpcStack.Outputs.VpcCidr

#################################################
## CF410: CF410-Client-Ubuntu
#################################################
  CF410ClientUbuntuStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://aws-blogs-artifacts-public.s3.amazonaws.com/DBBLOG-3840/CF410-Client-Ubuntu.yaml
      TimeoutInMinutes: 40
      Parameters:
        VpcId: !GetAtt CF210CorporateVpcStack.Outputs.VpcId
        DeploySubnet: !GetAtt CF210CorporateVpcStack.Outputs.PublicSubnetAz1
        Ec2Identifier: Az1-1
        Ec2Profile: !GetAtt CF110CommonEc2ResourceStack.Outputs.Ec2InstanceProfile
        InstanceType: c5.2xlarge
        SshKeyPairName: !Ref SshKeyPairName
        ClientIpCidr: !Ref ClientIpCidr
        UbuntuRootPassword: !Ref UbuntuRootPassword

Outputs:
# InputParmeters
  MasterUsername:
    Value: !Ref MasterUsername

  MasterUserPassword:
    Value: !Ref MasterUserPassword

  UbuntuRootPassword:
    Value: !Ref UbuntuRootPassword

## CF310: CF310-Db2
  CF310Db2InstanceDns:
    Value: !GetAtt CF310Db2Stack.Outputs.Db2InstanceDns
  
  CF310Db2InstancePort:
    Value: !GetAtt CF310Db2Stack.Outputs.Db2InstancePort

## CF410: Ubuntu Ec2
  CF410ClientUbuntuSsh:
    Value: !GetAtt CF410ClientUbuntuStack.Outputs.Ec2InstanceSsh
  
  CF410ClientUbuntuPublicIp:
    Value: !GetAtt CF410ClientUbuntuStack.Outputs.Ec2PublicIp
