AWSTemplateFormatVersion: "2010-09-09"
Description: Cfn Template by CloudAdvisor
Parameters: {}
Resources:
  VPC:
    Type: "AWS::EC2::VPC"
    Properties:
      CidrBlock: "10.20.0.0/16"
      EnableDnsHostnames: true
      EnableDnsSupport: true
      Tags:
        - Key: "Name"
          Value: "iac-vpc"
  Subnet:
    Type: "AWS::EC2::Subnet"
    Properties:
      CidrBlock: "10.20.100.0/24"
      VpcId: !Ref VPC
      Tags:
        - Key: "Name"
          Value: "iac-pub-sn-a"
      MapPublicIpOnLaunch: true
      AvailabilityZone: !Select [0, !GetAZs ""]
  Subnet2:
    Type: "AWS::EC2::Subnet"
    Properties:
      CidrBlock: "10.20.101.0/24"
      MapPublicIpOnLaunch: true
      Tags:
        - Key: "Name"
          Value: "iac-pub-sn-c"
      VpcId: !Ref VPC
      AvailabilityZone: !Select [2, !GetAZs ""]
  Subnet3:
    Type: "AWS::EC2::Subnet"
    Properties:
      CidrBlock: "10.20.200.0/24"
      Tags:
        - Key: "Name"
          Value: "iac-priv-sn-a"
      VpcId: !Ref VPC
      AvailabilityZone: !Select [0, !GetAZs ""]
  Subnet4:
    Type: "AWS::EC2::Subnet"
    Properties:
      EnableDns64: false
      CidrBlock: "10.20.201.0/24"
      Tags:
        - Key: "Name"
          Value: "iac-priv-sn-c"
      VpcId: !Ref VPC
      AvailabilityZone: !Select [2, !GetAZs ""]
  InternetGateway:
    Type: "AWS::EC2::InternetGateway"
    Properties:
      Tags:
        - Key: "Name"
          Value: "iac-igw"
  VPCGatewayAttachment:
    Type: "AWS::EC2::VPCGatewayAttachment"
    Properties:
      InternetGatewayId: !Ref InternetGateway
      VpcId: !Ref VPC
  EIP:
    Type: "AWS::EC2::EIP"
    Properties:
      Domain: "vpc"
  EIP2:
    Type: "AWS::EC2::EIP"
    Properties:
      Domain: "vpc"
  NatGateway:
    Type: "AWS::EC2::NatGateway"
    Properties:
      AllocationId: !GetAtt EIP.AllocationId
      SubnetId: !Ref Subnet
      Tags:
        - Key: "Name"
          Value: "iac-natgw-a"
  NatGateway2:
    Type: "AWS::EC2::NatGateway"
    Properties:
      AllocationId: !GetAtt EIP2.AllocationId
      SubnetId: !Ref Subnet2
      Tags:
        - Key: "Name"
          Value: "iac-natgw-c"
  RouteTable:
    Type: "AWS::EC2::RouteTable"
    Properties:
      Tags:
        - Key: "Name"
          Value: "iac-public-rt"
      VpcId: !Ref VPC
  RouteTable2:
    Type: "AWS::EC2::RouteTable"
    Properties:
      Tags:
        - Key: "Name"
          Value: "iac-private-rt-a"
      VpcId: !Ref VPC
  RouteTable3:
    Type: "AWS::EC2::RouteTable"
    Properties:
      Tags:
        - Key: "Name"
          Value: "iac-private-rt-c"
      VpcId: !Ref VPC
  SubnetRouteTableAssociation:
    Type: "AWS::EC2::SubnetRouteTableAssociation"
    Properties:
      RouteTableId: !Ref RouteTable
      SubnetId: !Ref Subnet
  SubnetRouteTableAssociation2:
    Type: "AWS::EC2::SubnetRouteTableAssociation"
    Properties:
      RouteTableId: !Ref RouteTable
      SubnetId: !Ref Subnet2
  SubnetRouteTableAssociation3:
    Type: "AWS::EC2::SubnetRouteTableAssociation"
    Properties:
      RouteTableId: !Ref RouteTable2
      SubnetId: !Ref Subnet3
  SubnetRouteTableAssociation4:
    Type: "AWS::EC2::SubnetRouteTableAssociation"
    Properties:
      RouteTableId: !Ref RouteTable3
      SubnetId: !Ref Subnet4
  Route4:
    Type: "AWS::EC2::Route"
    Properties:
      GatewayId: !Ref InternetGateway
      RouteTableId: !GetAtt RouteTable.RouteTableId
      DestinationCidrBlock: "0.0.0.0/0"
  Route5:
    Type: "AWS::EC2::Route"
    Properties:
      DestinationCidrBlock: "0.0.0.0/0"
      NatGatewayId: !GetAtt NatGateway.NatGatewayId
      RouteTableId: !GetAtt RouteTable2.RouteTableId
  Route6:
    Type: "AWS::EC2::Route"
    Properties:
      DestinationCidrBlock: "0.0.0.0/0"
      NatGatewayId: !GetAtt NatGateway2.NatGatewayId
      RouteTableId: !GetAtt RouteTable3.RouteTableId

  ECSCluster:
    Type: "AWS::ECS::Cluster"
    Properties:
      ClusterName: iac-ecs-cluster

  TaskDefinition:
    Type: "AWS::ECS::TaskDefinition"
    Properties:
      ContainerDefinitions:
        - Command: []
          Image: "nginx:latest"
          Name: "nginx"
          PortMappings:
            - ContainerPort: 80
              HostPort: 80
      Cpu: 512
      Family: "nginx"
      Memory: 1024
      NetworkMode: "awsvpc"
      TaskRoleArn: "arn:aws:iam::362708816803:role/ecsTaskExecutionRole"
      ExecutionRoleArn: "arn:aws:iam::362708816803:role/ecsTaskExecutionRole"
      RequiresCompatibilities:
        - "FARGATE"
      RuntimePlatform:
        OperatingSystemFamily: "LINUX"
        CpuArchitecture: "X86_64"

  ECSService:
    Type: "AWS::ECS::Service"
    DependsOn:
      - Listener
    Properties:
      ServiceName: "iac-nginx-svc"
      TaskDefinition:
        Ref: "TaskDefinition"
      DesiredCount: 2
      LaunchType: "FARGATE"
      NetworkConfiguration:
        AwsvpcConfiguration:
          Subnets:
            - !Ref Subnet3
            - !Ref Subnet4
          SecurityGroups:
            - !Ref ECSSecurityGroup
      LoadBalancers:
        - TargetGroupArn:
            Ref: "TargetGroup"
          ContainerPort: 80
          ContainerName: "nginx"
      Cluster:
        Ref: "ECSCluster"

  ECSSecurityGroup:
    Type: "AWS::EC2::SecurityGroup"
    Properties:
      GroupDescription: "ecs"
      SecurityGroupIngress:
        - CidrIp: "0.0.0.0/0"
          FromPort: 80
          IpProtocol: "tcp"
          ToPort: 80
      Tags:
        - Key: "Name"
          Value: "iac-service-sg"
      VpcId: !Ref VPC

  LoadBalancer:
    Type: "AWS::ElasticLoadBalancingV2::LoadBalancer"
    Properties:
      LoadBalancerName: "iac-alb"
      Type: "application"
      Subnets:
        - !Ref Subnet
        - !Ref Subnet2
      SecurityGroups:
        - !Ref ALBSecurityGroup
      Tags:
        - Key: "Name"
          Value: "iac-alb"

  ALBSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: "Allow HTTP"
      VpcId: !Ref VPC
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          CidrIp: "0.0.0.0/0"

  TargetGroup:
    Type: "AWS::ElasticLoadBalancingV2::TargetGroup"
    Properties:
      Port: 80
      Protocol: "HTTP"
      VpcId:
        Ref: "VPC"
      TargetType: "ip"
  Listener:
    Type: "AWS::ElasticLoadBalancingV2::Listener"
    Properties:
      LoadBalancerArn:
        Ref: "LoadBalancer"
      Port: 80
      Protocol: "HTTP"
      DefaultActions:
        - Type: "forward"
          TargetGroupArn:
            Ref: "TargetGroup"

Outputs: {}
Conditions: {}
Mappings: {}
Metadata: {}