AWSTemplateFormatVersion: '2010-09-09'

Description: >
  Workshop 66 - Lambda, API Gateway, SAM Workspace
  Template to setup the EC2 environment for VS Code Remote SSH.

Parameters:
  VpcCidrBlock:
    Description: The CIDR block range for your VPC
    Type: String
    Default: 10.43.0.0/28
  KeyName:
    Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
    Type: AWS::EC2::KeyPair::KeyName

Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref VpcCidrBlock
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-VPC"

  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-InternetGateway"

  AttachGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref 'VPC'
      InternetGatewayId: !Ref 'InternetGateway'

  RouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref 'VPC'
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-RouteTable"

  Route:
    Type: AWS::EC2::Route
    DependsOn: AttachGateway
    Properties:
      RouteTableId: !Ref 'RouteTable'
      DestinationCidrBlock: '0.0.0.0/0'
      GatewayId: !Ref 'InternetGateway'

  PublicSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref 'VPC'
      CidrBlock: !Ref VpcCidrBlock
      MapPublicIpOnLaunch: true
      AvailabilityZone: !Select
        - '0'
        - !GetAZs ''
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-PublicSubnet1"

  PublicSubnet1RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicSubnet1
      RouteTableId: !Ref RouteTable

  InstanceSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow SSH access
      VpcId: !Ref VPC
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0

  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/AdministratorAccess

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

  WorkspaceInstance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t3.small
      ImageId: resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64
      KeyName: !Ref KeyName
      SubnetId: !Ref PublicSubnet1
      SecurityGroupIds:
        - !Ref InstanceSecurityGroup
      IamInstanceProfile: !Ref InstanceProfile
      Tags:
        - Key: Name
          Value: !Sub "Workshop66-Workspace-${AWS::StackName}"
      UserData: 
        Fn::Base64: !Sub |
          #!/bin/bash
          # Update and install dependencies
          dnf update -y
          dnf install -y git docker python3 python3-pip util-linux jq
          
          # Start docker
          systemctl enable docker
          systemctl start docker
          usermod -aG docker ec2-user
          
          # Set default AWS region for ec2-user
          sudo -u ec2-user aws configure set region ${AWS::Region}
          
          # Install AWS SAM CLI
          curl -Lo /tmp/sam-cli.zip https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip
          unzip /tmp/sam-cli.zip -d /tmp/sam-installation
          sudo /tmp/sam-installation/install
          
          # Create sampleapp directory
          mkdir -p /home/ec2-user/sampleapp
          chown ec2-user:ec2-user /home/ec2-user/sampleapp

Outputs:
  SSHCommand:
    Description: Command to SSH into the EC2 Instance
    Value: !Sub "ssh -i <YOUR_KEY.pem> ec2-user@${WorkspaceInstance.PublicIp}"
  VSCodeRemoteSSH:
    Description: Hostname for VS Code Remote SSH
    Value: !GetAtt WorkspaceInstance.PublicIp
