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

  Sample SAM Template for Rekognition

Globals:
  Function:
    Timeout: 900
    MemorySize: 1024
    Handler: app.lambda_handler 
    Runtime: python3.10      
    Architectures:
      - x86_64
    Tracing: Active
    # You can add LoggingConfig parameters such as the Logformat, Log Group, and SystemLogLevel or ApplicationLogLevel. Learn more here https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-loggingconfig.
    LoggingConfig:
      LogFormat: JSON

Resources:
  RekognitionStateMachine:
    Type: AWS::Serverless::StateMachine # More info about State Machine Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html
    Properties:
      DefinitionUri: statemachine/rekognition.asl.json
      DefinitionSubstitutions:
        StartModelFunctionArn: !GetAtt StartModelFunction.Arn
        AnalyzeImageFunctionArn: !GetAtt AnalyzeImageFunction.Arn
        StopModelFunctionArn: !GetAtt StopModelFunction.Arn      
      
      Policies: # Find out more about SAM policy templates: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-templates.html
      - LambdaInvokePolicy:
          FunctionName: !Ref StartModelFunction
      - LambdaInvokePolicy:
          FunctionName: !Ref AnalyzeImageFunction
      - LambdaInvokePolicy:
          FunctionName: !Ref StopModelFunction     

  StartModelFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
    Properties:
      CodeUri: functions/start_model/
      Policies:
      - AmazonRekognitionFullAccess
      - AmazonS3FullAccess
      - Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Action:
              - ssm:GetParameter
              - ssm:GetParameters              
            Resource: 
              - arn:aws:ssm:eu-west-1:587594388832:parameter/*
      # Role: arn:aws:iam::587594388832:role/Lambda-functions-role

  AnalyzeImageFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: functions/analyze_image/
      Policies:
      - AmazonRekognitionFullAccess
      - AmazonS3FullAccess
      - Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Action:
              - ssm:GetParameter
              - ssm:GetParameters              
            Resource: 
              - arn:aws:ssm:eu-west-1:587594388832:parameter/*
      # Role: arn:aws:iam::587594388832:role/Lambda-functions-role
    

  StopModelFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: functions/stop_model/
      Policies:
      - AmazonRekognitionFullAccess
      - AmazonS3FullAccess
      - Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Action:
              - ssm:GetParameter
              - ssm:GetParameters              
            Resource: 
              - arn:aws:ssm:eu-west-1:587594388832:parameter/*
      # Role: arn:aws:iam::587594388832:role/Lambda-functions-role

  # # setup a schedule event to run the state machine daily
  # DailyTriggerRule:
  #   Type: AWS::Events::Rule
  #   Properties:
  #     ScheduleExpression: 'cron(0 12 * * ? *)'  # Runs daily at 12 PM UTC
  #     Targets:
  #       - Arn: !GetAtt RekognitionStateMachine.Arn
  #         Id: "RekognitionStateMachineTarget"
  #         RoleArn: !GetAtt EventBridgeInvokeRole.Arn

  # EventBridgeInvokeRole:
  #   Type: AWS::IAM::Role
  #   Properties:
  #     AssumeRolePolicyDocument:
  #       Version: '2012-10-17'
  #       Statement:
  #         - Effect: Allow
  #           Principal:
  #             Service: events.amazonaws.com
  #           Action: sts:AssumeRole
  #     Policies:
  #       - PolicyName: InvokeStateMachinePolicy
  #         PolicyDocument:
  #           Version: '2012-10-17'
  #           Statement:
  #             - Effect: Allow
  #               Action:
  #                 - states:StartExecution
  #               Resource: !GetAtt RekognitionStateMachine.Arn

  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:
  # StockTradingStateMachineHourlyTradingSchedule is an implicit Schedule event rule created out of Events key under Serverless::StateMachine
  # Find out more about other implicit resources you can reference within SAM
  # https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-generated-resources.html
  RekognitionStateMachineArn:
    Description: Rekognition State machine ARN
    Value: !Ref RekognitionStateMachine
  RekognitionStateMachineRoleArn:
    Description: IAM Role created for Rekognition State machine based on the specified
      SAM Policy Templates
    Value: !GetAtt RekognitionStateMachineRole.Arn
# # More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
# Globals:
#   Function:
#     Tracing: Active
#     # You can add LoggingConfig parameters such as the Logformat, Log Group, and SystemLogLevel or ApplicationLogLevel. Learn more here https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-loggingconfig.
#     LoggingConfig:
#       LogFormat: JSON
#   Api:
#     TracingEnabled: true
