AWSTemplateFormatVersion: '2010-09-09'
Description: Deploy a Fargate service running the rds-data-generator-pub container

Parameters:
  ContainerName:
    Type: String
    Default: rds-data-generator
  VPCCIDR:
    Type: String
    Default: 10.1.0.0/16
  PublicSubnetOneCIDR:
    Type: String
    Default: 10.1.0.0/24
 

Resources:

  Cluster:
    Type: AWS::ECS::Cluster
    Properties:
      ClusterName: rds-data-generator-cluster
      Tags:
        - Key: Project
          Value: RDS-Data-Generator

  TaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Family: !Ref 'AWS::StackName'
      NetworkMode: awsvpc
      RequiresCompatibilities:
        - FARGATE
      Cpu: 256
      Memory: 512
      ContainerDefinitions:
        - Name: !Ref ContainerName
          Image: public.ecr.aws/r1n1x1w7/rds-data-generator-pub:latest
          PortMappings:
            - ContainerPort: 80
              HostPort: 80
      Tags:
        - Key: Project
          Value: RDS-Data-Generator      

  RDSDGService:
    Type: AWS::ECS::Service
    Properties:
      ServiceName: rds-data-generator-service
      Cluster: !Ref Cluster
      TaskDefinition: !Ref TaskDefinition
      LaunchType: FARGATE
      DesiredCount: 1
      NetworkConfiguration:
        AwsvpcConfiguration:
          AssignPublicIp: ENABLED
          SecurityGroups:
            - !Ref ServiceSecurityGroup
          Subnets:
            - !Ref PublicSubnetOne
      Tags:
        - Key: Project
          Value: RDS-Data-Generator      

  ServiceSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      VpcId: !Ref VPC
      GroupName: RDS-Data-Gen-SG
      GroupDescription: Allow HTTP traffic
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: 443
          ToPort: 443
          CidrIp: 0.0.0.0/0   
      Tags:
        - Key: Project
          Value: RDS-Data-Generator           

  PublicSubnetOne:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [0, !GetAZs '']
      CidrBlock: !Ref PublicSubnetOneCIDR
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Project
          Value: RDS-Data-Generator

  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref VPCCIDR
      EnableDnsHostnames: true
      EnableDnsSupport: true
      Tags:
        - Key: Project
          Value: RDS-Data-Generator

  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Project
          Value: RDS-Data-Generator    

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

  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Project
          Value: RDS-Data-Generator

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

  PublicSubnetOneRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicSubnetOne
      RouteTableId: !Ref PublicRouteTable

Outputs:
  ClusterName:
    Description: The name of the ECS cluster
    Value: !Ref Cluster

  ServiceName:
    Description: The name of the ECS service
    Value: !Ref RDSDGService

  VpcId:
    Description: The ID of the created VPC
    Value: !Ref VPC
     
  RouteTableId:
    Description: The ID of the created Route Table
    Value: !Ref PublicRouteTable 