{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "{\"createdOn\":\"Mac\",\"createdBy\":\"Amplify\",\"createdWith\":\"7.6.26\",\"stackType\":\"function-Lambda\",\"metadata\":{}}",
  "Mappings": {
    "EnvironmentMap": {
      "MEDIAINGESTSERVER": {
        "dev": "rtmp://media-server-lb-dev-8305456172caa7ab.elb.us-east-1.amazonaws.com:1935/live",
        "prod": "rtmp://media-server-lb-prod-c17a510a165fee0a.elb.us-east-1.amazonaws.com:1935/live",
        "stage": "rtmp://ec2-34-228-198-148.compute-1.amazonaws.com:1935/live"
      },
      "MEDIALIVECLOUDFRONT": {
        "dev": "https://d3eu01ol3fe0ae.cloudfront.net",
        "prod": "https://d2nwe8nupc84ly.cloudfront.net",
        "stage": "https://d3c1c43o487j00.cloudfront.net"
      }
    }
  },
  "Parameters": {
    "CloudWatchRule": {
      "Type": "String",
      "Default": "NONE",
      "Description": " Schedule Expression"
    },
    "deploymentBucketName": {
      "Type": "String"
    },
    "env": {
      "Type": "String"
    },
    "s3Key": {
      "Type": "String"
    },
    "apiamplifiedchatappGraphQLAPIIdOutput": {
      "Type": "String",
      "Default": "apiamplifiedchatappGraphQLAPIIdOutput"
    },
    "apiamplifiedchatappGraphQLAPIEndpointOutput": {
      "Type": "String",
      "Default": "apiamplifiedchatappGraphQLAPIEndpointOutput"
    },
    "apiamplifiedchatappGraphQLAPIKeyOutput": {
      "Type": "String",
      "Default": "apiamplifiedchatappGraphQLAPIKeyOutput"
    },
    "apiGraphQLAPIIdOutput": {
      "Type": "String"
    },
    "apiGraphQLAPIEndpointOutput": {
      "Type": "String"
    },
    "apiGraphQLAPIKeyOutput": {
      "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",
            "channelPoolService",
            {
              "Fn::Join": [
                "",
                [
                  "channelPoolService",
                  "-",
                  {
                    "Ref": "env"
                  }
                ]
              ]
            }
          ]
        },
        "Environment": {
          "Variables": {
            "ENV": {
              "Ref": "env"
            },
            "REGION": {
              "Ref": "AWS::Region"
            },
            "API_AMPLIFIEDCHATAPP_GRAPHQLAPIIDOUTPUT": {
              "Ref": "apiamplifiedchatappGraphQLAPIIdOutput"
            },
            "API_AMPLIFIEDCHATAPP_GRAPHQLAPIENDPOINTOUTPUT": {
              "Ref": "apiamplifiedchatappGraphQLAPIEndpointOutput"
            },
            "API_AMPLIFIEDCHATAPP_GRAPHQLAPIKEYOUTPUT": {
              "Ref": "apiamplifiedchatappGraphQLAPIKeyOutput"
            },
            "AWSCHANNELPOOLTABLE": {
              "Fn::ImportValue": {
                "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:AWSChannelPoolTable: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"
            },
            "MEDIAINGESTSERVER": {
              "Fn::FindInMap": [
                "EnvironmentMap",
                "MEDIAINGESTSERVER",
                {
                  "Ref": "env"
                }
              ]
            },
            "MEDIALIVECLOUDFRONT": {
              "Fn::FindInMap": [
                "EnvironmentMap",
                "MEDIALIVECLOUDFRONT",
                {
                  "Ref": "env"
                }
              ]
            },
            "AWS_NODEJS_CONNECTION_REUSE_ENABLED": 1
          }
        },
        "Role": {
          "Fn::GetAtt": [
            "LambdaExecutionRole",
            "Arn"
          ]
        },
        "Runtime": "nodejs14.x",
        "Layers": [],
        "Timeout": 25
      }
    },
    "LambdaExecutionRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "RoleName": {
          "Fn::If": [
            "ShouldNotCreateEnvResources",
            "amplifiedchatappLambdaRole0af75805",
            {
              "Fn::Join": [
                "",
                [
                  "amplifiedchatappLambdaRole0af75805",
                  "-",
                  {
                    "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"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    },
    "AmplifyResourcesPolicy": {
      "DependsOn": [
        "LambdaExecutionRole"
      ],
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyName": "amplify-lambda-execution-policy",
        "Roles": [
          {
            "Ref": "LambdaExecutionRole"
          }
        ],
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Action": [
                "appsync:GraphQL"
              ],
              "Resource": [
                {
                  "Fn::Join": [
                    "",
                    [
                      "arn:aws:appsync:",
                      {
                        "Ref": "AWS::Region"
                      },
                      ":",
                      {
                        "Ref": "AWS::AccountId"
                      },
                      ":apis/",
                      {
                        "Ref": "apiamplifiedchatappGraphQLAPIIdOutput"
                      },
                      "/types/Query/*"
                    ]
                  ]
                },
                {
                  "Fn::Join": [
                    "",
                    [
                      "arn:aws:appsync:",
                      {
                        "Ref": "AWS::Region"
                      },
                      ":",
                      {
                        "Ref": "AWS::AccountId"
                      },
                      ":apis/",
                      {
                        "Ref": "apiamplifiedchatappGraphQLAPIIdOutput"
                      },
                      "/types/Mutation/*"
                    ]
                  ]
                },
                {
                  "Fn::Join": [
                    "",
                    [
                      "arn:aws:appsync:",
                      {
                        "Ref": "AWS::Region"
                      },
                      ":",
                      {
                        "Ref": "AWS::AccountId"
                      },
                      ":apis/",
                      {
                        "Ref": "apiamplifiedchatappGraphQLAPIIdOutput"
                      },
                      "/types/Subscription/*"
                    ]
                  ]
                }
              ]
            }
          ]
        }
      }
    },
    "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:AWSChannelPoolTable:Name"
                        }
                      }
                    }
                  ]
                },
                {
                  "Fn::Sub": [
                    "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${tablename}/index/*",
                    {
                      "tablename": {
                        "Fn::ImportValue": {
                          "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:AWSChannelPoolTable: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"
                        }
                      }
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    },
    "CloudWatchEvent": {
      "Type": "AWS::Events::Rule",
      "Properties": {
        "Description": "Schedule rule for Lambda",
        "ScheduleExpression": {
          "Ref": "CloudWatchRule"
        },
        "State": "ENABLED",
        "Targets": [
          {
            "Arn": {
              "Fn::GetAtt": [
                "LambdaFunction",
                "Arn"
              ]
            },
            "Id": {
              "Ref": "LambdaFunction"
            }
          }
        ]
      }
    },
    "PermissionForEventsToInvokeLambda": {
      "Type": "AWS::Lambda::Permission",
      "Properties": {
        "FunctionName": {
          "Ref": "LambdaFunction"
        },
        "Action": "lambda:InvokeFunction",
        "Principal": "events.amazonaws.com",
        "SourceArn": {
          "Fn::GetAtt": [
            "CloudWatchEvent",
            "Arn"
          ]
        }
      }
    },
    "lambdaexecutionpolicyInvokeLambda": {
      "DependsOn": [
        "LambdaExecutionRole"
      ],
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyName": "lambda-execution-policy-invoke-lambda",
        "Roles": [
          {
            "Ref": "LambdaExecutionRole"
          }
        ],
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Action": [
                "lambda:InvokeFunction"
              ],
              "Resource": {
                "Fn::Sub": [
                  "arn:aws:lambda:${region}:${account}:function:videoConvertService-${env}",
                  {
                    "region": {
                      "Ref": "AWS::Region"
                    },
                    "account": {
                      "Ref": "AWS::AccountId"
                    },
                    "env": {
                      "Ref": "env"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  },
  "Outputs": {
    "Name": {
      "Value": {
        "Ref": "LambdaFunction"
      }
    },
    "Arn": {
      "Value": {
        "Fn::GetAtt": [
          "LambdaFunction",
          "Arn"
        ]
      }
    },
    "Region": {
      "Value": {
        "Ref": "AWS::Region"
      }
    },
    "LambdaExecutionRole": {
      "Value": {
        "Ref": "LambdaExecutionRole"
      }
    },
    "CloudWatchEventRule": {
      "Value": {
        "Ref": "CloudWatchEvent"
      }
    },
    "LambdaExecutionRoleArn": {
      "Value": {
        "Fn::GetAtt": [
          "LambdaExecutionRole",
          "Arn"
        ]
      }
    }
  }
}