Skip to main content

AWS AppSync

Core concept: AppSync is a fully managed GraphQL API service โ€” ideal for real-time, multi-data-source, and mobile/web apps.


AppSync vs API Gatewayโ€‹

FeatureAppSyncAPI Gateway
ProtocolGraphQLREST / HTTP / WebSocket
Real-timeโœ… Native subscriptions (WebSocket)WebSocket API only
Multiple data sourcesโœ… Single query, multiple sourcesโŒ One integration per endpoint
Offline syncโœ… Conflict resolution built-inโŒ
Cachingโœ… Server-side per-resolverโœ… Stage-level
Use caseMobile/web apps, real-time dashboardsREST APIs, microservices

Data Sourcesโ€‹

SourceDescription
DynamoDBDirect DynamoDB access via VTL mapping
LambdaAny custom logic, any backend
RDS / Aurora ServerlessSQL queries via Data API
OpenSearchFull-text search
HTTPAny REST API
NoneLocal resolvers (computed values)

Resolversโ€‹

Resolvers map GraphQL operations to data source operations using VTL (Velocity Template Language) or JavaScript (APPSYNC_JS):

// JavaScript resolver โ€” DynamoDB GetItem
export function request(ctx) {
return {
operation: 'GetItem',
key: util.dynamodb.toMapValues({ id: ctx.args.id })
};
}

export function response(ctx) {
return ctx.result;
}

Pipeline Resolversโ€‹

Chain multiple data sources in one resolver:

Query: getOrderWithProducts
โ†’ Step 1: DynamoDB (get order)
โ†’ Step 2: DynamoDB (get product details)
โ†’ Step 3: Lambda (calculate price with tax)
โ†’ Return merged result

Real-Time Subscriptionsโ€‹

# Schema
type Subscription {
onOrderUpdated(customerId: ID!): Order
@aws_subscribe(mutations: ["updateOrder"])
}

# Client subscribes via WebSocket
subscription {
onOrderUpdated(customerId: "CUST-123") {
orderId
status
updatedAt
}
}
  • Client connects over WebSocket (MQTT over WebSocket)
  • AppSync pushes updates automatically when mutation fires

Authorization Modesโ€‹

ModeUse Case
API KeyPublic APIs, development
AWS IAMServer-to-server, IAM roles
Cognito User PoolsUser authentication (most common)
OIDCThird-party identity providers
Lambda AuthorizerCustom auth logic

Multiple modes can be configured simultaneously, with one as default.


Cachingโ€‹

Per-resolver TTL: 0 โ€“ 3600 seconds
Cache key: resolved query arguments

Enables: Cache frequently-requested data
Reduces: Lambda invocations, DynamoDB reads

๐ŸŽฏ DVA-C02 Exam Tipsโ€‹

Quick Exam Rules
  • AppSync vs API Gateway: If you need real-time WebSocket Subscriptions natively tied to data changes, choose AppSync. If you need simple REST API microservices, choose API Gateway.
  • Data Source unification: AppSync allows a single GraphQL endpoint to aggregate data from DynamoDB, RDS, and Lambda simultaneously.
  • Resolvers: Use Pipeline Resolvers to sequence operations across multiple data sources.

๐Ÿงช Practice Questionsโ€‹

Q1. A mobile app needs real-time order status updates pushed to connected clients whenever an order is updated. Which service is BEST suited?

A) API Gateway WebSocket API
B) SQS + client polling
C) AppSync with GraphQL Subscriptions
D) SNS mobile push notifications

โœ… Answer & Explanation

C โ€” AppSync Subscriptions automatically push mutation events to connected WebSocket clients โ€” purpose-built for this pattern. API Gateway WebSocket requires managing connection IDs and routing manually.


Q2. A single GraphQL query needs to aggregate data from DynamoDB and call a Lambda for business logic. Which AppSync feature enables this without client-side orchestration?

A) Multiple AppSync APIs
B) Pipeline Resolver chaining DynamoDB and Lambda functions
C) Lambda resolver that calls DynamoDB
D) REST API Gateway passthrough

โœ… Answer & Explanation

B โ€” Pipeline Resolvers chain multiple resolver functions (each connecting to a different data source) into a single GraphQL operation. The client makes one request; AppSync orchestrates the steps.


๐Ÿ”— Resourcesโ€‹