AWSTemplateFormatVersion: "2010-09-09"
Description: >-
  cloud path, curso CLF-C02, lab 5. Una VPC con una subred publica y una
  instancia EC2 t3.micro con un servidor web. Gasta credito del Free Tier: la
  instancia y su direccion IPv4 publica (0.005 USD por hora) cobran mientras
  existan. Borra la pila al terminar el lab.

Parameters:
  TipoDeInstancia:
    Type: String
    Description: Tipos marcados como Free tier eligible para cuentas creadas desde el 15 de julio de 2025 (arquitectura x86).
    AllowedValues: [t3.micro, t3.small]
    Default: t3.micro
  ImagenAmazonLinux:
    Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
    Description: Parametro publico de AWS con la AMI mas reciente de Amazon Linux 2023.
    Default: /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64

Resources:
  Vpc:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.50.0.0/16
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: cloudpath-lab05
        - Key: cloudpath:lab
          Value: lab05

  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: cloudpath:lab
          Value: lab05

  ConexionGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref Vpc
      InternetGatewayId: !Ref InternetGateway

  SubredPublica:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref Vpc
      CidrBlock: 10.50.1.0/24
      AvailabilityZone: !Select [0, !GetAZs ""]
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: cloudpath-lab05-publica
        - Key: cloudpath:lab
          Value: lab05

  TablaDeRutas:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref Vpc
      Tags:
        - Key: cloudpath:lab
          Value: lab05

  RutaAInternet:
    Type: AWS::EC2::Route
    DependsOn: ConexionGateway
    Properties:
      RouteTableId: !Ref TablaDeRutas
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway

  AsociacionSubred:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref SubredPublica
      RouteTableId: !Ref TablaDeRutas

  GrupoWeb:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Lab 5 de cloud path. Solo HTTP desde internet; sin SSH.
      VpcId: !Ref Vpc
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          CidrIp: 0.0.0.0/0
          Description: HTTP
      Tags:
        - Key: cloudpath:lab
          Value: lab05

  ServidorWeb:
    Type: AWS::EC2::Instance
    DependsOn: RutaAInternet
    Properties:
      InstanceType: !Ref TipoDeInstancia
      ImageId: !Ref ImagenAmazonLinux
      SubnetId: !Ref SubredPublica
      SecurityGroupIds:
        - !Ref GrupoWeb
      MetadataOptions:
        HttpTokens: required
      UserData:
        Fn::Base64: |
          #!/bin/bash
          dnf install -y httpd
          TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 60")
          ZONA=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/availability-zone)
          echo "<h1>cloud path, lab 5</h1><p>Esta instancia corre en la zona $ZONA</p>" > /var/www/html/index.html
          systemctl enable --now httpd
      Tags:
        - Key: Name
          Value: cloudpath-lab05-web
        - Key: cloudpath:lab
          Value: lab05

Outputs:
  UrlDelServidor:
    Description: Abre esta direccion en el navegador (puede tardar un par de minutos).
    Value: !Sub "http://${ServidorWeb.PublicDnsName}"
  ZonaDeDisponibilidad:
    Value: !GetAtt ServidorWeb.AvailabilityZone
