# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 AWSTemplateFormatVersion: '2010-09-09' Description: 'sputnik - AppSync - Version %%VERSION%%' Parameters: sourceBucket: Type: String Description: sputnik S3 bucket. sourceKeyPrefix: Type: String Description: sputnik Source key prefix. userPoolId: Type: String Description: sputnik User Pool Id. settingsTable: Type: String Description: DynamoDB Table for storing the application settings dataStoreTable: Type: String Description: DynamoDB Table for the data store deviceTypesTable: Type: String Description: DynamoDB Table for storing the device types deviceBlueprintsTable: Type: String Description: DynamoDB Table for storing the device blueprints devicesTable: Type: String Description: DynamoDB Table for storing the devices deploymentsTable: Type: String Description: DynamoDB Table for storing the deployments systemsTable: Type: String Description: DynamoDB Table for storing the systems adminServiceLambdaFunctionArn: Type: String Description: Admin Micro Service ARN systemBlueprintsTable: Type: String Description: DynamoDB Table for storing the system blueprints settingsServiceLambdaFunctionArn: Type: String Description: Settings Micro Service ARN devicesServiceLambdaFunctionArn: Type: String Description: Devices Micro Service ARN deploymentsServiceLambdaFunctionArn: Type: String Description: Deployments Micro Service ARN systemsServiceLambdaFunctionArn: Type: String Description: Systems Micro Service ARN utilsCustomResourceLambdaFunctionArn: Type: String Description: Utils Micro Service ARN Resources: # #################################################################################################### # #################################################################################################### # # Helpers # # #################################################################################################### # #################################################################################################### AppSyncRandomStringResource: Type: Custom::AppSyncRandomString Properties: ServiceToken: !GetAtt AppSyncRandomStringFunction.Arn Length: '12' AppSyncRandomStringFunction: Type: AWS::Lambda::Function Properties: Handler: index.lambda_handler Timeout: 10 Role: !GetAtt AppSyncLambdaExecutionRole.Arn Runtime: python3.6 Code: ZipFile: | import cfnresponse from random import choice from string import ascii_lowercase, digits def random_string(length=8, chars=ascii_lowercase + digits): return "".join(choice(chars) for x in range(length)) def lambda_handler(event, context): print(f"Data in event: {event}") response_data = {} if event["RequestType"] == "Create": string_length = int(event["ResourceProperties"]["Length"]) physicalResourceId = random_string(string_length) response_data = { "RandomString": physicalResourceId } else: # if event["RequestType"] == "Update" or event["RequestType"] == "Delete": physicalResourceId = event["PhysicalResourceId"] cfnresponse.send(event, context, cfnresponse.SUCCESS, response_data, physicalResourceId) AppSyncLambdaExecutionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: sts:AssumeRole Condition: {} Path: / ManagedPolicyArns: - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole # #################################################################################################### # #################################################################################################### # # ROLE # # #################################################################################################### # #################################################################################################### # ROLE appSyncRole: Type: 'AWS::IAM::Role' Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: 'Allow' Principal: Service: - 'appsync.amazonaws.com' Action: - 'sts:AssumeRole' Path: '/' # POLICY appSyncDDBPolicy: Type: 'AWS::IAM::ManagedPolicy' Properties: Description: 'Policy for the sputnik AppSync API to access DynamoDB.' PolicyDocument: Version: '2012-10-17' Statement: - Effect: 'Allow' Action: - 'dynamodb:BatchGetItem' - 'dynamodb:BatchWriteItem' - 'dynamodb:DeleteItem' - 'dynamodb:GetItem' - 'dynamodb:PutItem' - 'dynamodb:Query' - 'dynamodb:Scan' - 'dynamodb:UpdateItem' Resource: - !Join ['', ['arn:aws:dynamodb:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':table/', !Ref settingsTable ]] - !Join ['', ['arn:aws:dynamodb:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':table/', !Ref dataStoreTable ]] - !Join ['', ['arn:aws:dynamodb:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':table/', !Ref deviceTypesTable ]] - !Join ['', ['arn:aws:dynamodb:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':table/', !Ref deviceBlueprintsTable ]] - !Join ['', ['arn:aws:dynamodb:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':table/', !Ref devicesTable ]] - !Join ['', ['arn:aws:dynamodb:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':table/', !Ref devicesTable, '/index/deviceBlueprintId']] - !Join ['', ['arn:aws:dynamodb:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':table/', !Ref devicesTable, '/index/deviceTypeId']] - !Join ['', ['arn:aws:dynamodb:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':table/', !Ref deploymentsTable ]] - !Join ['', ['arn:aws:dynamodb:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':table/', !Ref systemsTable ]] - !Join ['', ['arn:aws:dynamodb:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':table/', !Ref systemBlueprintsTable ]] Roles: - !Ref appSyncRole appSyncLambdaPolicy: Type: 'AWS::IAM::ManagedPolicy' Properties: Description: 'Policy for the sputnik AppSync API to access Lambda.' PolicyDocument: Version: '2012-10-17' Statement: - Effect: 'Allow' Action: - 'lambda:InvokeFunction' Resource: - !Ref adminServiceLambdaFunctionArn - !Ref deploymentsServiceLambdaFunctionArn - !Ref devicesServiceLambdaFunctionArn - !Ref settingsServiceLambdaFunctionArn - !Ref systemsServiceLambdaFunctionArn - !Ref utilsCustomResourceLambdaFunctionArn Roles: - !Ref appSyncRole # #################################################################################################### # #################################################################################################### # # API # # #################################################################################################### # #################################################################################################### # API appSyncGraphQLApi: Type: 'AWS::AppSync::GraphQLApi' Properties: Name: !Sub 'sputnik-API-${AppSyncRandomStringResource}' AuthenticationType: AMAZON_COGNITO_USER_POOLS UserPoolConfig: UserPoolId: !Ref userPoolId AwsRegion: !Ref 'AWS::Region' DefaultAction: DENY # #################################################################################################### # #################################################################################################### # # DATA SOURCES # # #################################################################################################### # #################################################################################################### # DATA SOURCES - DynamoDB appSyncDataSourceSettingsDynamoDB: Type: 'AWS::AppSync::DataSource' Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId Name: !Sub 'sputnik_appSyncDataSourceSettingsDynamoDB_${AppSyncRandomStringResource}' Description: The Settings DynamoDB table App Sync Data source Type: AMAZON_DYNAMODB ServiceRoleArn: !Join ['', ['arn:aws:iam::', !Ref 'AWS::AccountId', ':role/', !Ref appSyncRole]] DynamoDBConfig: AwsRegion: !Ref 'AWS::Region' TableName: !Ref settingsTable appSyncDataSourceDataStoreDynamoDB: Type: 'AWS::AppSync::DataSource' Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId Name: !Sub 'sputnik_appSyncDataSourceDataStoreDynamoDB_${AppSyncRandomStringResource}' Description: The Data Store DynamoDB table App Sync Data Source Type: AMAZON_DYNAMODB ServiceRoleArn: !Join ['', ['arn:aws:iam::', !Ref 'AWS::AccountId', ':role/', !Ref appSyncRole]] DynamoDBConfig: AwsRegion: !Ref 'AWS::Region' TableName: !Ref dataStoreTable appSyncDataSourceDeviceTypesDynamoDB: Type: 'AWS::AppSync::DataSource' Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId Name: !Sub 'sputnik_appSyncDataSourceDeviceTypesDynamoDB_${AppSyncRandomStringResource}' Description: The Device Types DynamoDB table App Sync Data Source Type: AMAZON_DYNAMODB ServiceRoleArn: !Join ['', ['arn:aws:iam::', !Ref 'AWS::AccountId', ':role/', !Ref appSyncRole]] DynamoDBConfig: AwsRegion: !Ref 'AWS::Region' TableName: !Ref deviceTypesTable appSyncDataSourceDeviceBlueprintsDynamoDB: Type: 'AWS::AppSync::DataSource' Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId Name: !Sub 'sputnik_appSyncDataSourceDeviceBlueprintsDynamoDB_${AppSyncRandomStringResource}' Description: The Device Blueprints DynamoDB table App Sync Data Source Type: AMAZON_DYNAMODB ServiceRoleArn: !Join ['', ['arn:aws:iam::', !Ref 'AWS::AccountId', ':role/', !Ref appSyncRole]] DynamoDBConfig: AwsRegion: !Ref 'AWS::Region' TableName: !Ref deviceBlueprintsTable appSyncDataSourceDevicesDynamoDB: Type: 'AWS::AppSync::DataSource' Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId Name: !Sub 'sputnik_appSyncDataSourceDevicesDynamoDB_${AppSyncRandomStringResource}' Description: The Devices DynamoDB table App Sync Data Source Type: AMAZON_DYNAMODB ServiceRoleArn: !Join ['', ['arn:aws:iam::', !Ref 'AWS::AccountId', ':role/', !Ref appSyncRole]] DynamoDBConfig: AwsRegion: !Ref 'AWS::Region' TableName: !Ref devicesTable appSyncDataSourceDeploymentsDynamoDB: Type: 'AWS::AppSync::DataSource' Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId Name: !Sub 'sputnik_appSyncDataSourceDeploymentsDynamoDB_${AppSyncRandomStringResource}' Description: The Deployments DynamoDB table App Sync Data Source Type: AMAZON_DYNAMODB ServiceRoleArn: !Join ['', ['arn:aws:iam::', !Ref 'AWS::AccountId', ':role/', !Ref appSyncRole]] DynamoDBConfig: AwsRegion: !Ref 'AWS::Region' TableName: !Ref deploymentsTable appSyncDataSourceSystemsDynamoDB: Type: 'AWS::AppSync::DataSource' Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId Name: !Sub 'sputnik_appSyncDataSourceSystemsDynamoDB_${AppSyncRandomStringResource}' Description: The Systems DynamoDB table App Sync Data Source Type: AMAZON_DYNAMODB ServiceRoleArn: !Join ['', ['arn:aws:iam::', !Ref 'AWS::AccountId', ':role/', !Ref appSyncRole]] DynamoDBConfig: AwsRegion: !Ref 'AWS::Region' TableName: !Ref systemsTable appSyncDataSourceSystemBlueprintsDynamoDB: Type: 'AWS::AppSync::DataSource' Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId Name: !Sub 'sputnik_appSyncDataSourceSystemBlueprintsDynamoDB_${AppSyncRandomStringResource}' Description: The System Blueprints DynamoDB table App Sync Data Source Type: AMAZON_DYNAMODB ServiceRoleArn: !Join ['', ['arn:aws:iam::', !Ref 'AWS::AccountId', ':role/', !Ref appSyncRole]] DynamoDBConfig: AwsRegion: !Ref 'AWS::Region' TableName: !Ref systemBlueprintsTable appSyncDataSourceAdminLambda: Type: AWS::AppSync::DataSource Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId Name: !Sub 'sputnik_appSyncDataSourceAdminLambda_${AppSyncRandomStringResource}' Description: The Admin Lambda App Sync Data Source Type: AWS_LAMBDA ServiceRoleArn: !Join ['', ['arn:aws:iam::', !Ref 'AWS::AccountId', ':role/', !Ref appSyncRole]] LambdaConfig: LambdaFunctionArn: !Ref adminServiceLambdaFunctionArn appSyncDataSourceSettingsLambda: Type: AWS::AppSync::DataSource Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId Name: !Sub 'sputnik_appSyncDataSourceSettingsLambda_${AppSyncRandomStringResource}' Description: The Settings Lambda App Sync Data Source Type: AWS_LAMBDA ServiceRoleArn: !Join ['', ['arn:aws:iam::', !Ref 'AWS::AccountId', ':role/', !Ref appSyncRole]] LambdaConfig: LambdaFunctionArn: !Ref settingsServiceLambdaFunctionArn appSyncDataSourceDevicesLambda: Type: AWS::AppSync::DataSource Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId Name: !Sub 'sputnik_appSyncDataSourceDevicesLambda_${AppSyncRandomStringResource}' Description: The Devices Lambda App Sync Data Source Type: AWS_LAMBDA ServiceRoleArn: !Join ['', ['arn:aws:iam::', !Ref 'AWS::AccountId', ':role/', !Ref appSyncRole]] LambdaConfig: LambdaFunctionArn: !Ref devicesServiceLambdaFunctionArn appSyncDataSourceDeploymentsLambda: Type: AWS::AppSync::DataSource Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId Name: !Sub 'sputnik_appSyncDataSourceDeploymentsLambda_${AppSyncRandomStringResource}' Description: The Deployments Lambda App Sync Data Source Type: AWS_LAMBDA ServiceRoleArn: !Join ['', ['arn:aws:iam::', !Ref 'AWS::AccountId', ':role/', !Ref appSyncRole]] LambdaConfig: LambdaFunctionArn: !Ref deploymentsServiceLambdaFunctionArn appSyncDataSourceSystemsLambda: Type: AWS::AppSync::DataSource Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId Name: !Sub 'sputnik_appSyncDataSourceSystemsLambda_${AppSyncRandomStringResource}' Description: The Systems Lambda App Sync Data Source Type: AWS_LAMBDA ServiceRoleArn: !Join ['', ['arn:aws:iam::', !Ref 'AWS::AccountId', ':role/', !Ref appSyncRole]] LambdaConfig: LambdaFunctionArn: !Ref systemsServiceLambdaFunctionArn appSyncDataSourceUtilsLambda: Type: AWS::AppSync::DataSource Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId Name: !Sub 'sputnik_appSyncDataSourceUtilsLambda_${AppSyncRandomStringResource}' Description: The Utils Lambda App Sync Data Source Type: AWS_LAMBDA ServiceRoleArn: !Join ['', ['arn:aws:iam::', !Ref 'AWS::AccountId', ':role/', !Ref appSyncRole]] LambdaConfig: LambdaFunctionArn: !Ref utilsCustomResourceLambdaFunctionArn # #################################################################################################### # #################################################################################################### # # RESOLVERS - Data Store # # #################################################################################################### # #################################################################################################### # RESOLVERS - QUERIES - Data Store - Get Data appSyncResolverQueryGetData: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: getData DataSourceName: !GetAtt appSyncDataSourceDataStoreDynamoDB.Name RequestMappingTemplate: | #set($thingName = ${ctx.args.thingName}) #set($startTimestamp = $util.time.nowEpochMilliSeconds()-${ctx.args.timeAgoInSecs}*1000) #set($ThingNameAndMetric = "${thingName}-${ctx.args.metricName}") { "version" : "2017-02-28", "operation" : "Query", "query" : { "expression" : "ThingNameAndMetric = :ThingNameAndMetric AND #t >= :startTimestamp", "expressionValues" : { ":ThingNameAndMetric": {"S": "${ThingNameAndMetric}"}, ":startTimestamp": {"N": $startTimestamp} }, "expressionNames": { "#t": "Timestamp" } } } ResponseMappingTemplate: | { "Data": $util.toJson($ctx.result.items) } # #################################################################################################### # #################################################################################################### # # RESOLVERS - Admin # # #################################################################################################### # #################################################################################################### # RESOLVERS - QUERIES - Admin - Get User appSyncResolverQueryGetUser: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: getUser DataSourceName: !GetAtt appSyncDataSourceAdminLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "getUser", "username": "${ctx.args.username}" } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - QUERIES - Admin - List Groups appSyncResolverQueryListGroups: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: listGroups DataSourceName: !GetAtt appSyncDataSourceAdminLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "listGroups" #if( ${context.arguments.limit} ) , "limit": ${context.arguments.limit} #end #if( ${context.arguments.nextToken} ) , "nextToken": "${context.arguments.nextToken}" #end } } ResponseMappingTemplate: | { "Groups": $util.toJson($ctx.result.Groups), #if( ${ctx.result.NextToken} ) "NextToken": "${ctx.result.NextToken}", #end } # RESOLVERS - QUERIES - Admin - List Users appSyncResolverQueryListUsers: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: listUsers DataSourceName: !GetAtt appSyncDataSourceAdminLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "listUsers" #if( ${context.arguments.limit} ) , "limit": ${context.arguments.limit} #end #if( ${context.arguments.paginationToken} ) , "paginationToken": "${context.arguments.paginationToken}" #end } } ResponseMappingTemplate: | { "Users": $util.toJson($ctx.result.Users), #if( ${ctx.result.PaginationToken} ) "PaginationToken": "${ctx.result.PaginationToken}", #end } # RESOLVERS - MUTATIONS - Admin - Delete User appSyncResolverMutationDeleteUser: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: deleteUser DataSourceName: !GetAtt appSyncDataSourceAdminLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "deleteUser", "username": "${context.arguments.username}" } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - MUTATIONS - Admin - Disable User appSyncResolverMutationDisableUser: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: disableUser DataSourceName: !GetAtt appSyncDataSourceAdminLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "disableUser", "username": "${context.arguments.username}" } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - MUTATIONS - Admin - Enable User appSyncResolverMutationEnableUser: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: enableUser DataSourceName: !GetAtt appSyncDataSourceAdminLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "enableUser", "username": "${context.arguments.username}" } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - MUTATIONS - Admin - Invite User appSyncResolverMutationInviteUser: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: inviteUser DataSourceName: !GetAtt appSyncDataSourceAdminLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "inviteUser", "name": "${context.arguments.name}", "email": "${context.arguments.email}", "groups": $util.toJson($context.arguments.groups) } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - MUTATIONS - Admin - Update User appSyncResolverMutationUpdateUser: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: updateUser DataSourceName: !GetAtt appSyncDataSourceAdminLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "updateUser", "username": "${context.arguments.username}", "groups": $util.toJson($context.arguments.groups) } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # #################################################################################################### # #################################################################################################### # # RESOLVERS - DeviceTypes # # #################################################################################################### # #################################################################################################### # RESOLVERS - QUERIES - DeviceTypes - List Device Types appSyncResolverQueryListDeviceTypes: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: listDeviceTypes DataSourceName: !GetAtt appSyncDataSourceDeviceTypesDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Scan", #if( $ctx.args.limit ) "limit": $ctx.args.limit, #end #if( ${ctx.args.nextToken} ) "nextToken": "${ctx.args.nextToken}" #end } ResponseMappingTemplate: | { "deviceTypes": $util.toJson($ctx.result.items), #if( ${ctx.result.nextToken} ) "nextToken": "${ctx.result.nextToken}", #end } # RESOLVERS - QUERIES - DeviceTypes - Get Device Type appSyncResolverQueryGetDeviceType: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: getDeviceType DataSourceName: !GetAtt appSyncDataSourceDeviceTypesDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "GetItem", "key" : { "id" : $util.dynamodb.toDynamoDBJson($ctx.args.id) } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - MUTATIONS - DeviceTypes - Add Device Type appSyncResolverMutationAddDeviceType: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: addDeviceType DataSourceName: !GetAtt appSyncDataSourceDeviceTypesDynamoDB.Name RequestMappingTemplate: | #set( $validTypes = ["UNKNOWN", "SDK", "A:FreeRTOS", "GREENGRASS", "THING_ONLY"] ) #set( $temp = "UNKNOWN" ) #foreach($t in $validTypes) #if($t == $ctx.args.type) #set( $temp = $t ) #end #end #set( $ctx.args.type = $temp ) #set( $values = $util.dynamodb.toMapValues($ctx.args) ) $!{values.put("createdAt", $util.dynamodb.toDynamoDB($util.time.nowISO8601()))} $!{values.put("updatedAt", $values.get("createdAt"))} { "version" : "2017-02-28", "operation" : "PutItem", "key" : { "id" : $util.dynamodb.toStringJson($util.autoId()) }, "attributeValues" : $util.toJson($values) } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - MUTATIONS - DeviceTypes - Delete Device Type appSyncResolverMutationDeleteDeviceType: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: deleteDeviceType DataSourceName: !GetAtt appSyncDataSourceDeviceTypesDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "DeleteItem", "key" : { "id" : $util.dynamodb.toStringJson($ctx.args.id) } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - MUTATIONS - DeviceTypes - Update Device Type appSyncResolverMutationUpdateDeviceType: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: updateDeviceType DataSourceName: !GetAtt appSyncDataSourceDeviceTypesDynamoDB.Name RequestMappingTemplate: | #set( $validTypes = ["UNKNOWN", "SDK", "A:FreeRTOS", "GREENGRASS", "THING_ONLY"] ) #set( $temp = "UNKNOWN" ) #foreach($t in $validTypes) #if($t == $ctx.args.type) #set( $temp = $t ) #end #end #set( $ctx.args.type = $temp ) #set( $expression = "SET #n = :n, #t = :t, updatedAt = :updatedAt" ) { "version" : "2017-02-28", "operation" : "UpdateItem", "key" : { "id" : $util.dynamodb.toStringJson($ctx.args.id) }, "update" : { "expressionValues": { ":n" : $util.dynamodb.toStringJson($ctx.args.name), ":t" : $util.dynamodb.toStringJson($ctx.args.type), ":updatedAt" : $util.dynamodb.toStringJson($util.time.nowISO8601()) #if($ctx.args.spec) #set( $expression = "$expression, spec = :spec" ) , ":spec" : $util.dynamodb.toMapJson($ctx.args.spec) #end }, "expression" : "$expression", "expressionNames": { "#n": "name", "#t": "type" } } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # #################################################################################################### # #################################################################################################### # # RESOLVERS - Settings # # #################################################################################################### # #################################################################################################### # RESOLVERS - QUERIES - Settings - Get Setting appSyncResolverQueryGetSetting: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: getSetting DataSourceName: !GetAtt appSyncDataSourceSettingsDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "GetItem", "key" : { "id" : $util.dynamodb.toDynamoDBJson($ctx.args.id) } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - MUTATIONS - Settings - Update Setting appSyncResolverMutationUpdateSetting: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: updateSetting DataSourceName: !GetAtt appSyncDataSourceSettingsDynamoDB.Name RequestMappingTemplate: | #set( $expression = "SET #t = :t, #uA = :uA" ) { "version" : "2017-02-28", "operation" : "UpdateItem", "key" : { "id" : $util.dynamodb.toStringJson($ctx.args.id) }, "update" : { "expressionValues": { #if( $ctx.args.setting ) #set( $expression = "$expression, setting = :setting" ) ":setting": $util.dynamodb.toMapJson($ctx.args.setting), #end ":t" : $util.dynamodb.toStringJson($ctx.args.type), ":uA" : $util.dynamodb.toStringJson($util.time.nowISO8601()) }, "expression" : "$expression", "expressionNames": { "#t": "type", "#uA": "updatedAt" } } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # #################################################################################################### # #################################################################################################### # # RESOLVERS - Just In Time On Boarding # # #################################################################################################### # #################################################################################################### # RESOLVERS - QUERIES - Just In Time On Boarding - Get State appSyncResolverQueryGetJustInTimeOnBoardingState: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: getJustInTimeOnBoardingState DataSourceName: !GetAtt appSyncDataSourceSettingsLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation": "Invoke", "payload": { "cmd": "getJustInTimeOnBoardingState", "arguments": $util.toJson($context.arguments) } } ResponseMappingTemplate: '$util.toJson($context.result)' # RESOLVERS - MUTATIONS - Just In Time On Boarding - Set State appSyncResolverMutationSetJustInTimeOnBoardingState: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: setJustInTimeOnBoardingState DataSourceName: !GetAtt appSyncDataSourceSettingsLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation": "Invoke", "payload": { "cmd": "setJustInTimeOnBoardingState", "enabled": $context.arguments.enabled } } ResponseMappingTemplate: '$util.toJson($context.result)' # #################################################################################################### # #################################################################################################### # # RESOLVERS - Device Blueprints # # #################################################################################################### # #################################################################################################### # RESOLVERS - QUERIES - Device Blueprints - List appSyncResolverQueryListDeviceBlueprints: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: listDeviceBlueprints DataSourceName: !GetAtt appSyncDataSourceDeviceBlueprintsDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Scan", #if( $ctx.args.limit ) "limit": $ctx.args.limit, #end #if( ${ctx.args.nextToken} ) "nextToken": "${ctx.args.nextToken}" #end } ResponseMappingTemplate: | { "deviceBlueprints": $util.toJson($ctx.result.items), #if( ${ctx.result.nextToken} ) "nextToken": "${ctx.result.nextToken}", #end } # RESOLVERS - QUERIES - Device Blueprints - Get appSyncResolverQueryGetDeviceBlueprint: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: getDeviceBlueprint DataSourceName: !GetAtt appSyncDataSourceDeviceBlueprintsDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "GetItem", "key" : { "id" : $util.dynamodb.toDynamoDBJson($ctx.args.id) } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - MUTATIONS - Device Blueprints - Add appSyncResolverMutationAddDeviceBlueprint: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: addDeviceBlueprint DataSourceName: !GetAtt appSyncDataSourceDeviceBlueprintsDynamoDB.Name RequestMappingTemplate: | #set( $validTypes = ["UNKNOWN", "SDK", "A:FreeRTOS", "GREENGRASS", "THING_ONLY"] ) #set( $temp = "UNKNOWN" ) #foreach($t in $validTypes) #if($t == $ctx.args.type) #set( $temp = $t ) #end #end #set( $ctx.args.type = $temp ) #set( $values = $util.dynamodb.toMapValues($ctx.args) ) $!{values.put("createdAt", $util.dynamodb.toDynamoDB($util.time.nowISO8601()))} $!{values.put("updatedAt", $values.get("createdAt"))} { "version" : "2017-02-28", "operation" : "PutItem", "key" : { "id" : $util.dynamodb.toStringJson($util.autoId()) }, "attributeValues" : $util.toJson($values) } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - MUTATIONS - Device Blueprints - Delete appSyncResolverMutationDeleteDeviceBlueprint: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: deleteDeviceBlueprint DataSourceName: !GetAtt appSyncDataSourceDeviceBlueprintsDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "DeleteItem", "key" : { "id" : $util.dynamodb.toStringJson($ctx.args.id) } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - MUTATIONS - Device Blueprints - Update appSyncResolverMutationUpdateDeviceBlueprint: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: updateDeviceBlueprint DataSourceName: !GetAtt appSyncDataSourceDeviceBlueprintsDynamoDB.Name RequestMappingTemplate: | #set( $validTypes = ["UNKNOWN", "SDK", "A:FreeRTOS", "GREENGRASS", "THING_ONLY"] ) #set( $temp = "UNKNOWN" ) #foreach($t in $validTypes) #if($t == $ctx.args.type) #set( $temp = $t ) #end #end #set( $ctx.args.type = $temp ) #set( $expression = "SET #n = :n, #t = :t, #uA = :uA" ) { "version" : "2017-02-28", "operation" : "UpdateItem", "key" : { "id" : $util.dynamodb.toStringJson($ctx.args.id) }, "update" : { "expressionValues": { #if( $ctx.args.compatibility ) ## Add compatibility to expression #set( $expression = "$expression, compatibility = :compatibility" ) ":compatibility" : $util.dynamodb.toListJson($ctx.args.compatibility), #end #if( $ctx.args.deviceTypeMappings ) ## Add deviceTypeMappings to expression #set( $expression = "$expression, deviceTypeMappings = :deviceTypeMappings" ) ":deviceTypeMappings" : $util.dynamodb.toListJson($ctx.args.deviceTypeMappings), #end #if( $ctx.args.spec ) ## Add spec to expression #set( $expression = "$expression, spec = :spec" ) ":spec" :$util.dynamodb.toMapJson($ctx.args.spec), #end ":n" : $util.dynamodb.toStringJson($ctx.args.name), ":t" : $util.dynamodb.toStringJson($ctx.args.type), ":uA" : $util.dynamodb.toStringJson($util.time.nowISO8601()) }, "expression" : "$expression", "expressionNames": { "#n": "name", "#t": "type", "#uA": "updatedAt" } } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # #################################################################################################### # #################################################################################################### # # RESOLVERS - Devices # # #################################################################################################### # #################################################################################################### # RESOLVERS - QUERIES - Devices - List appSyncResolverQueryListDevices: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: listDevices DataSourceName: !GetAtt appSyncDataSourceDevicesDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Scan", #if( $ctx.args.limit ) "limit": $ctx.args.limit, #end #if( ${ctx.args.nextToken} ) "nextToken": "${ctx.args.nextToken}" #end } ResponseMappingTemplate: | { "devices": $util.toJson($ctx.result.items), #if( ${ctx.result.nextToken} ) "nextToken": "${ctx.result.nextToken}", #end } # RESOLVERS - QUERIES - Devices - List Devices for a given Device Type appSyncResolverQueryListDevicesOfDeviceType: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: listDevicesOfDeviceType DataSourceName: !GetAtt appSyncDataSourceDevicesDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Query", "index" : "deviceTypeId", #if( $ctx.args.limit ) "limit": $ctx.args.limit, #end #if( ${ctx.args.nextToken} ) "nextToken": "${ctx.args.nextToken}", #end "query" : { "expression" : "deviceTypeId = :deviceTypeId", "expressionValues" : { ":deviceTypeId" : $util.dynamodb.toDynamoDBJson($ctx.args.deviceTypeId) } } } ResponseMappingTemplate: | { "devices": $util.toJson($ctx.result.items), #if( ${ctx.result.nextToken} ) "nextToken": "${ctx.result.nextToken}", #end } # RESOLVERS - QUERIES - Devices - List Devices with Device Blueprints appSyncResolverQueryListDevicesWithDeviceBlueprint: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: listDevicesWithDeviceBlueprint DataSourceName: !GetAtt appSyncDataSourceDevicesDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Query", "index" : "deviceBlueprintId", #if( $ctx.args.limit ) "limit": $ctx.args.limit, #end #if( ${ctx.args.nextToken} ) "nextToken": "${ctx.args.nextToken}", #end "query" : { "expression" : "deviceBlueprintId = :deviceBlueprintId", "expressionValues" : { ":deviceBlueprintId" : $util.dynamodb.toDynamoDBJson($ctx.args.deviceBlueprintId) } } } ResponseMappingTemplate: | { "devices": $util.toJson($ctx.result.items), #if( ${ctx.result.nextToken} ) "nextToken": "${ctx.result.nextToken}", #end } # RESOLVERS - QUERIES - Devices - Get appSyncResolverQueryGetDevice: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: getDevice DataSourceName: !GetAtt appSyncDataSourceDevicesDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "GetItem", "key" : { "thingId" : $util.dynamodb.toDynamoDBJson($ctx.args.thingId) } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - QUERIES - Devices - Get Device Stats appSyncResolverQueryGetDeviceStats: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: getDeviceStats DataSourceName: !GetAtt appSyncDataSourceDevicesLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "getDeviceStats" } } ResponseMappingTemplate: '$util.toJson($context.result)' # RESOLVERS - MUTATIONS - Devices - Add appSyncResolverMutationAddDevice: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: addDevice DataSourceName: !GetAtt appSyncDataSourceDevicesLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "addDevice", "name": "$context.arguments.name", "deviceTypeId": "$context.arguments.deviceTypeId", "deviceBlueprintId": "$context.arguments.deviceBlueprintId" } } ResponseMappingTemplate: '$util.toJson($context.result)' # RESOLVERS - MUTATIONS - Devices - Delete appSyncResolverMutationDeleteDevice: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: deleteDevice DataSourceName: !GetAtt appSyncDataSourceDevicesLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "deleteDevice", "thingId": "$context.arguments.thingId" } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - MUTATIONS - Devices - Update appSyncResolverMutationUpdateDevice: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: updateDevice DataSourceName: !GetAtt appSyncDataSourceDevicesLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "updateDevice", "thingId": "$context.arguments.thingId", "name": "$context.arguments.name", "spec": $util.toJson($context.arguments.spec), "deviceTypeId": "$context.arguments.deviceTypeId", "deviceBlueprintId": "$context.arguments.deviceBlueprintId" } } ResponseMappingTemplate: '$util.toJson($context.result)' # RESOLVERS - MUTATIONS - Devices - Create Certificate appSyncResolverMutationCreateCertificate: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: createCertificate DataSourceName: !GetAtt appSyncDataSourceDevicesLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "createCertificate", "csr": "$util.escapeJavaScript($context.arguments.csr)", "thingId": "$context.arguments.thingId" } } ResponseMappingTemplate: '$util.toJson($context.result)' # #################################################################################################### # #################################################################################################### # # RESOLVERS - Deployments # # #################################################################################################### # #################################################################################################### # RESOLVERS - QUERIES - Deployments - List appSyncResolverQueryListDeployments: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: listDeployments DataSourceName: !GetAtt appSyncDataSourceDeploymentsDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Scan", #if( $ctx.args.limit ) "limit": $ctx.args.limit, #end #if( ${ctx.args.nextToken} ) "nextToken": "${ctx.args.nextToken}" #end } ResponseMappingTemplate: | { "deployments": $util.toJson($ctx.result.items), #if( ${ctx.result.nextToken} ) "nextToken": "${ctx.result.nextToken}", #end } # RESOLVERS - MUTATIONS - Deployments - Add appSyncResolverMutationAddDeployment: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: addDeployment DataSourceName: !GetAtt appSyncDataSourceDeploymentsLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "addDeployment", "thingId": "$context.arguments.thingId" } } ResponseMappingTemplate: '$util.toJson($context.result)' # #################################################################################################### # #################################################################################################### # # RESOLVERS - Systems # # #################################################################################################### # #################################################################################################### # RESOLVERS - QUERIES - Systems - List appSyncResolverQueryListSystems: Type: 'AWS::AppSync::Resolver' Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: 'listSystems' DataSourceName: !GetAtt appSyncDataSourceSystemsDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Scan", #if( $ctx.args.limit ) "limit": $ctx.args.limit, #end #if( ${ctx.args.nextToken} ) "nextToken": "${ctx.args.nextToken}" #end } ResponseMappingTemplate: | { "systems": $util.toJson($ctx.result.items), #if( ${ctx.result.nextToken} ) "nextToken": "${ctx.result.nextToken}", #end } # RESOLVERS - QUERIES - Systems - Get appSyncResolverQueryGetSystem: Type: 'AWS::AppSync::Resolver' Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: 'getSystem' DataSourceName: !GetAtt appSyncDataSourceSystemsDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "GetItem", "key" : { "id" : $util.dynamodb.toDynamoDBJson($ctx.args.id) } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - QUERIES - Systems - Get Stats appSyncResolverQueryGetSystemStats: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: getSystemStats DataSourceName: !GetAtt appSyncDataSourceSystemsLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "getSystemStats" } } ResponseMappingTemplate: '$util.toJson($context.result)' # RESOLVERS - MUTATIONS - Systems - Add appSyncResolverMutationAddSystem: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: addSystem DataSourceName: !GetAtt appSyncDataSourceSystemsDynamoDB.Name RequestMappingTemplate: | #set( $values = $util.dynamodb.toMapValues($ctx.args) ) $!{values.put("createdAt", $util.dynamodb.toDynamoDB($util.time.nowISO8601()))} $!{values.put("updatedAt", $values.get("createdAt"))} { "version" : "2017-02-28", "operation" : "PutItem", "key" : { "id" : $util.dynamodb.toStringJson($util.autoId()) }, "attributeValues" : $util.toJson($values) } ResponseMappingTemplate: '$util.toJson($context.result)' # RESOLVERS - MUTATIONS - Systems - Delete appSyncResolverMutationDeleteSystem: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: deleteSystem DataSourceName: !GetAtt appSyncDataSourceSystemsDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "DeleteItem", "key" : { "id" : $util.dynamodb.toStringJson($ctx.args.id) } } ResponseMappingTemplate: '$util.toJson($context.result)' # RESOLVERS - MUTATIONS - Systems - Update appSyncResolverMutationUpdateSystem: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: updateSystem DataSourceName: !GetAtt appSyncDataSourceSystemsDynamoDB.Name RequestMappingTemplate: | #set( $expression = "SET #n = :n, description = :d, deviceIds = :deviceIds, updatedAt = :updatedAt" ) { "version" : "2017-02-28", "operation" : "UpdateItem", "key" : { "id" : $util.dynamodb.toStringJson($ctx.args.id) }, "update" : { "expressionValues": { ":n" : $util.dynamodb.toStringJson($ctx.args.name), ":d" : $util.dynamodb.toStringJson($ctx.args.description), ":updatedAt" : $util.dynamodb.toStringJson($util.time.nowISO8601()), ":deviceIds" : $util.dynamodb.toListJson($ctx.args.deviceIds) }, "expression" : "$expression", "expressionNames": { "#n": "name" } } } ResponseMappingTemplate: '$util.toJson($context.result)' # RESOLVERS - MUTATIONS - Systems - Refresh appSyncResolverMutationRefreshSystem: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: refreshSystem DataSourceName: !GetAtt appSyncDataSourceSystemsLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "cmd": "refreshSystem", "id": "$context.arguments.id" } } ResponseMappingTemplate: '$util.toJson($context.result)' # #################################################################################################### # #################################################################################################### # # RESOLVERS - System Blueprints # # #################################################################################################### # #################################################################################################### # RESOLVERS - QUERIES - System Blueprints - List appSyncResolverQueryListSystemBlueprints: Type: 'AWS::AppSync::Resolver' Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: 'listSystemBlueprints' DataSourceName: !GetAtt appSyncDataSourceSystemBlueprintsDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Scan", #if( $ctx.args.limit ) "limit": $ctx.args.limit, #end #if( ${ctx.args.nextToken} ) "nextToken": "${ctx.args.nextToken}" #end } ResponseMappingTemplate: | { "systemBlueprints": $util.toJson($ctx.result.items), #if( ${ctx.result.nextToken} ) "nextToken": "${ctx.result.nextToken}", #end } # RESOLVERS - QUERIES - System Blueprints - Get appSyncResolverQueryGetSystemBlueprint: Type: 'AWS::AppSync::Resolver' Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: 'getSystemBlueprint' DataSourceName: !GetAtt appSyncDataSourceSystemBlueprintsDynamoDB.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "GetItem", "key" : { "id" : $util.dynamodb.toDynamoDBJson($ctx.args.id) } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # RESOLVERS - MUTATIONS - System Blueprints - Update appSyncResolverMutationUpdateSystemBlueprint: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: updateSystemBlueprint DataSourceName: !GetAtt appSyncDataSourceSystemBlueprintsDynamoDB.Name RequestMappingTemplate: | #set( $expression = "SET #n = :n, #d = :d, #p = :p, #uA = :uA" ) { "version" : "2017-02-28", "operation" : "UpdateItem", "key" : { "id" : $util.dynamodb.toStringJson($ctx.args.id) }, "update" : { "expressionValues": { #if( $ctx.args.spec ) ## Add spec to expression #set( $expression = "$expression, spec = :spec" ) ":spec" :$util.dynamodb.toMapJson($ctx.args.spec), #end ":n" : $util.dynamodb.toStringJson($ctx.args.name), ":d" : $util.dynamodb.toStringJson($ctx.args.description), ":p" : $util.dynamodb.toStringJson($ctx.args.prefix), ":uA" : $util.dynamodb.toStringJson($util.time.nowISO8601()) }, "expression" : "$expression", "expressionNames": { "#n": "name", "#d": "description", "#p": "prefix", "#uA": "updatedAt" } } } ResponseMappingTemplate: '$util.toJson($ctx.result)' # #################################################################################################### # #################################################################################################### # # RESOLVERS - Utils # # #################################################################################################### # #################################################################################################### # RESOLVERS - QUERIES - Utils - Describe Endpoint appSyncResolverQueryDescribeEndpoint: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: describeEndpoint DataSourceName: !GetAtt appSyncDataSourceUtilsLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "RequestType": "Utils", "cmd": "describeEndpoint", "endpointType": "$context.arguments.endpointType" } } ResponseMappingTemplate: '$util.toJson($context.result)' # RESOLVERS - QUERIES - Utils - Get Thing Shadow appSyncResolverQueryGetThingShadow: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: getThingShadow DataSourceName: !GetAtt appSyncDataSourceUtilsLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "RequestType": "Utils", "cmd": "iotdata.getThingShadow", "params": $util.toJson($context.arguments.params) } } ResponseMappingTemplate: '$util.toJson($context.result)' # RESOLVERS - QUERIES - Utils - S3 list Objects appSyncResolverQueryS3ListObjectsV2: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Query FieldName: s3ListObjectsV2 DataSourceName: !GetAtt appSyncDataSourceUtilsLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "RequestType": "Utils", "cmd": "s3.listObjectsV2", "params": $util.toJson($context.arguments.params) } } ResponseMappingTemplate: '$util.toJson($context.result)' # RESOLVERS - MUTATIONS - Utils - Attach Principal Policy appSyncResolverMutationAttachPrincipalPolicy: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: attachPrincipalPolicy DataSourceName: !GetAtt appSyncDataSourceUtilsLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "RequestType": "Utils", "cmd": "attachPrincipalPolicy", "policyName": "$context.arguments.policyName", "principal": "$context.arguments.principal" } } ResponseMappingTemplate: '$util.toJson($context.result)' # RESOLVERS - MUTATIONS - Utils - Update Thing Shadow appSyncResolverMutationUpdateThingShadow: Type: 'AWS::AppSync::Resolver' DependsOn: appSyncSchema Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId TypeName: Mutation FieldName: updateThingShadow DataSourceName: !GetAtt appSyncDataSourceUtilsLambda.Name RequestMappingTemplate: | { "version" : "2017-02-28", "operation" : "Invoke", "payload": { "RequestType": "Utils", "cmd": "iotdata.updateThingShadow", "params": $util.toJson($context.arguments.params) } } ResponseMappingTemplate: '$util.toJson($context.result)' # SCHEMA appSyncSchema: Type: 'AWS::AppSync::GraphQLSchema' Properties: ApiId: !GetAtt appSyncGraphQLApi.ApiId DefinitionS3Location: !Join ['/', ['s3:/', !Ref sourceBucket, !Ref sourceKeyPrefix, 'sputnik-appsync-schema.gql']] Outputs: appSyncGraphQLApi: Description: 'sputnik AppSync API' Value: !Ref appSyncGraphQLApi appSyncGraphQLUrl: Description: 'sputnik AppSync Endpoint URL' Value: !GetAtt appSyncGraphQLApi.GraphQLUrl