This sample project demonstrates how to build a serverless GraphQL API using AWS AppSync with .NET Lambda functions as direct resolvers. The application is a simple Todo management system that showcases best practices for implementing AWS AppSync APIs with .NET Lambda functions.
The solution uses the following AWS services:
- AWS AppSync: Provides the GraphQL API interface
- AWS Lambda (.NET 8): Implements the business logic as direct resolvers
- Amazon DynamoDB: Stores the Todo items
- AWS CDK: Defines and deploys the infrastructure as code
TodoApp/
├── src/
│ ├── TodoApp.Api/ # Lambda function code
│ │ ├── Entity/ # DynamoDB entity models
│ │ │ └── TodoItemEntity.cs # Todo item entity for DynamoDB
│ │ ├── GraphQLTypes/ # GraphQL type definitions
│ │ │ ├── CreateTodoItem.cs # Input type for creating todos
│ │ │ ├── Todo.cs # Todo type definition
│ │ │ └── UpdateTodoItem.cs # Input type for updating todos
│ │ ├── Repository/ # Data access layer
│ │ │ ├── ITodoRepository.cs # Repository interface
│ │ │ └── TodoRepository.cs # DynamoDB repository implementation
│ │ ├── Services/ # Business logic layer
│ │ │ ├── ITodoService.cs # Service interface
│ │ │ ├── TodoMapper.cs # Entity to DTO mapping
│ │ │ └── TodoService.cs # Service implementation
│ │ ├── Functions.cs # Lambda function handlers
│ │ ├── Startup.cs # Dependency injection setup
│ │ ├── TodoApp.Api.csproj # Project file
│ │
│ ├── TodoApp.AspireHost/ # .NET Aspire host project
│ │ ├── Program.cs # Aspire host configuration
│ │
│ └── TodoApp.Cdk/ # CDK Infrastructure code
│ ├── graphql/ # GraphQL schema files
│ │ └── schema.graphql # GraphQL schema definition
│ ├── AppSyncApiStack.cs # Main CDK stack with AppSync and Lambda resources
│ ├── Program.cs # CDK app entry point
│
├── tests/
│ └── TodoApp.Tests/ # Unit tests
│ ├── TodoRepositoryTests.cs # Repository tests
│ ├── TodoServiceTests.cs # Service tests
-
AppSyncApiStack.cs
: Contains the CDK infrastructure definition including:- AppSync GraphQL API configuration
- DynamoDB table definition
- Lambda function definitions with appropriate IAM permissions
- Direct Lambda resolver mappings
-
graphql/schema.graphql
: Defines the GraphQL schema with queries and mutations -
Functions.cs
: Contains the Lambda function handlers that implement the GraphQL resolvers -
TodoService.cs
: Implements the business logic for managing Todo items
The API supports the following operations:
getTodoById(id: ID!)
: Retrieve a specific Todo item by IDlistTodos
: Retrieve all Todo items
createTodo(title: String!, description: String)
: Create a new Todo itemupdateTodo(id: ID!, title: String!, description: String, completed: Boolean!)
: Update an existing Todo itemdeleteTodo(id: ID!)
: Delete a Todo item
- .NET 8 SDK installed
- AWS CDK CLI installed (
npm install -g aws-cdk
) - AWS CLI installed and configured with appropriate credentials
- AWS Lambda .NET Global Tool installed (
dotnet tool install -g Amazon.Lambda.Tools
) - An AWS account with permissions to create the required resources
git clone https://github.com/aws-samples/sample-appsync-dotnet-lambda-resolvers.git
cd sample-appsync-dotnet-lambda-resolvers
cd src/TodoApp.Api
dotnet lambda package
cd ../..
cdk bootstrap # Only needed the first time you use CDK in an account/region
cdk deploy
The deployment will output the GraphQL API URL and API Key that you can use to interact with your API.
You can test the API using the AWS AppSync Console or any GraphQL client like Postman or Insomnia.
You can test your API using popular API clients like Thunder Client or Postman:
-
Configure your request headers:
x-api-key
: Your AppSync API keyContent-Type
: application/json
-
Set the endpoint URL to your AppSync API URL
-
Write your GraphQL queries or mutations in the request body
-
Send the request and examine the response
query ListTodos {
listTodos {
id
title
description
completed
createdAt
updatedAt
}
}
query GetTodo {
getTodoById(id: "your-todo-id") {
id
title
description
completed
createdAt
updatedAt
}
}
mutation CreateTodo {
createTodo(
title: "Complete AWS AppSync sample"
description: "Finish the AppSync with .NET Lambda sample project"
) {
id
title
description
completed
createdAt
}
}
The project implements several security best practices:
- Least Privilege Permissions: Each Lambda function has only the permissions it needs
- API Key Authentication: The GraphQL API is protected with an API key
- Resource Policies: Resources are configured with appropriate access controls
This project includes a .NET Aspire host project (TodoApp.AspireHost
) that allows you to test your Lambda functions locally.
-
Navigate to the Aspire host project directory:
cd src/TodoApp.AspireHost
-
Run the Aspire host project:
dotnet run
-
This will launch the .NET Aspire dashboard in your browser, where you can:
- Monitor your local Lambda function executions
- View logs and performance metrics
- Test your Lambda functions with sample events
For more detailed information about building Lambda functions with .NET Aspire, refer to the AWS blog post: Building Lambda with Aspire
To avoid incurring future charges, delete the stack:
cdk destroy
- Amazon.Lambda.AppSyncEvents NuGet package
- AWS AppSync Developer Guide
- AWS Lambda .NET Developer Guide
- AWS CDK Developer Guide
- AWS Lambda Annotations for .NET
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.