AWSTemplateFormatVersion: "2010-09-09"
Description: >
  CoreNova Ubuntu 22.04 LTS Hardened GPU AMI (CUDA Ready) - Zero-Inbound SSM
  Deployment. Deploys with zero open SSH ports, IMDSv2 required, encrypted gp3
  storage, and AWS Systems Manager Session Manager access.

Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Label:
          default: Marketplace AMI & GPU Compute
        Parameters:
          - AmiId
          - InstanceType
          - RootVolumeSizeGiB
      - Label:
          default: VPC & Private Subnet
        Parameters:
          - VpcId
          - SubnetId

Parameters:
  AmiId:
    Type: String
    Default: ami-0aed9b96c79cd6bcd
    Description: >
      Subscribed AWS Marketplace AMI ID for your target region. Verify regional
      AMI ID on the AWS Marketplace launch page before launching.
  InstanceType:
    Type: String
    Default: g4dn.xlarge
    AllowedValues:
      - g4dn.xlarge
      - g4dn.2xlarge
      - g4dn.4xlarge
      - g4dn.8xlarge
      - g5.xlarge
      - g5.2xlarge
      - g5.4xlarge
      - g6.xlarge
      - g6.2xlarge
      - p3.2xlarge
    Description: NVIDIA GPU instance type (ensure G/P vCPU quota is >= 4 in this region).
  VpcId:
    Type: AWS::EC2::VPC::Id
    Description: Target VPC ID.
  SubnetId:
    Type: AWS::EC2::Subnet::Id
    Description: Target subnet ID with outbound HTTPS access to AWS SSM endpoints.
  RootVolumeSizeGiB:
    Type: Number
    Default: 60
    MinValue: 40
    MaxValue: 2000
    Description: Encrypted gp3 root volume size in GiB (minimum 40 GiB).

Resources:
  InstanceRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: ec2.amazonaws.com
            Action: sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore

  InstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties:
      Roles:
        - !Ref InstanceRole

  ZeroInboundSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Zero inbound ports for CoreNova Hardened GPU AMI (SSM access only)
      VpcId: !Ref VpcId
      SecurityGroupEgress:
        - IpProtocol: "-1"
          CidrIp: 0.0.0.0/0
          Description: Standard outbound access for SSM, NTP, and apt security updates

  GpuInstance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !Ref AmiId
      InstanceType: !Ref InstanceType
      SubnetId: !Ref SubnetId
      SecurityGroupIds:
        - !Ref ZeroInboundSecurityGroup
      IamInstanceProfile: !Ref InstanceProfile
      MetadataOptions:
        HttpTokens: required
        HttpEndpoint: enabled
        HttpPutResponseHopLimit: 1
      BlockDeviceMappings:
        - DeviceName: /dev/sda1
          Ebs:
            VolumeSize: !Ref RootVolumeSizeGiB
            VolumeType: gp3
            Encrypted: true
            DeleteOnTermination: true
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-hardened-gpu-cuda"

Outputs:
  InstanceId:
    Description: EC2 Instance ID
    Value: !Ref GpuInstance
  SsmShellCommand:
    Description: Connect via AWS SSM Session Manager (no SSH key or port 22 needed)
    Value: !Sub "aws ssm start-session --target ${GpuInstance} --region ${AWS::Region}"
  VerifyGpuCommand:
    Description: Run inside the SSM session to confirm NVIDIA driver & CUDA readiness
    Value: "nvidia-smi && nvcc --version"
