AWSTemplateFormatVersion: 2010-09-09

Parameters:
  ClusterName:
    Type: String
    Description: Name of MSK Cluster
  ClusterConfigurationArn:
    Type: String
    Description: MSK Cluster Configuration ARN
  ClusterConfigurationRevision:
    Type: Number
    Description: MSK Cluster Configuration Revision number
    Default: 1

Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 172.31.0.0/16
      Tags:
        - Key: Name
          Value: !Ref AWS::StackName

  PublicSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone:
        Fn::Select:
          - 0
          - Fn::GetAZs: ''
      CidrBlock: 172.31.0.0/24
      MapPublicIpOnLaunch: true

  PrivateSubnetA:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone:
        Fn::Select:
          - 0
          - Fn::GetAZs: ''
      CidrBlock: 172.31.3.0/24
      MapPublicIpOnLaunch: false

  PrivateSubnetB:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone:
        Fn::Select:
          - 1
          - Fn::GetAZs: ''
      CidrBlock: 172.31.2.0/24
      MapPublicIpOnLaunch: false

  InternetGateway:
    Type: AWS::EC2::InternetGateway

  GatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref VPC
      InternetGatewayId: !Ref InternetGateway

  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC

  PublicRoute:
    Type: AWS::EC2::Route
    DependsOn: GatewayAttachment
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway

  PublicSubnetRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicSubnet
      RouteTableId: !Ref PublicRouteTable

  NatGateway:
    Type: AWS::EC2::NatGateway
    DependsOn: NatPublicIP
    Properties:
      AllocationId: !GetAtt NatPublicIP.AllocationId
      SubnetId: !Ref PublicSubnet

  NatPublicIP:
    Type: AWS::EC2::EIP
    DependsOn: VPC
    Properties:
      Domain: vpc

  PrivateRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC

  PrivateRoute:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref NatGateway

  PrivateSubnetARouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PrivateSubnetA
      RouteTableId: !Ref PrivateRouteTable

  PrivateSubnetBRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PrivateSubnetB
      RouteTableId: !Ref PrivateRouteTable

  MSKCluster:
    Type: 'AWS::MSK::Cluster'
    Properties:
      ClusterName: !Ref ClusterName
      KafkaVersion: 2.2.1
      NumberOfBrokerNodes: 2
      BrokerNodeGroupInfo:
        InstanceType: kafka.t3.small
        ClientSubnets:
          - !Ref PrivateSubnetA
          - !Ref PrivateSubnetB
        StorageInfo:
          EBSStorageInfo:
            VolumeSize: 1
      ConfigurationInfo:
        Arn: !Ref ClusterConfigurationArn
        Revision: !Ref ClusterConfigurationRevision

  FileSystem:
    Type: AWS::EFS::FileSystem
    Properties:
      PerformanceMode: generalPurpose
      FileSystemTags:
        - Key: Name
          Value: ServerlessFrameworkTestsVolume

  MountTarget:
    Type: AWS::EFS::MountTarget
    Properties:
      FileSystemId: !Ref FileSystem
      SubnetId: !Ref PrivateSubnetA
      SecurityGroups:
        - !GetAtt VPC.DefaultSecurityGroup

  AccessPointResource:
    Type: AWS::EFS::AccessPoint
    Properties:
      FileSystemId: !Ref FileSystem
      PosixUser:
        Uid: 1001
        Gid: 1001
      RootDirectory:
        CreationInfo:
          OwnerGid: 1001
          OwnerUid: 1001
          Permissions: 770
        Path: /efs

Outputs:
  VPC:
    Description: VPC ID
    Value: !Ref VPC

  PrivateSubnetA:
    Description: Private Subnet A ID
    Value: !Ref PrivateSubnetA

  SecurityGroup:
    Description: Default security for Lambda VPC
    Value: !GetAtt VPC.DefaultSecurityGroup

  MSKCluster:
    Description: Created MSK Cluster
    Value: !Ref MSKCluster

  EFSAccessPointARN:
    Description: EFS Access Point ARN
    Value: !GetAtt AccessPointResource.Arn
