{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "An auto-generated nested stack.",
    "Metadata": {},
    "Parameters": {
        "AppSyncApiName": {
            "Type": "String",
            "Description": "The name of the AppSync API",
            "Default": "AppSyncSimpleTransform"
        },
        "APIKeyExpirationEpoch": {
            "Type": "Number",
            "Description": "The epoch time in seconds when the API Key should expire. Setting this to 0 will default to 1 week from the deployment date. Setting this to -1 will not create an API Key.",
            "Default": 0
        },
        "DynamoDBModelTableReadIOPS": {
            "Type": "Number",
            "Description": "The number of read IOPS the table should support.",
            "Default": 5
        },
        "DynamoDBModelTableWriteIOPS": {
            "Type": "Number",
            "Description": "The number of write IOPS the table should support.",
            "Default": 5
        },
        "DynamoDBBillingMode": {
            "Type": "String",
            "Description": "Configure @model types to create DynamoDB tables with PAY_PER_REQUEST or PROVISIONED billing modes.",
            "Default": "PAY_PER_REQUEST",
            "AllowedValues": [
                "PAY_PER_REQUEST",
                "PROVISIONED"
            ]
        },
        "DynamoDBEnablePointInTimeRecovery": {
            "Type": "String",
            "Description": "Whether to enable Point in Time Recovery on the table",
            "Default": "false",
            "AllowedValues": [
                "true",
                "false"
            ]
        },
        "AuthCognitoUserPoolId": {
            "Type": "String",
            "Description": "The id of an existing User Pool to connect. If this is changed, a user pool will not be created for you.",
            "Default": "NONE"
        },
        "env": {
            "Type": "String",
            "Description": "The environment name. e.g. Dev, Test, or Production",
            "Default": "NONE"
        },
        "S3DeploymentBucket": {
            "Type": "String",
            "Description": "The S3 bucket containing all deployment assets for the project."
        },
        "S3DeploymentRootKey": {
            "Type": "String",
            "Description": "An S3 key relative to the S3DeploymentBucket that points to the root of the deployment directory."
        },
        "AppSyncApiId": {
            "Type": "String",
            "Description": "The id of the AppSync API associated with this project."
        },
        "GetAttGraphQLAPIApiId": {
            "Type": "String",
            "Description": "Auto-generated parameter that forwards Fn.GetAtt(GraphQLAPI, ApiId) through to nested stacks."
        }
    },
    "Resources": {
        "HomeIAMRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "RoleName": {
                    "Fn::If": [
                        "HasEnvironmentParameter",
                        {
                            "Fn::Join": [
                                "-",
                                [
                                    "Home",
                                    "role",
                                    {
                                        "Ref": "GetAttGraphQLAPIApiId"
                                    },
                                    {
                                        "Ref": "env"
                                    }
                                ]
                            ]
                        },
                        {
                            "Fn::Join": [
                                "-",
                                [
                                    "Home",
                                    "role",
                                    {
                                        "Ref": "GetAttGraphQLAPIApiId"
                                    }
                                ]
                            ]
                        }
                    ]
                },
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": "appsync.amazonaws.com"
                            },
                            "Action": "sts:AssumeRole"
                        }
                    ]
                },
                "Policies": [
                    {
                        "PolicyName": "DynamoDBAccess",
                        "PolicyDocument": {
                            "Version": "2012-10-17",
                            "Statement": [
                                {
                                    "Effect": "Allow",
                                    "Action": [
                                        "dynamodb:BatchGetItem",
                                        "dynamodb:BatchWriteItem",
                                        "dynamodb:PutItem",
                                        "dynamodb:DeleteItem",
                                        "dynamodb:GetItem",
                                        "dynamodb:Scan",
                                        "dynamodb:Query",
                                        "dynamodb:UpdateItem"
                                    ],
                                    "Resource": [
                                        {
                                            "Fn::Sub": [
                                                "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${tablename}",
                                                {
                                                    "tablename": {
                                                        "Fn::If": [
                                                            "HasEnvironmentParameter",
                                                            {
                                                                "Fn::Join": [
                                                                    "-",
                                                                    [
                                                                        "Home",
                                                                        {
                                                                            "Ref": "GetAttGraphQLAPIApiId"
                                                                        },
                                                                        {
                                                                            "Ref": "env"
                                                                        }
                                                                    ]
                                                                ]
                                                            },
                                                            {
                                                                "Fn::Join": [
                                                                    "-",
                                                                    [
                                                                        "Home",
                                                                        {
                                                                            "Ref": "GetAttGraphQLAPIApiId"
                                                                        }
                                                                    ]
                                                                ]
                                                            }
                                                        ]
                                                    }
                                                }
                                            ]
                                        },
                                        {
                                            "Fn::Sub": [
                                                "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${tablename}/*",
                                                {
                                                    "tablename": {
                                                        "Fn::If": [
                                                            "HasEnvironmentParameter",
                                                            {
                                                                "Fn::Join": [
                                                                    "-",
                                                                    [
                                                                        "Home",
                                                                        {
                                                                            "Ref": "GetAttGraphQLAPIApiId"
                                                                        },
                                                                        {
                                                                            "Ref": "env"
                                                                        }
                                                                    ]
                                                                ]
                                                            },
                                                            {
                                                                "Fn::Join": [
                                                                    "-",
                                                                    [
                                                                        "Home",
                                                                        {
                                                                            "Ref": "GetAttGraphQLAPIApiId"
                                                                        }
                                                                    ]
                                                                ]
                                                            }
                                                        ]
                                                    }
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    }
                ]
            }
        },
        "HomeDataSource": {
            "Type": "AWS::AppSync::DataSource",
            "Properties": {
                "ApiId": {
                    "Ref": "GetAttGraphQLAPIApiId"
                },
                "Name": "HomeTable",
                "Type": "AMAZON_DYNAMODB",
                "ServiceRoleArn": {
                    "Fn::GetAtt": [
                        "HomeIAMRole",
                        "Arn"
                    ]
                },
                "DynamoDBConfig": {
                    "AwsRegion": {
                        "Ref": "AWS::Region"
                    },
                    "TableName": {
                        "Fn::If": [
                            "HasEnvironmentParameter",
                            {
                                "Fn::Join": [
                                    "-",
                                    [
                                        "Home",
                                        {
                                            "Ref": "GetAttGraphQLAPIApiId"
                                        },
                                        {
                                            "Ref": "env"
                                        }
                                    ]
                                ]
                            },
                            {
                                "Fn::Join": [
                                    "-",
                                    [
                                        "Home",
                                        {
                                            "Ref": "GetAttGraphQLAPIApiId"
                                        }
                                    ]
                                ]
                            }
                        ]
                    }
                }
            },
            "DependsOn": [
                "HomeIAMRole"
            ]
        },
        "GetHomeResolver": {
            "Type": "AWS::AppSync::Resolver",
            "Properties": {
                "ApiId": {
                    "Ref": "GetAttGraphQLAPIApiId"
                },
                "DataSourceName": {
                    "Fn::GetAtt": [
                        "HomeDataSource",
                        "Name"
                    ]
                },
                "FieldName": "getHome",
                "TypeName": "Query",
                "RequestMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Query",
                                        "getHome",
                                        "req",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                },
                "ResponseMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Query",
                                        "getHome",
                                        "res",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                }
            }
        },
        "ListHomeResolver": {
            "Type": "AWS::AppSync::Resolver",
            "Properties": {
                "ApiId": {
                    "Ref": "GetAttGraphQLAPIApiId"
                },
                "DataSourceName": {
                    "Fn::GetAtt": [
                        "HomeDataSource",
                        "Name"
                    ]
                },
                "FieldName": "listHomes",
                "TypeName": "Query",
                "RequestMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Query",
                                        "listHomes",
                                        "req",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                },
                "ResponseMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Query",
                                        "listHomes",
                                        "res",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                }
            }
        },
        "CreateHomeResolver": {
            "Type": "AWS::AppSync::Resolver",
            "Properties": {
                "ApiId": {
                    "Ref": "GetAttGraphQLAPIApiId"
                },
                "DataSourceName": {
                    "Fn::GetAtt": [
                        "HomeDataSource",
                        "Name"
                    ]
                },
                "FieldName": "createHome",
                "TypeName": "Mutation",
                "RequestMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Mutation",
                                        "createHome",
                                        "req",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                },
                "ResponseMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Mutation",
                                        "createHome",
                                        "res",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                }
            }
        },
        "UpdateHomeResolver": {
            "Type": "AWS::AppSync::Resolver",
            "Properties": {
                "ApiId": {
                    "Ref": "GetAttGraphQLAPIApiId"
                },
                "DataSourceName": {
                    "Fn::GetAtt": [
                        "HomeDataSource",
                        "Name"
                    ]
                },
                "FieldName": "updateHome",
                "TypeName": "Mutation",
                "RequestMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Mutation",
                                        "updateHome",
                                        "req",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                },
                "ResponseMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Mutation",
                                        "updateHome",
                                        "res",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                }
            }
        },
        "DeleteHomeResolver": {
            "Type": "AWS::AppSync::Resolver",
            "Properties": {
                "ApiId": {
                    "Ref": "GetAttGraphQLAPIApiId"
                },
                "DataSourceName": {
                    "Fn::GetAtt": [
                        "HomeDataSource",
                        "Name"
                    ]
                },
                "FieldName": "deleteHome",
                "TypeName": "Mutation",
                "RequestMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Mutation",
                                        "deleteHome",
                                        "req",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                },
                "ResponseMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Mutation",
                                        "deleteHome",
                                        "res",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                }
            }
        }
    },
    "Outputs": {
        "GetAttHomeDataSourceName": {
            "Value": {
                "Fn::GetAtt": [
                    "HomeDataSource",
                    "Name"
                ]
            },
            "Export": {
                "Name": {
                    "Fn::Join": [
                        ":",
                        [
                            {
                                "Ref": "AppSyncApiId"
                            },
                            "GetAtt",
                            "HomeDataSource",
                            "Name"
                        ]
                    ]
                }
            }
        }
    },
    "Conditions": {
        "ShouldUsePayPerRequestBilling": {
            "Fn::Equals": [
                {
                    "Ref": "DynamoDBBillingMode"
                },
                "PAY_PER_REQUEST"
            ]
        },
        "ShouldUsePointInTimeRecovery": {
            "Fn::Equals": [
                {
                    "Ref": "DynamoDBEnablePointInTimeRecovery"
                },
                "true"
            ]
        },
        "ShouldCreateAPIKey": {
            "Fn::And": [
                {
                    "Fn::Not": [
                        {
                            "Fn::Equals": [
                                {
                                    "Ref": "APIKeyExpirationEpoch"
                                },
                                -1
                            ]
                        }
                    ]
                },
                {
                    "Fn::Equals": [
                        {
                            "Ref": "AuthCognitoUserPoolId"
                        },
                        "NONE"
                    ]
                }
            ]
        },
        "APIKeyExpirationEpochIsPositive": {
            "Fn::And": [
                {
                    "Fn::Not": [
                        {
                            "Fn::Equals": [
                                {
                                    "Ref": "APIKeyExpirationEpoch"
                                },
                                -1
                            ]
                        }
                    ]
                },
                {
                    "Fn::Not": [
                        {
                            "Fn::Equals": [
                                {
                                    "Ref": "APIKeyExpirationEpoch"
                                },
                                0
                            ]
                        }
                    ]
                }
            ]
        },
        "HasEnvironmentParameter": {
            "Fn::Not": [
                {
                    "Fn::Equals": [
                        {
                            "Ref": "env"
                        },
                        "NONE"
                    ]
                }
            ]
        }
    }
}