AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: SAM Template for serverless Workflow

Parameters:
    Environment: 
      Type: String
    VPCSecurityGroupID:
      Type: String
    VPCSubnetID1:
      Type: String
    VPCSubnetID2:
      Type: String
    VPCSubnetID3:
      Type: String
    DatabaseHost:
      Type: String
    DatabaseUser:
      Type: String
    DatabasePassword:
      Type: String
    DatabaseName:
      Type: String
    SlackURL:
      Type: String

Globals:
  Function:
    Handler: app.lambda_handler
    Runtime: python3.8
    Timeout: 121
    MemorySize: 256
    Environment:
      Variables:
        host: !Ref DatabaseHost
        user: !Ref DatabaseUser
        password: !Ref DatabasePassword
        database: !Ref DatabaseName
        sqs_url: !Ref sqs
        slack_url: !Ref SlackURL
    VpcConfig:
      SubnetIds:
        - !Ref VPCSubnetID1
        - !Ref VPCSubnetID2
        - !Ref VPCSubnetID3
      SecurityGroupIds:
        - !Ref VPCSecurityGroupID

Resources:
  receiverlambda:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub receiver-${Environment}
      CodeUri: receiver/
      Events:
        S3Event:
          Type: S3
          Properties:
            Bucket: !Ref sftpreceivers3bucket
            Events: 's3:ObjectCreated:Put'
      Policies:
      - Statement:
        - Effect: Allow
          Action:
          - s3:ListAllMyBuckets
          Resource: '*'
        - Effect: Allow
          Action:
          - s3:ListBucket
          - s3:GetBucketLocation
          Resource: !Sub 'arn:aws:s3:::receiver-bucket'
        - Effect: Allow
          Action:
          - s3:PutObject
          - s3:PutObjectAcl
          - s3:GetObject
          - s3:GetObjectAcl
          Resource: !Sub 'arn:aws:s3:::receiver-bucket/folder/*'
      - Statement:
        - Effect: Allow
          Action:
          - sqs:SendMessage
          Resource: !Sub 'arn:aws:sqs:${AWS::Region}:${AWS::AccountId}:sqs-${Environment}'
    
    sftpreceivers3bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub receiverbucket-${Environment}
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true
      BucketEncryption:
        ServerSideEncryptionConfiguration:
        - ServerSideEncryptionByDefault:
            SSEAlgorithm: AES256

  s3tolambdapermission:
    Type: AWS::Lambda::Permission
    Properties:
      Action: 'lambda:InvokeFunction'
      FunctionName: !Ref receiverlambda
      Principal: s3.amazonaws.com
      SourceArn: !GetAtt sftpreceivers3bucket.Arn
      
  processorlambda:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub processor-${Environment}
      MemorySize: 1024
      CodeUri: processor/
      Events:
        SQSEvent:
          Type: SQS
          Properties:
            Queue: !GetAtt sqs.Arn
            BatchSize: 10
            Enabled: true
  sqs:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Sub sqs-${Environment}
      VisibilityTimeout: 180
      KmsMasterKeyId: alias/aws/sqs

  reprocessorlambda:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub reprocessor-${Environment}
      CodeUri: reprocessor/
      Policies:
      - Statement:
        - Effect: Allow
          Action:
          - sqs:SendMessage
          Resource: !Sub 'arn:aws:sqs:${AWS::Region}:${AWS::AccountId}:${Environment}'
      Events:
        SQSEvent:
          Type: SQS
          Properties:
            Queue: !GetAtt reprocessorsqs.Arn
            BatchSize: 10
            Enabled: true
  reprocessorsqs:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Sub reprocessor-sqs-${Environment}
      VisibilityTimeout: 180
      KmsMasterKeyId: alias/aws/sqs
