{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "{\"createdOn\":\"Mac\",\"createdBy\":\"Amplify\",\"createdWith\":\"7.6.26\",\"stackType\":\"function-Lambda\",\"metadata\":{}}",
  "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",
            "activityService",
            {
              "Fn::Join": [
                "",
                [
                  "activityService",
                  "-",
                  {
                    "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"
            },
            "ACTIVITYTABLE": {
              "Fn::ImportValue": {
                "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:ActivityTable:Name"
              }
            },
            "USERTABLE": {
              "Fn::ImportValue": {
                "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:UserTable:Name"
              }
            },
            "LIKETABLE": {
              "Fn::ImportValue": {
                "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:LikeTable:Name"
              }
            },
            "FOLLOWTABLE": {
              "Fn::ImportValue": {
                "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:FollowTable:Name"
              }
            },
            "VIDEOTABLE": {
              "Fn::ImportValue": {
                "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:VideoTable:Name"
              }
            },
            "GRAPHQLAPIENDPOINTOUTPUT": {
              "Ref": "apiGraphQLAPIEndpointOutput"
            },
            "GRAPHQLAPIKEYOUTPUT": {
              "Ref": "apiGraphQLAPIKeyOutput"
            },
            "REDIS_URI": {
              "Fn::Sub": [
                "{{resolve:ssm:/amplify/${env}/REDIS_URI:1}}",
                {
                  "env": {
                    "Ref": "env"
                  }
                }
              ]
            },
            "REDIS_PORT": {
              "Fn::Sub": [
                "{{resolve:ssm:/amplify/${env}/REDIS_PORT:1}}",
                {
                  "env": {
                    "Ref": "env"
                  }
                }
              ]
            },
            "REDIS_USERNAME": {
              "Fn::Sub": [
                "{{resolve:ssm:/amplify/${env}/REDIS_USERNAME:1}}",
                {
                  "env": {
                    "Ref": "env"
                  }
                }
              ]
            },
            "REDIS_PASSWORD": {
              "Fn::Sub": [
                "{{resolve:ssm:/amplify/${env}/REDIS_PASSWORD:1}}",
                {
                  "env": {
                    "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",
            "amplifiedchatappLambdaRoled2861edc",
            {
              "Fn::Join": [
                "",
                [
                  "amplifiedchatappLambdaRoled2861edc",
                  "-",
                  {
                    "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:ActivityTable:Name"
                        }
                      }
                    }
                  ]
                },
                {
                  "Fn::Sub": [
                    "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${tablename}/index/*",
                    {
                      "tablename": {
                        "Fn::ImportValue": {
                          "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:ActivityTable:Name"
                        }
                      }
                    }
                  ]
                },
                {
                  "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}",
                    {
                      "tablename": {
                        "Fn::ImportValue": {
                          "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:LikeTable:Name"
                        }
                      }
                    }
                  ]
                },
                {
                  "Fn::Sub": [
                    "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${tablename}",
                    {
                      "tablename": {
                        "Fn::ImportValue": {
                          "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:FollowTable:Name"
                        }
                      }
                    }
                  ]
                },
                {
                  "Fn::Sub": [
                    "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${tablename}",
                    {
                      "tablename": {
                        "Fn::ImportValue": {
                          "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:VideoTable:Name"
                        }
                      }
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    },
    "LambdaTriggerPolicyLike": {
      "DependsOn": [
        "LambdaExecutionRole"
      ],
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyName": "amplify-lambda-execution-policy-Like",
        "Roles": [
          {
            "Ref": "LambdaExecutionRole"
          }
        ],
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Action": [
                "dynamodb:DescribeStream",
                "dynamodb:GetRecords",
                "dynamodb:GetShardIterator",
                "dynamodb:ListStreams"
              ],
              "Resource": {
                "Fn::ImportValue": {
                  "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:LikeTable:StreamArn"
                }
              }
            }
          ]
        }
      }
    },
    "LambdaEventSourceMappingLike": {
      "Type": "AWS::Lambda::EventSourceMapping",
      "DependsOn": [
        "LambdaTriggerPolicyLike",
        "LambdaExecutionRole"
      ],
      "Properties": {
        "BatchSize": 100,
        "Enabled": true,
        "EventSourceArn": {
          "Fn::ImportValue": {
            "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:LikeTable:StreamArn"
          }
        },
        "FunctionName": {
          "Fn::GetAtt": [
            "LambdaFunction",
            "Arn"
          ]
        },
        "StartingPosition": "LATEST"
      }
    },
    "LambdaTriggerPolicyFollow": {
      "DependsOn": [
        "LambdaExecutionRole"
      ],
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyName": "amplify-lambda-execution-policy-Follow",
        "Roles": [
          {
            "Ref": "LambdaExecutionRole"
          }
        ],
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Action": [
                "dynamodb:DescribeStream",
                "dynamodb:GetRecords",
                "dynamodb:GetShardIterator",
                "dynamodb:ListStreams"
              ],
              "Resource": {
                "Fn::ImportValue": {
                  "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:FollowTable:StreamArn"
                }
              }
            }
          ]
        }
      }
    },
    "LambdaEventSourceMappingFollow": {
      "Type": "AWS::Lambda::EventSourceMapping",
      "DependsOn": [
        "LambdaTriggerPolicyFollow",
        "LambdaExecutionRole"
      ],
      "Properties": {
        "BatchSize": 100,
        "Enabled": true,
        "EventSourceArn": {
          "Fn::ImportValue": {
            "Fn::Sub": "${apiGraphQLAPIIdOutput}:GetAtt:FollowTable:StreamArn"
          }
        },
        "FunctionName": {
          "Fn::GetAtt": [
            "LambdaFunction",
            "Arn"
          ]
        },
        "StartingPosition": "LATEST"
      }
    },
    "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"
          ]
        }
      }
    }
  },
  "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"
        ]
      }
    }
  }
}