AWSTemplateFormatVersion: '2010-09-09'
Description: >
  Crée 3 VPCs avec 2 subnets publics et privés dans chaque AZ.
  Déploie une instance EC2 t2.micro dans 1 subnet public de chaque VPC.


Parameters:
  AMIId:
    Type: String
    Description: ID de l'AMI a utiliser pour les instances EC2


Resources:
  # VPCs
  VPC1:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.5.0.0/16
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: VPC1

  VPC2:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.6.0.0/16
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: VPC2

  VPC3:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.7.0.0/16
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: VPC3

  # Internet Gateway
  InternetGateway1:
    Type: AWS::EC2::InternetGateway

  InternetGateway2:
    Type: AWS::EC2::InternetGateway

  InternetGateway3:
    Type: AWS::EC2::InternetGateway

  AttachIGW1:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref VPC1
      InternetGatewayId: !Ref InternetGateway1

  AttachIGW2:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref VPC2
      InternetGatewayId: !Ref InternetGateway2

  AttachIGW3:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref VPC3
      InternetGatewayId: !Ref InternetGateway3

  # Security Groups
  SecurityGroup1:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow SSH and ICMP
      VpcId: !Ref VPC1  # Utilisation de VPC1, mais à dupliquer pour VPC2 et VPC3
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0
        - IpProtocol: icmp
          FromPort: -1
          ToPort: -1
          CidrIp: 0.0.0.0/0
      Tags:
        - Key: Name
          Value: EC2-SG


  SecurityGroup2:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow SSH and ICMP
      VpcId: !Ref VPC2  # Utilisation de VPC1, mais à dupliquer pour VPC2 et VPC3
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0
        - IpProtocol: icmp
          FromPort: -1
          ToPort: -1
          CidrIp: 0.0.0.0/0
      Tags:
        - Key: Name
          Value: EC2-SG


  SecurityGroup3:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow SSH and ICMP
      VpcId: !Ref VPC3  # Utilisation de VPC1, mais à dupliquer pour VPC2 et VPC3
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0
        - IpProtocol: icmp
          FromPort: -1
          ToPort: -1
          CidrIp: 0.0.0.0/0
      Tags:
        - Key: Name
          Value: EC2-SG

  # Subnets VPC1
  VPC1SubnetPublic1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC1
      CidrBlock: 10.5.1.0/24
      MapPublicIpOnLaunch: true
      AvailabilityZone: eu-west-1a
      Tags:
        - Key: Name
          Value: VPC1SubnetPublic1

  VPC1SubnetPublic2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC1
      CidrBlock: 10.5.2.0/24
      MapPublicIpOnLaunch: true
      AvailabilityZone: eu-west-1b
      Tags:
        - Key: Name
          Value: VPC1SubnetPublic2

  VPC1SubnetPrivate1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC1
      CidrBlock: 10.5.3.0/24
      MapPublicIpOnLaunch: false
      AvailabilityZone: eu-west-1a
      Tags:
        - Key: Name
          Value: VPC1SubnetPrivate1

  VPC1SubnetPrivate2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC1
      CidrBlock: 10.5.4.0/24
      MapPublicIpOnLaunch: false
      AvailabilityZone: eu-west-1b
      Tags:
        - Key: Name
          Value: VPC1SubnetPrivate2


  # Subnets VPC2
  VPC2SubnetPublic1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC2
      CidrBlock: 10.6.1.0/24
      MapPublicIpOnLaunch: true
      AvailabilityZone: eu-west-1a
      Tags:
        - Key: Name
          Value: VPC2SubnetPublic1

  VPC2SubnetPublic2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC2
      CidrBlock: 10.6.2.0/24
      MapPublicIpOnLaunch: true
      AvailabilityZone: eu-west-1b
      Tags:
        - Key: Name
          Value: VPC2SubnetPublic2

  VPC2SubnetPrivate1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC2
      CidrBlock: 10.6.3.0/24
      MapPublicIpOnLaunch: false
      AvailabilityZone: eu-west-1a
      Tags:
        - Key: Name
          Value: VPC2SubnetPrivate1

  VPC2SubnetPrivate2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC2
      CidrBlock: 10.6.4.0/24
      MapPublicIpOnLaunch: false
      AvailabilityZone: eu-west-1b
      Tags:
        - Key: Name
          Value: VPC2SubnetPrivate2


  # Subnets VPC3
  VPC3SubnetPublic1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC3
      CidrBlock: 10.7.1.0/24
      MapPublicIpOnLaunch: true
      AvailabilityZone: eu-west-1a
      Tags:
        - Key: Name
          Value: VPC3SubnetPublic1

  VPC3SubnetPublic2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC3
      CidrBlock: 10.7.2.0/24
      MapPublicIpOnLaunch: true
      AvailabilityZone: eu-west-1b
      Tags:
        - Key: Name
          Value: VPC3SubnetPublic2

  VPC3SubnetPrivate1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC3
      CidrBlock: 10.7.3.0/24
      MapPublicIpOnLaunch: false
      AvailabilityZone: eu-west-1a
      Tags:
        - Key: Name
          Value: VPC3SubnetPrivate1

  VPC3SubnetPrivate2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC3
      CidrBlock: 10.7.4.0/24
      MapPublicIpOnLaunch: false
      AvailabilityZone: eu-west-1b
      Tags:
        - Key: Name
          Value: VPC3SubnetPrivate2


  # Route Tables VPC1
  VPC1RouteTablePublic1:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC1
      Tags:
        - Key: Name
          Value: VPC1RouteTablePublic1

  VPC1RoutePublic1:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref VPC1RouteTablePublic1
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway1

  VPC1RouteTableAssociationPublic1:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref VPC1SubnetPublic1
      RouteTableId: !Ref VPC1RouteTablePublic1

  # Route Tables VPC2
  VPC2RouteTablePublic1:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC2
      Tags:
        - Key: Name
          Value: VPC2RouteTablePublic1

  VPC2RoutePublic1:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref VPC2RouteTablePublic1
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway2

  VPC2RouteTableAssociationPublic1:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref VPC2SubnetPublic1
      RouteTableId: !Ref VPC2RouteTablePublic1


  # Route Tables VPC3
  VPC3RouteTablePublic1:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC3
      Tags:
        - Key: Name
          Value: VPC3RouteTablePublic1

  VPC3RoutePublic1:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref VPC3RouteTablePublic1
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway3

  VPC3RouteTableAssociationPublic1:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref VPC3SubnetPublic1
      RouteTableId: !Ref VPC3RouteTablePublic1

  # Instances EC2
  EC2Instance1:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.micro
      ImageId: !Ref AMIId
      SubnetId: !Ref VPC1SubnetPublic1
      SecurityGroupIds:
        - !Ref SecurityGroup1
      Tags:
        - Key: Name
          Value: EC2-VPC1

  EC2Instance2:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.micro
      ImageId: !Ref AMIId
      SubnetId: !Ref VPC2SubnetPublic1
      SecurityGroupIds:
        - !Ref SecurityGroup2
      Tags:
        - Key: Name
          Value: EC2-VPC2


  EC2Instance3:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.micro
      ImageId: !Ref AMIId
      SubnetId: !Ref VPC3SubnetPublic1
      SecurityGroupIds:
        - !Ref SecurityGroup3
      Tags:
        - Key: Name
          Value: EC2-VPC3

Outputs:
  VPC1Id:
    Description: ID du premier VPC
    Value: !Ref VPC1
  VPC2Id:
    Description: ID du deuxième VPC
    Value: !Ref VPC2
  VPC3Id:
    Description: ID du troisième VPC
    Value: !Ref VPC3
