AWSTemplateFormatVersion: "2010-09-09"
Description: The AWS CloudFormation template for this Serverless application
Parameters:
  MyResourcePrefix:
    Type: String
    Default: sample
    Description: "Enter your project prefix. e.g: my-company"
  MySerial:
    Type: String
    Default: the-serial
    Description: "Enter your or trial serial code."
Resources:
  LightfileLogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub "/aws/lambda/${MyResourcePrefix}-lightfile"
  IamRoleForLambda:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Sub "${MyResourcePrefix}-lightfile-role"
      Path: /
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - sts:AssumeRole
      Policies:
        - PolicyName: !Sub "${MyResourcePrefix}-lightfile-log-policy"
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogStream
                Resource:
                  - Fn::Sub: arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${MyResourcePrefix}-lightfile:*
              - Effect: Allow
                Action:
                  - logs:PutLogEvents
                Resource:
                  - Fn::Sub: arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${MyResourcePrefix}-lightfile:*:*
        - PolicyName: !Sub "${MyResourcePrefix}-lightfile-s3-policy"
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - s3:GetObject
                  - s3:GetObjectAcl
                  - s3:GetObjectTagging
                  - s3:PutObject
                  - s3:PutObjectAcl
                  - s3:PutObjectTagging
                Resource:
                  - !Sub "arn:aws:s3:::${MyResourcePrefix}-lightfile-test/*"
                  - !Sub "arn:aws:s3:::${MyResourcePrefix}-lightfile-backup/*"
  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket: ideamans-lightfile-lambda-us-east-1
        S3Key: lambda/4.3.0/lightfile-lambda.zip
      FunctionName: !Sub "${MyResourcePrefix}-lightfile"
      Handler: lambda.handler
      MemorySize: 512
      Role:
        Fn::GetAtt:
          - IamRoleForLambda
          - Arn
      Runtime: nodejs20.x
      Timeout: 30
      Environment:
        Variables:
          SERIAL: !Ref "MySerial"
          QUALITY: medium
          BACKUP_BUCKET: !Sub "${MyResourcePrefix}-lightfile-backup"
          BACKUP_PREFIX: "${bucket}"
    DependsOn:
      - LightfileLogGroup
      - IamRoleForLambda
  LambdaPermissionForTestBucket:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName:
        Fn::GetAtt:
          - LambdaFunction
          - Arn
      Action: lambda:InvokeFunction
      Principal: !Sub "s3.${AWS::URLSuffix}"
      SourceArn: !Sub "arn:${AWS::Partition}:s3:::${MyResourcePrefix}-lightfile-test"
    DependsOn:
      - LambdaFunction
  LambdaEventTriggerForTestBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub "${MyResourcePrefix}-lightfile-test"
      OwnershipControls:
        Rules:
          - ObjectOwnership: ObjectWriter
      PublicAccessBlockConfiguration:
        BlockPublicAcls: False
        BlockPublicPolicy: False
        IgnorePublicAcls: False
        RestrictPublicBuckets: False
      NotificationConfiguration:
        LambdaConfigurations:
          - Event: "s3:ObjectCreated:*"
            Function:
              Fn::GetAtt:
                - LambdaFunction
                - Arn
    DependsOn:
      - LambdaPermissionForTestBucket
  LambdaEventTriggerForBackupBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub "${MyResourcePrefix}-lightfile-backup"
      OwnershipControls:
        Rules:
          - ObjectOwnership: ObjectWriter
    DependsOn:
      - LambdaFunction
