AWS Lambda
Setup steps
- On AWS add Lambda functions
- Create AWS credentials
- On TakeShape add an AWS Lambda service
- Add any
query
,mutation
or@resolver
property to your schema which uses the AWS service you created.
On AWS
Add a sample AWS Lambda function
hello-world.js
exports.handler = async (event) => {
return `Hello ${event.args.name}`;
};
event.args
is the object that is set byargsMapping
. Any values you want to use in the Lambda add to yourargsMapping
.- The return value of your function can be any JSON-serializable value which must match the type set in the TakeShape schema.
Configure AWS credentials
- Log into your AWS account and navigate to IAM Policies.
- Select "Create Policy"
- Select the "JSON" tab
- Paste in the access policy below replacing the "Resource" with the arn of your Lambda function.
hello-world-policy
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowAuroraToExampleFunction",
"Effect": "Allow",
"Action": "lambda:InvokeFunction",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:hello-world"
}
]
}
- Navigate to IAM Users.
- Select "Add users"
- On the next screen give the user a name and check the "Programatic access" checkbox.
- Select "Next: Permissions"
- Select "Attach policies directly" and select the policy created above
- Select through "Next: Tags", "Next: Review" and "Create user"
- Either copy/paste the "Access key ID" and "Secret access key" to a safe place or download the CSV
On TakeShape
Add an AWS Lambda service
Project → Schema → Connect Service → AWS Lambda
- In the "Name" field, choose the name you'd like to give this service. The "Slug" field will be automatically filled in based on the name field.
- Under "Authentication" enter the AWS "Access key ID" and "Secret access key".
Add any query
, mutation
or @resolver
property
Project → Schema → JSON
Query example
schema.json
"helloWord": {
"shape": "string",
"resolver": {
"name": "awsLambda:invoke",
"service": "lambda",
"options": {"functionName": "hello-world"}
},
"args": {"type": "object", "properties": {"name": {"type": "string"}}}
},
@resolver
property example
schema.json
"hello": {
"type": "string",
"@resolver": {
"name": "awsLambda:invoke",
"service": "lambda",
"options": {"functionName": "hello-world"},
"argsMapping": {
"name": [["get", {"path": "source.name"}]]
}
}
}
Test the query using the API Explorer
Project → API → API Explorer
Test query
{
helloWord(name: "Bob")
}
Test query response
{
"data": {
"helloWord": "Hello Bob"
}
}