{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "{\"createdOn\":\"Mac\",\"createdBy\":\"Amplify\",\"createdWith\":\"7.6.26\",\"stackType\":\"function-Lambda\",\"metadata\":{}}",
  "Mappings": {
    "EnvironmentMap": {
      "AUTH0SIZZLDOMAIN": {
        "dev": "https://sizzl.us.auth0.com",
        "stage": "https://sizzl.us.auth0.com",
        "prod": "https://sizzl-prod.us.auth0.com"
      },
      "AUTH0CLIENTID": {
        "dev": "5Q6yL9nHqyllwTcLR65Z6qWvE1FKxYwV",
        "stage": "5Q6yL9nHqyllwTcLR65Z6qWvE1FKxYwV",
        "prod": "62lePubfHMpfh7WfvWuoJ4U4XCDAeNTG"
      },
      "AUTH0CLIENTSECRET": {
        "dev": "KzCPlNyH7QhWixBWGLh4WN-Xjzq9WQTfVmjK6TDoQYYoMQNjXcydiBaWuCkE7e5m",
        "stage": "KzCPlNyH7QhWixBWGLh4WN-Xjzq9WQTfVmjK6TDoQYYoMQNjXcydiBaWuCkE7e5m",
        "prod": "nlzgBa0lp1PkhkbNJQfOUL9yMLZw1MNy0--CEbHH2QaCaAtSe66ir0wZcpbYzFtS"
      },
      "CUSTOMERIOAPPKEY": {
        "dev": "90be910260473052b538afbd2d34915d",
        "stage": "90be910260473052b538afbd2d34915d",
        "prod": "435a5ce51e1a44b3a2a1c42724c65023"
      },
      "CUSTOMERIOSITEID": {
        "dev": "16eb0e3ede2332e90812",
        "stage": "16eb0e3ede2332e90812",
        "prod": "1c0f143c0d9209ec064c"
      },
      "CUSTOMERIOAPIKEY": {
        "dev": "516c2ed6f00f09b951f7",
        "stage": "516c2ed6f00f09b951f7",
        "prod": "cf998fc738b151682420"
      },
      "SourceMediaBucket": {
        "dev": "media-storage.dev.sizzl.com",
        "prod": "media-storage.prod.sizzl.com",
        "stage": "media-storage.stage.sizzl.com"
      },
      "MEDIALIVECLOUDFRONT": {
        "dev": "https://d3eu01ol3fe0ae.cloudfront.net",
        "prod": "https://d2nwe8nupc84ly.cloudfront.net",
        "stage": "https://d3c1c43o487j00.cloudfront.net"
      }
    }
  },
  "Parameters": {
    "apiGraphQLAPIIdOutput": {
      "Type": "String"
    },
    "apiGraphQLAPIEndpointOutput": {
      "Type": "String"
    },
    "apiGraphQLAPIKeyOutput": {
      "Type": "String"
    },
    "CloudWatchRule": {
      "Type": "String",
      "Default": "NONE",
      "Description": " Schedule Expression"
    },
    "deploymentBucketName": {
      "Type": "String"
    },
    "env": {
      "Type": "String"
    },
    "s3Key": {
      "Type": "String"
    }
  },
  "Conditions": {
    "ShouldNotCreateEnvResources": {
      "Fn::Equals": [
        {
          "Ref": "env"
        },
        "NONE"
      ]
    }
  },
  "Resources": {
    "LambdaFunction": {
      "Type": "AWS::Lambda::Function",
      "Metadata": {
        "aws:asset:path": "./src",
        "aws:asset:property": "Code"
      },
      "Properties": {
        "Code": {
          "S3Bucket": {
            "Ref": "deploymentBucketName"
          },
          "S3Key": {
            "Ref": "s3Key"
          }
        },
        "Handler": "index.handler",
        "FunctionName": {
          "Fn::If": [
            "ShouldNotCreateEnvResources",
            "putChannel",
            {
              "Fn::Join": [
                "",
                [
                  "putChannel",
                  "-",
                  {
                    "Ref": "env"
                  }
                ]
              ]
            }
          ]
        },
        "Environment": {
          "Variables": {
            "ENV": {
              "Ref": "env"
            },
            "REGION": {
              "Ref": "AWS::Region"
            },
            "S3Bucket": {
              "Fn::Sub": [
                "${SourceMediaBucket}-${account}",
                {
                  "account": {
                    "Ref": "AWS::AccountId"
                  },
                  "SourceMediaBucket": {
                    "Fn::FindInMap": [
                      "EnvironmentMap",
                      "SourceMediaBucket",
                      {
                        "Ref": "env"
                      }
                    ]
                  }
                }
              ]
            },
            "USERTABLE": {
              "Fn::ImportValue": {
                "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:UserTable:Name"
              }
            },
            "CHANNELTABLE": {
              "Fn::ImportValue": {
                "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:ChannelTable:Name"
              }
            },
            "VIDEOTABLE": {
              "Fn::ImportValue": {
                "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:VideoTable:Name"
              }
            },
            "GRAPHQLAPIENDPOINTOUTPUT": {
              "Ref": "apiGraphQLAPIEndpointOutput"
            },
            "GRAPHQLAPIKEYOUTPUT": {
              "Ref": "apiGraphQLAPIKeyOutput"
            },
            "AWS_NODEJS_CONNECTION_REUSE_ENABLED": 1,
            "AUTH0SIZZLDOMAIN": {
              "Fn::FindInMap": [
                "EnvironmentMap",
                "AUTH0SIZZLDOMAIN",
                {
                  "Ref": "env"
                }
              ]
            },
            "AUTH0CLIENTID": {
              "Fn::FindInMap": [
                "EnvironmentMap",
                "AUTH0CLIENTID",
                {
                  "Ref": "env"
                }
              ]
            },
            "AUTH0CLIENTSECRET": {
              "Fn::FindInMap": [
                "EnvironmentMap",
                "AUTH0CLIENTSECRET",
                {
                  "Ref": "env"
                }
              ]
            },
            "CUSTOMERIOAPPKEY": {
              "Fn::FindInMap": [
                "EnvironmentMap",
                "CUSTOMERIOAPPKEY",
                {
                  "Ref": "env"
                }
              ]
            },
            "CUSTOMERIOAPIKEY": {
              "Fn::FindInMap": [
                "EnvironmentMap",
                "CUSTOMERIOAPIKEY",
                {
                  "Ref": "env"
                }
              ]
            },
            "CUSTOMERIOSITEID": {
              "Fn::FindInMap": [
                "EnvironmentMap",
                "CUSTOMERIOSITEID",
                {
                  "Ref": "env"
                }
              ]
            },
            "SourceMediaBucket": {
              "Fn::FindInMap": [
                "EnvironmentMap",
                "SourceMediaBucket",
                {
                  "Ref": "env"
                }
              ]
            },
            "MEDIALIVECLOUDFRONT": {
              "Fn::FindInMap": [
                "EnvironmentMap",
                "MEDIALIVECLOUDFRONT",
                {
                  "Ref": "env"
                }
              ]
            }
          }
        },
        "Role": {
          "Fn::GetAtt": [
            "LambdaExecutionRole",
            "Arn"
          ]
        },
        "Runtime": "nodejs14.x",
        "Layers": [],
        "Timeout": 25
      }
    },
    "LambdaExecutionRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "RoleName": {
          "Fn::If": [
            "ShouldNotCreateEnvResources",
            "amplifiedchatappLambdaRole0a5d27f4",
            {
              "Fn::Join": [
                "",
                [
                  "amplifiedchatappLambdaRole0a5d27f4",
                  "-",
                  {
                    "Ref": "env"
                  }
                ]
              ]
            }
          ]
        },
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "lambda.amazonaws.com"
                ]
              },
              "Action": [
                "sts:AssumeRole"
              ]
            }
          ]
        }
      }
    },
    "lambdaexecutionpolicy": {
      "DependsOn": [
        "LambdaExecutionRole"
      ],
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyName": "lambda-execution-policy",
        "Roles": [
          {
            "Ref": "LambdaExecutionRole"
          }
        ],
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
              ],
              "Resource": {
                "Fn::Sub": [
                  "arn:aws:logs:${region}:${account}:log-group:/aws/lambda/${lambda}:log-stream:*",
                  {
                    "region": {
                      "Ref": "AWS::Region"
                    },
                    "account": {
                      "Ref": "AWS::AccountId"
                    },
                    "lambda": {
                      "Ref": "LambdaFunction"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    },
    "AccessDynamoDBApiResourcesPolicy": {
      "DependsOn": [
        "LambdaExecutionRole"
      ],
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyName": "amplify-lambda-execution-policy",
        "Roles": [
          {
            "Ref": "LambdaExecutionRole"
          }
        ],
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Action": [
                "dynamodb:BatchWriteItem",
                "dynamodb:BatchGet*",
                "dynamodb:DescribeStream",
                "dynamodb:DescribeTable",
                "dynamodb:Get*",
                "dynamodb:Query",
                "dynamodb:Scan",
                "dynamodb:BatchWrite*",
                "dynamodb:CreateTable",
                "dynamodb:Delete*",
                "dynamodb:Update*",
                "dynamodb:PutItem"
              ],
              "Resource": [
                {
                  "Fn::Sub": [
                    "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${tablename}",
                    {
                      "tablename": {
                        "Fn::ImportValue": {
                          "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:UserTable:Name"
                        }
                      }
                    }
                  ]
                },
                {
                  "Fn::Sub": [
                    "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${tablename}/index/*",
                    {
                      "tablename": {
                        "Fn::ImportValue": {
                          "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:UserTable:Name"
                        }
                      }
                    }
                  ]
                },
                {
                  "Fn::Sub": [
                    "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${tablename}",
                    {
                      "tablename": {
                        "Fn::ImportValue": {
                          "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:ChannelTable:Name"
                        }
                      }
                    }
                  ]
                },
                {
                  "Fn::Sub": [
                    "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${tablename}",
                    {
                      "tablename": {
                        "Fn::ImportValue": {
                          "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:VideoTable:Name"
                        }
                      }
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    },
    "S3PresignedURLPolicy": {
      "DependsOn": [
        "LambdaExecutionRole"
      ],
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyName": {
          "Fn::Sub": [
            "S3PresignedURLPolicy-${env}",
            {
              "env": {
                "Ref": "env"
              }
            }
          ]
        },
        "Roles": [
          {
            "Ref": "LambdaExecutionRole"
          }
        ],
        "PolicyDocument": {
          "Statement": [
            {
              "Effect": "Allow",
              "Action": [
                "s3:GetObject",
                "s3:PutObject"
              ],
              "Resource": [
                {
                  "Fn::Sub": [
                    "arn:aws:s3:::${SourceMediaBucket}-${account}/*",
                    {
                      "account": {
                        "Ref": "AWS::AccountId"
                      },
                      "SourceMediaBucket": {
                        "Fn::FindInMap": [
                          "EnvironmentMap",
                          "SourceMediaBucket",
                          {
                            "Ref": "env"
                          }
                        ]
                      }
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    }
  },
  "Outputs": {
    "Name": {
      "Value": {
        "Ref": "LambdaFunction"
      }
    },
    "Arn": {
      "Value": {
        "Fn::GetAtt": [
          "LambdaFunction",
          "Arn"
        ]
      }
    },
    "Region": {
      "Value": {
        "Ref": "AWS::Region"
      }
    },
    "LambdaExecutionRole": {
      "Value": {
        "Ref": "LambdaExecutionRole"
      }
    },
    "LambdaExecutionRoleArn": {
      "Value": {
        "Fn::GetAtt": [
          "LambdaExecutionRole",
          "Arn"
        ]
      }
    }
  }
}