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.