AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  LightsailMetricsEmail

  Sample SAM Template for Lightsail Metrics Email

Globals:
  Function:
    Timeout: 300
    MemorySize: 512
    LoggingConfig:
      LogFormat: JSON

Resources:
  EmailSendFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: EmailSendFunction/
      Handler: app.lambda_handler
      Runtime: python3.12
      Environment:
        Variables:
          # CHANGE_ME
          email_from: 'software-alerts@overdrive.co.za'
          email_to: 'software-alerts@overdrive.co.za' 
          email_subject: 'Lightsail Metrics Report'
          dashboard_name: 'Lightsail_Metrics'
      Architectures:
      - x86_64
      # CHANGE_ME
      Policies:
        - Statement:
          - Effect: Allow
            Action:
              - ses:SendRawEmail
            Resource: '*'
          - Effect: Allow
            Action:
              - lightsail:GetInstances
              - lightsail:GetInstanceMetricData
            Resource: '*'
      Events:
        MorningSchedule:
          Type: Schedule
          Properties:
            Schedule: cron(0 8 ? * * *)
        SendEmail:
          Type: Api
          Properties:
            Path: /hello
            Method: get

  ApplicationResourceGroup:
    Type: AWS::ResourceGroups::Group
    Properties:
      Name:
        Fn::Sub: ApplicationInsights-SAM-${AWS::StackName}
      ResourceQuery:
        Type: CLOUDFORMATION_STACK_1_0

  ApplicationInsightsMonitoring:
    Type: AWS::ApplicationInsights::Application
    Properties:
      ResourceGroupName:
        Ref: ApplicationResourceGroup
      AutoConfigurationEnabled: 'true'

Outputs:
  SendEmailApi:
    Description: API Gateway endpoint URL for Prod stage for Hello World function
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  EmailSendFunction:
    Description: Lightsail Metrics Email Lambda Function ARN
    Value: !GetAtt EmailSendFunction.Arn