{
    "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": {
        "SceneActionTable": {
            "Type": "AWS::DynamoDB::Table",
            "Properties": {
                "TableName": {
                    "Fn::If": [
                        "HasEnvironmentParameter",
                        {
                            "Fn::Join": [
                                "-",
                                [
                                    "SceneAction",
                                    {
                                        "Ref": "GetAttGraphQLAPIApiId"
                                    },
                                    {
                                        "Ref": "env"
                                    }
                                ]
                            ]
                        },
                        {
                            "Fn::Join": [
                                "-",
                                [
                                    "SceneAction",
                                    {
                                        "Ref": "GetAttGraphQLAPIApiId"
                                    }
                                ]
                            ]
                        }
                    ]
                },
                "KeySchema": [
                    {
                        "AttributeName": "id",
                        "KeyType": "HASH"
                    }
                ],
                "AttributeDefinitions": [
                    {
                        "AttributeName": "id",
                        "AttributeType": "S"
                    },
                    {
                        "AttributeName": "sceneActionSceneId",
                        "AttributeType": "S"
                    },
                    {
                        "AttributeName": "sceneActionShadeId",
                        "AttributeType": "S"
                    }
                ],
                "StreamSpecification": {
                    "StreamViewType": "NEW_AND_OLD_IMAGES"
                },
                "BillingMode": {
                    "Fn::If": [
                        "ShouldUsePayPerRequestBilling",
                        "PAY_PER_REQUEST",
                        {
                            "Ref": "AWS::NoValue"
                        }
                    ]
                },
                "ProvisionedThroughput": {
                    "Fn::If": [
                        "ShouldUsePayPerRequestBilling",
                        {
                            "Ref": "AWS::NoValue"
                        },
                        {
                            "ReadCapacityUnits": {
                                "Ref": "DynamoDBModelTableReadIOPS"
                            },
                            "WriteCapacityUnits": {
                                "Ref": "DynamoDBModelTableWriteIOPS"
                            }
                        }
                    ]
                },
                "SSESpecification": {
                    "SSEEnabled": true
                },
                "PointInTimeRecoverySpecification": {
                    "Fn::If": [
                        "ShouldUsePointInTimeRecovery",
                        {
                            "PointInTimeRecoveryEnabled": true
                        },
                        {
                            "Ref": "AWS::NoValue"
                        }
                    ]
                },
                "GlobalSecondaryIndexes": [
                    {
                        "IndexName": "gsi-SceneActionScene",
                        "KeySchema": [
                            {
                                "AttributeName": "sceneActionSceneId",
                                "KeyType": "HASH"
                            }
                        ],
                        "Projection": {
                            "ProjectionType": "ALL"
                        },
                        "ProvisionedThroughput": {
                            "Fn::If": [
                                "ShouldUsePayPerRequestBilling",
                                {
                                    "Ref": "AWS::NoValue"
                                },
                                {
                                    "ReadCapacityUnits": {
                                        "Ref": "DynamoDBModelTableReadIOPS"
                                    },
                                    "WriteCapacityUnits": {
                                        "Ref": "DynamoDBModelTableWriteIOPS"
                                    }
                                }
                            ]
                        }
                    },
                    {
                        "IndexName": "gsi-SceneActionShade",
                        "KeySchema": [
                            {
                                "AttributeName": "sceneActionShadeId",
                                "KeyType": "HASH"
                            }
                        ],
                        "Projection": {
                            "ProjectionType": "ALL"
                        },
                        "ProvisionedThroughput": {
                            "Fn::If": [
                                "ShouldUsePayPerRequestBilling",
                                {
                                    "Ref": "AWS::NoValue"
                                },
                                {
                                    "ReadCapacityUnits": {
                                        "Ref": "DynamoDBModelTableReadIOPS"
                                    },
                                    "WriteCapacityUnits": {
                                        "Ref": "DynamoDBModelTableWriteIOPS"
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        },
        "SceneActionIAMRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "RoleName": {
                    "Fn::If": [
                        "HasEnvironmentParameter",
                        {
                            "Fn::Join": [
                                "-",
                                [
                                    "SceneAction",
                                    "role",
                                    {
                                        "Ref": "GetAttGraphQLAPIApiId"
                                    },
                                    {
                                        "Ref": "env"
                                    }
                                ]
                            ]
                        },
                        {
                            "Fn::Join": [
                                "-",
                                [
                                    "SceneAction",
                                    "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": [
                                                                    "-",
                                                                    [
                                                                        "SceneAction",
                                                                        {
                                                                            "Ref": "GetAttGraphQLAPIApiId"
                                                                        },
                                                                        {
                                                                            "Ref": "env"
                                                                        }
                                                                    ]
                                                                ]
                                                            },
                                                            {
                                                                "Fn::Join": [
                                                                    "-",
                                                                    [
                                                                        "SceneAction",
                                                                        {
                                                                            "Ref": "GetAttGraphQLAPIApiId"
                                                                        }
                                                                    ]
                                                                ]
                                                            }
                                                        ]
                                                    }
                                                }
                                            ]
                                        },
                                        {
                                            "Fn::Sub": [
                                                "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${tablename}/*",
                                                {
                                                    "tablename": {
                                                        "Fn::If": [
                                                            "HasEnvironmentParameter",
                                                            {
                                                                "Fn::Join": [
                                                                    "-",
                                                                    [
                                                                        "SceneAction",
                                                                        {
                                                                            "Ref": "GetAttGraphQLAPIApiId"
                                                                        },
                                                                        {
                                                                            "Ref": "env"
                                                                        }
                                                                    ]
                                                                ]
                                                            },
                                                            {
                                                                "Fn::Join": [
                                                                    "-",
                                                                    [
                                                                        "SceneAction",
                                                                        {
                                                                            "Ref": "GetAttGraphQLAPIApiId"
                                                                        }
                                                                    ]
                                                                ]
                                                            }
                                                        ]
                                                    }
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    }
                ]
            }
        },
        "SceneActionDataSource": {
            "Type": "AWS::AppSync::DataSource",
            "Properties": {
                "ApiId": {
                    "Ref": "GetAttGraphQLAPIApiId"
                },
                "Name": "SceneActionTable",
                "Type": "AMAZON_DYNAMODB",
                "ServiceRoleArn": {
                    "Fn::GetAtt": [
                        "SceneActionIAMRole",
                        "Arn"
                    ]
                },
                "DynamoDBConfig": {
                    "AwsRegion": {
                        "Ref": "AWS::Region"
                    },
                    "TableName": {
                        "Fn::If": [
                            "HasEnvironmentParameter",
                            {
                                "Fn::Join": [
                                    "-",
                                    [
                                        "SceneAction",
                                        {
                                            "Ref": "GetAttGraphQLAPIApiId"
                                        },
                                        {
                                            "Ref": "env"
                                        }
                                    ]
                                ]
                            },
                            {
                                "Fn::Join": [
                                    "-",
                                    [
                                        "SceneAction",
                                        {
                                            "Ref": "GetAttGraphQLAPIApiId"
                                        }
                                    ]
                                ]
                            }
                        ]
                    }
                }
            },
            "DependsOn": [
                "SceneActionIAMRole"
            ]
        },
        "GetSceneActionResolver": {
            "Type": "AWS::AppSync::Resolver",
            "Properties": {
                "ApiId": {
                    "Ref": "GetAttGraphQLAPIApiId"
                },
                "DataSourceName": {
                    "Fn::GetAtt": [
                        "SceneActionDataSource",
                        "Name"
                    ]
                },
                "FieldName": "getSceneAction",
                "TypeName": "Query",
                "RequestMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Query",
                                        "getSceneAction",
                                        "req",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                },
                "ResponseMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Query",
                                        "getSceneAction",
                                        "res",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                }
            }
        },
        "ListSceneActionResolver": {
            "Type": "AWS::AppSync::Resolver",
            "Properties": {
                "ApiId": {
                    "Ref": "GetAttGraphQLAPIApiId"
                },
                "DataSourceName": {
                    "Fn::GetAtt": [
                        "SceneActionDataSource",
                        "Name"
                    ]
                },
                "FieldName": "listSceneActions",
                "TypeName": "Query",
                "RequestMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Query",
                                        "listSceneActions",
                                        "req",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                },
                "ResponseMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Query",
                                        "listSceneActions",
                                        "res",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                }
            }
        },
        "CreateSceneActionResolver": {
            "Type": "AWS::AppSync::Resolver",
            "Properties": {
                "ApiId": {
                    "Ref": "GetAttGraphQLAPIApiId"
                },
                "DataSourceName": {
                    "Fn::GetAtt": [
                        "SceneActionDataSource",
                        "Name"
                    ]
                },
                "FieldName": "createSceneAction",
                "TypeName": "Mutation",
                "RequestMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Mutation",
                                        "createSceneAction",
                                        "req",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                },
                "ResponseMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Mutation",
                                        "createSceneAction",
                                        "res",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                }
            }
        },
        "UpdateSceneActionResolver": {
            "Type": "AWS::AppSync::Resolver",
            "Properties": {
                "ApiId": {
                    "Ref": "GetAttGraphQLAPIApiId"
                },
                "DataSourceName": {
                    "Fn::GetAtt": [
                        "SceneActionDataSource",
                        "Name"
                    ]
                },
                "FieldName": "updateSceneAction",
                "TypeName": "Mutation",
                "RequestMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Mutation",
                                        "updateSceneAction",
                                        "req",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                },
                "ResponseMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Mutation",
                                        "updateSceneAction",
                                        "res",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                }
            }
        },
        "DeleteSceneActionResolver": {
            "Type": "AWS::AppSync::Resolver",
            "Properties": {
                "ApiId": {
                    "Ref": "GetAttGraphQLAPIApiId"
                },
                "DataSourceName": {
                    "Fn::GetAtt": [
                        "SceneActionDataSource",
                        "Name"
                    ]
                },
                "FieldName": "deleteSceneAction",
                "TypeName": "Mutation",
                "RequestMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Mutation",
                                        "deleteSceneAction",
                                        "req",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                },
                "ResponseMappingTemplateS3Location": {
                    "Fn::Sub": [
                        "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/${ResolverFileName}",
                        {
                            "S3DeploymentBucket": {
                                "Ref": "S3DeploymentBucket"
                            },
                            "S3DeploymentRootKey": {
                                "Ref": "S3DeploymentRootKey"
                            },
                            "ResolverFileName": {
                                "Fn::Join": [
                                    ".",
                                    [
                                        "Mutation",
                                        "deleteSceneAction",
                                        "res",
                                        "vtl"
                                    ]
                                ]
                            }
                        }
                    ]
                }
            }
        }
    },
    "Outputs": {
        "GetAttSceneActionDataSourceName": {
            "Value": {
                "Fn::GetAtt": [
                    "SceneActionDataSource",
                    "Name"
                ]
            },
            "Export": {
                "Name": {
                    "Fn::Join": [
                        ":",
                        [
                            {
                                "Ref": "AppSyncApiId"
                            },
                            "GetAtt",
                            "SceneActionDataSource",
                            "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"
                    ]
                }
            ]
        }
    }
}