AWSTemplateFormatVersion: '2010-09-09'
Description: Basic Minecraft Server Setup on EC2 with S3 Bucket for Mods

Parameters:
  KeyName:
    Type: AWS::EC2::KeyPair::KeyName
    Description: EC2 KeyPair to enable SSH access

Resources:

  MinecraftSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow Minecraft and SSH
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 25565
          ToPort: 25565
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0

  MinecraftS3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub "minecraft-mod-${AWS::AccountId}-${AWS::Region}"

  MinecraftS3AccessRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: ec2.amazonaws.com
            Action: sts:AssumeRole
      Policies:
        - PolicyName: S3AccessPolicy
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - s3:GetObject
                  - s3:ListBucket
                Resource:
                  - !GetAtt MinecraftS3Bucket.Arn
                  - !Sub "${MinecraftS3Bucket.Arn}/*"

  MinecraftInstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties:
      Roles:
        - !Ref MinecraftS3AccessRole

  MinecraftInstance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: c5a.xlarge
      KeyName: !Ref KeyName
      SecurityGroups:
        - !Ref MinecraftSecurityGroup
      IamInstanceProfile: !Ref MinecraftInstanceProfile
      ImageId: ami-084568db4383264d4 
      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash
          # Install Java 21 (Azul Zulu)
          wget -qO - https://repos.azul.com/azul-repo.key | gpg --dearmor -o /usr/share/keyrings/azul.gpg
          echo "deb [signed-by=/usr/share/keyrings/azul.gpg] https://repos.azul.com/zulu/deb stable main" > /etc/apt/sources.list.d/zulu.list
          apt-get update
          apt-get install -y zulu21-jdk unzip curl

          # Install AWS CLI v2
          curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
          unzip awscliv2.zip
          ./aws/install


          # Create mc folder
          mkdir -p /opt/minecraft/

          # Download Forge installer (you can automate running it later)
          wget https://maven.minecraftforge.net/net/minecraftforge/forge/1.20.1-47.4.12/forge-1.20.1-47.4.12-installer.jar -O /opt/minecraft/forge-installer.jar

          # Run Forge installer
          cd /opt/minecraft
          java -jar forge-installer.jar --installServer
          
          





Outputs:
  InstancePublicIP:
    Description: Public IP of Minecraft server
    Value: !GetAtt MinecraftInstance.PublicIp

  MinecraftModsBucket:
    Description: S3 bucket for storing Minecraft mods
    Value: !Ref MinecraftS3Bucket
