Build AWS AppSync API and API Client using Python

   Go back to the Task List

  « 4. Create Lambda Function    6. Test AppSync Queries »

5. Configure AppSync Data Source

The AppSync API with schema and the backend Lambda function both are ready. Time to tie them together.

  1. Goto AWS AppSync console, open dojoapi details. Click on the Data Sources menu in the left and then click on the Create data source button.

    AWS AppSync

  2. On the next screen, type in dojodatasource as the data source name. Select AWS Lambda Function for the data source type. Select EU-WEST-1 as the region. Select dojoworklamda function ARN. Finally choose New Role option and click on the Create button.

    AWS AppSync

  3. The datasource is ready. You now attach the data source to the methods in the schema. Click on the Schema menu option in the left and click on the Attach button next to addTask(…): String in the Resolvers section.

    AWS AppSync

  4. On the next screen, select dojodatasource as the data source. Enable both options Enable request mapping template and Enable response mapping template checkboxes. In the Configure the request mapping template field, copy-paste the configuration shown below.

    AWS AppSync

    {
      "version" : "2017-02-28",
      "operation": "Invoke",
      "payload": {
            "method": "addTask",
            "arguments":  $utils.toJson($context.arguments)
        }
    }
    

    `

  5. The configuration above, the addTask method would invoke the Lambda function and would pass two fields in the parameters - method and arguments. Remember when you defined the Lambda function. The event parameter in the Lambda function expected to carry two fields - method and arguments. This is the place where you are configuring for these two field values. The event parameter is populated with the value addTask. The arguments parameter is populated from the query context which will have fields id and description. For response, the Lambda function result is returned.

  6. Click on the Save Resolver button to save the resolver configuration for the addTask method.

  7. You need to repeat the same for the getTask method. Click on the Schema menu option in the left and click on the Attach button next to getTask(…): String in the Resolvers section.

    AWS AppSync

  8. On the next screen, select dojodatasource as the data source. Enable both options Enable request mapping template and Enable response mapping template checkboxes. In the Configure the request mapping template field, copy-paste the configuration shown below.

    AWS AppSync

    {
      "version" : "2017-02-28",
      "operation": "Invoke",
      "payload": {
            "method": "getTask",
            "arguments":  $utils.toJson($context.arguments)
        }
    }
    

    `

  9. The configuration above, the getTask method would invoke the Lambda function and would pass two fields in the parameters - method and arguments. The event parameter is populated with the value getTask. The arguments parameter is populated from the query context which will have id field. For response, the Lambda function result is returned.

  10. Click on the Save Resolver button to save the resolver configuration for the getTask method.

  11. The resolver has attached the backend Lambda function to the API and schema. API is ready - time to test it.