Unify data silos with AWS AppSync

Silos

What’s GraphQL?

What’s AppSync?

Data Sources

DynamoDB

Lambda


import json
print(‘Loading function’)def lambda_handler(event, context):
print (json.dumps(event))
print (context)

vehicle_id={}
vehicle_id=event[‘source’][‘vehicle_id’]
print(vehicle_id)

vehicles = {
“123456” : { “vehicle_id” : “123456”, “fuel” : “electric”, “category”: “SUV” },
“987654321” : { “vehicle_id” : “987654321”, “fuel”: “hybrid”, “category”: “Saloon”}
}

print(vehicles[vehicle_id])
return (vehicles[vehicle_id])

RDS (Aurora PostgreSQL)


create table accident (
vehicle_id varchar,
accident_date date,
damage varchar,
cost integer);
insert into accident values (123456, ‘2020–11–23 18:00:00’, ‘windscreen smashed’, 100);
insert into accident values (987654321, ‘2020–11–24 18:00:00’, ‘dent in front passenger door’, 600);
commit;
{
“username”: “xxxxxxxxxxxxxx”,
“password”: “xxxxxxxxxxxxxx”
}

aws secretsmanager create-secret — name HttpRDSSecret — secret-string file://creds.json — region eu-west-1

Create The GraphQL API

Schema


type Query {
#Get a single vehicle.
singleVehicle(vehicle_id: String): Vehicle
}
type Vehicle {
vehicle_id: String
model: String
year: String
colour: String
make: String
fuel: String
category: String
accident_date: String
accident_damage: String
accident_cost: String
}
schema {
query: Query
}

Data Sources

Resolvers

DynamoDB


{
“version”: “2017–02–28”,
“operation”: “GetItem”,
“key”: {
“vehicle_id”: $util.dynamodb.toDynamoDBJson($ctx.args.vehicle_id),
}

## Pass back the result from DynamoDB. **
$util.toJson($ctx.result)

Lambda


$util.toJson($context.result.get(“fuel”))


$util.toJson($context.result.get(“category”))

RDS


{
“version”: “2018–05–29”,
“statements”: [
$util.toJson(“select accident_date from accident WHERE vehicle_id = ‘$ctx.source.vehicle_id’”)
]
}

#if($ctx.error)
$util.error($ctx.error.message, $ctx.error.type)
#end
#set($output = $utils.rds.toJsonObject($ctx.result)[0])
## Make sure to handle instances where fields are null
## or don’t exist according to your business logic
#foreach( $item in $output )
#set($accident_date = $item.get(‘accident_date’))
#end
$util.toJson($accident_date)


{
“version”: “2018–05–29”,
“statements”: [
$util.toJson(“select damage from accident WHERE vehicle_id = ‘$ctx.source.vehicle_id’”)
]
}

#if($ctx.error)
$util.error($ctx.error.message, $ctx.error.type)
#end
#set($output = $utils.rds.toJsonObject($ctx.result)[0])
## Make sure to handle instances where fields are null
## or don’t exist according to your business logic
#foreach( $item in $output )
#set($damage = $item.get(‘damage’))
#end
$util.toJson($damage)

{
“version”: “2018–05–29”,
“statements”: [
$util.toJson(“select cost from accident WHERE vehicle_id = ‘$ctx.source.vehicle_id’”)
]
}

#if($ctx.error)
$util.error($ctx.error.message, $ctx.error.type)
#end
#set($output = $utils.rds.toJsonObject($ctx.result)[0])
## Make sure to handle instances where fields are null
## or don’t exist according to your business logic
#foreach( $item in $output )
#set($cost = $item.get(‘cost’))
#end
$util.toJson($cost)

Query

Tips

Data Architect @CDL_Software , AWS Community Builder, 12 x AWS Certified. GCP, Azure, SCRUM, DAMA CDMP, Hashicorp & Kafka Certified