AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
  {{ cookiecutter.project_name }}

  Sample SAM Template for {{ cookiecutter.project_name }}

Resources:
  SfnToStockCheckerFunctionConnector:
    Type: AWS::Serverless::Connector # More info about Connector Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-connector.html
    Properties:
      Source:
        Id: StockTradingStateMachine
      Destination:
        Id: StockCheckerFunction
      Permissions:
        - Write

  SfnToStockBuyerFunctionConnector:
    Type: AWS::Serverless::Connector
    Properties:
      Source:
        Id: StockTradingStateMachine
      Destination:
        Id: StockBuyerFunction
      Permissions:
        - Write

  SfnToStockSellerFunctionConnector:
    Type: AWS::Serverless::Connector
    Properties:
      Source:
        Id: StockTradingStateMachine
      Destination:
        Id: StockSellerFunction
      Permissions:
        - Write

  SfnToTransactionTableConnector:
    Type: AWS::Serverless::Connector
    Properties:
      Source:
        Id: StockTradingStateMachine
      Destination:
        Id: TransactionTable
      Permissions:
        - Write

  StockTradingStateMachine:
    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/stock_trader.asl.json
      DefinitionSubstitutions:
        StockCheckerFunctionArn: !GetAtt StockCheckerFunction.Arn
        StockSellerFunctionArn: !GetAtt StockSellerFunction.Arn
        StockBuyerFunctionArn: !GetAtt StockBuyerFunction.Arn
        DDBPutItem: !Sub arn:${AWS::Partition}:states:::dynamodb:putItem
        DDBTable: !Ref TransactionTable
      Events:
        HourlyTradingSchedule:
          Type: Schedule # More info about Schedule Event Source: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-statemachine-schedule.html
          Properties:
            Description: Schedule to run the stock trading state machine every hour
            Enabled: False # This schedule is disabled by default to avoid incurring charges.
            Schedule: "rate(1 hour)"
      Policies:
        - CloudWatchPutMetricPolicy: {}

  StockCheckerFunction:
    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/stock_checker/
      Handler: app.lambda_handler
      Runtime: python3.9

  StockSellerFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: functions/stock_seller/
      Handler: app.lambda_handler
      Runtime: python3.9

  StockBuyerFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: functions/stock_buyer/
      Handler: app.lambda_handler
      Runtime: python3.9

  TransactionTable:
    Type: AWS::Serverless::SimpleTable # More info about SimpleTable Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-simpletable.html
    Properties:
      PrimaryKey:
        Name: Id
        Type: String
      ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1

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
  StockTradingStateMachineArn:
    Description: "Stock Trading State machine ARN"
    Value: !Ref StockTradingStateMachine
  StockTradingStateMachineRoleArn:
    Description: "IAM Role created for Stock Trading State machine based on the specified SAM Policy Templates"
    Value: !GetAtt StockTradingStateMachineRole.Arn
