AWSTemplateFormatVersion: "2010-09-09"

Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
    -
      Label:
        default: "Network Configuration"
      Parameters:
        - GraphAppKitVPC
        - GraphAppKitSubnet
    -
      Label:
        default: "Server Configuration"
      Parameters:
        - GraphAppKitKeyPair

Parameters:
  GraphAppKitKeyPair:
    Type: AWS::EC2::KeyPair::KeyName
    Description: "Enter an EC2 Key Pair for this instance"
  GraphAppKitVPC:
    Type: AWS::EC2::VPC::Id
    Description: "Enter the VPC where Neptune is presently hosted. Ex: vpc-abc"
  GraphAppKitSubnet:
    Type: AWS::EC2::Subnet::Id
    Description: "Enter a public subnet within the previously selected VPC that also has access to Neptune. Ex: subnet-123"
  InstanceType:
    Type: String
    Default: 'g4dn.xlarge'
    Description: "Enter a RAPIDS.ai-compatible GPU instance type. Ex: g4dn.xlarge"
  NeptuneReaderHost:
    Type: String
    Description: "Enter the Neptune Cluster Read Endpoint URL. Ex: abc.def.ghi.neptune.amazonaws.com"

#2.36.6-11.0
#Generated with: src/bootstraps/scripts/graphistry-ami-list.sh
Mappings:
  RegionMap:
    eu-north-1:
      "HVM64": "ami-0b2592478df250046"
    ap-south-1:
      "HVM64": "ami-0bfb82d22c366410f"
    eu-west-3:
      "HVM64": "ami-097d478a5770e380e"
    eu-west-2:
      "HVM64": "ami-043e20df39a9045af"
    eu-west-1:
      "HVM64": "ami-093602dc6405d5520"
    ap-northeast-2:
      "HVM64": "ami-0cc634c9ca40a31ed"
    ap-northeast-1:
      "HVM64": "ami-09c66f9f92a9764f0"
    sa-east-1:
      "HVM64": "ami-03eb2854f7ed11992"
    ca-central-1:
      "HVM64": "ami-0a09738b9642a7c6a"
    ap-southeast-1:
      "HVM64": "ami-0815657491a4418de"
    ap-southeast-2:
      "HVM64": "ami-03c592c67b2291c2c"
    eu-central-1:
      "HVM64": "ami-0a353808c57350e8a"
    us-east-1:
      "HVM64": "ami-087af94bb9a4c396e"
    us-east-2:
      "HVM64": "ami-053efa142e054478a"
    us-west-1:
      "HVM64": "ami-012c1c8f9dbb1a2c6"
    us-west-2:
      "HVM64": "ami-0036548682eb8b53d"
Resources:
  GraphAppKitSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Graphistry Graph App Kit Access 8501 22
      Tags: 
        - Key: "name"
          Value: "graph-app-kit-quicklaunch-a"
        - Key: "kind"
          Value: "graph-app-kit"
      VpcId: 
        Ref: GraphAppKitVPC
      SecurityGroupIngress:
      - IpProtocol: tcp
        FromPort: 8501
        ToPort: 8501
        CidrIp: 0.0.0.0/0
      - IpProtocol: tcp
        FromPort: 22
        ToPort: 22
        CidrIp: 0.0.0.0/0
      - IpProtocol: tcp
        FromPort: 80
        ToPort: 80
        CidrIp: 0.0.0.0/0
      - IpProtocol: tcp
        FromPort: 443
        ToPort: 443
        CidrIp: 0.0.0.0/0
  GraphAppKitEC2:
    Type: AWS::EC2::Instance
    Properties:
      Tags: 
        - Key: "kind"
          Value: "graph-app-kit-full"
        - Key: Name
          Value: Graphistry-Graph-App-Kit
      ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", HVM64]
      InstanceType: !Ref InstanceType
      SubnetId:
        Ref: GraphAppKitSubnet
      SecurityGroupIds:
      - 
        Ref: GraphAppKitSecurityGroup
      KeyName: 
        Ref: GraphAppKitKeyPair
      BlockDeviceMappings:
        - DeviceName: /dev/xvda
          Ebs:
            VolumeType: gp2
            VolumeSize: '60'
            DeleteOnTermination: 'true'
            Encrypted: 'false'
      UserData:
        Fn::Base64:
          Fn::Join:
          - ''
          - - "#!/bin/bash\n"
            - "set -ex\n"
            - "sudo usermod -a -G docker ubuntu\n"
            - "echo '===== System check'\n"
            - "nvidia-smi\n"
            - "echo '===== Downloading graph-app-kit'\n"
            - "cd /home/ubuntu\n"
            - "mkdir -p graph-app-kit/public\n"
            - "cd graph-app-kit/public\n"
            - "git clone https://github.com/graphistry/graph-app-kit.git\n"
            - "cp -r /home/ubuntu/graph-app-kit/public /home/ubuntu/graph-app-kit/private\n"
            - "echo '===== Running graph-app-kit bootstraps'\n"
            - "cd /home/ubuntu/graph-app-kit/public/graph-app-kit/src/bootstraps/neptune\n"
            - "./graphistry.sh \""
            - Ref: NeptuneReaderHost
            - "\"\n"
            - "/opt/aws/bin/cfn-signal -e $? --stack "
            - Ref: AWS::StackName
            - " --resource GraphAppKitEC2 --region "
            - Ref: AWS::Region
            - "\n"
    CreationPolicy:
      ResourceSignal:
        Count: 1
        Timeout: "PT20M"

Outputs:
  PublicIp:
    Description: GraphAppKitEC2 Public IP
    Value: !GetAtt GraphAppKitEC2.PublicIp
    Export:
      Name: !Sub "${AWS::StackName}-PublicIp"