Intro to Pipeline Resolvers for AppSync Implementation

2023-03-01

Intro to Pipeline Resolvers for AppSync Implementation

This is a series of articles about setting up a complex Serverless backend infrastructure with AWS SAM and CloudFormation.

Here is the index of all the articles in case you want to jump to any of them:

1. Setup of AppSync and API Gateway with Multiple AWS Cognito User Pools

2. Configuring S3 Buckets with Permissions and Access Roles in AWS Cognito AuthRole

3. Intro to DynamoDB Resolvers for AppSync Implementation

4. Intro to Lambda Resolvers for AppSync Implementation

5. Configuring an AWS VPC to Include Lambda Resolvers with a Fixed IP

6. Intro to Pipeline Resolvers for AppSync Implementation

7. Handling Lambda Resolver Timeouts with SNS Messages

Introduction

AppSync's pipeline resolvers offer a way to chain multiple operations sequentially. This guide will explore how to use pipeline resolvers.

Why Pipeline Resolvers?

Pipeline resolvers allow developers to break down complex operations into smaller, more manageable steps. Each step can directly map a data source or a Lambda function.

This approach helps decouple different pieces of code to avoid having functions that do several things. For example, you can have a Pipeline Resolver that:

  1. Verifies the user role to verify that they are authorized to perform the current operation
  2. Log the request to an audit/monitoring tool.
  3. Execute the actual operation.

The Code

Setting Up Pipeline Resolvers

  1. Create the functions: following previous examples
  2. Create the new Resolver: setting the `kind` property to "PIPELINE" and adding the three functions under the PipelineConfig property of the resolver resource.

Resources:

  # ... [Previous resources from the template]

  # UpdateUserFunction
  UpdateUserFunction:
    Type: "AWS::AppSync::FunctionConfiguration"
    Properties:
      ApiId: !GetAtt AppSyncAPI.ApiId
      Name: "UpdateUserFunction"
      DataSourceName: !GetAtt PipelineFunctionDataSource.Name
      FunctionVersion: "2018-05-29"
      RequestMappingTemplate: |
        {
          "version": "2018-05-29",
          "operation": "Invoke",
          "payload": {
            "operation": "updateUser",
            "args": $util.toJson($context.args)
          }
        }
      ResponseMappingTemplate: "$util.toJson($context.result)"

  # Pipeline Resolver for updateUser
  UpdateUserResolver:
    Type: "AWS::AppSync::Resolver"
    Properties:
      ApiId: !GetAtt AppSyncAPI.ApiId
      TypeName: "Mutation"
      FieldName: "updateUser"
      Kind: "PIPELINE"
      PipelineConfig:
        Functions:
          - !GetAtt VerifyUserRoleFunction.FunctionId
          - !GetAtt LogActionFunction.FunctionId
          - !GetAtt UpdateUserFunction.FunctionId
      RequestMappingTemplate: "{}" # No-op since the logic is in the functions
      ResponseMappingTemplate: "$util.toJson($context.prev.result)"

  # ... [Other resources like IAM roles, AppSync API, etc.]

Conclusion

Pipeline resolvers in AppSync provide a structured approach to handling complex operations. By chaining multiple steps, developers can ensure data integrity, enhance security, and improve operational efficiency.

Please note that the code examples have been simplified to help understand the approach and the resources. Some adjustments might be needed.

---------

Next UP: Part 7. Handling Lambda Resolver Timeouts with SNS Messages