This app demonstrates how you can use a Lambda function as an AWS AppSync datasource to perform really cool operations and return the result in a GraphQL query. In this example, we build an app that allows someone to put the text they would like to translate into the app, the app then returns the translation into voice for the user to play back.
- clone the project
git clone https://github.com/dabit3/appsync-lambda-ai.git
- change directories into the project & install depencies
cd appsync-lambda-ai
yarn || npm install
-
Visit https://s3.console.aws.amazon.com/ and click Create bucket
-
Give the bucket a name and choose defaults for all other options
-
Update line 46 in App.js to use the bucket name you just created (replace
YOURBUCKETNAME
andREGION
):
const mp3Url = `https://s3-REGION.amazonaws.com/YOURBUCKETNAME/${sentence}`
-
In the AWS dashboard, go to the Lambda console
-
Create a new function by clicking on Create Function
-
Give the function a name, choose the runtime as Node.js 12.x, choose Create a custom role for the role, give the role a name of lambda_ai_role & click Allow.
-
Click Create Function
-
Next, we need to add permissions to the Lambda function in order to access other services (such as S3, Polly, & Translate). To do so, go to the IAM console, go to Roles, find & choose the lambda_ai_role, and add the following policies:
- AmazonS3FullAccess
- AmazonPollyFullAccess
- TranslateReadOnly
-
Next, we need to update and add the actual lambda function we will be running! In the lambda folder of this project, there is a lambda function (index.js) & a package.json file. In
lambda/index.js
on line 8, replaceYOURBUCKETNAME
with the bucket name you used when you created the S3 bucket and theREGION
where this project lives. -
Finally, from within the lambda directory install the dependencies and then zip the folder into a zip file using the following command:
zip -r ../translate.zip *
Upload the zip file to Lambda as the function code and click Save
-
Create a new AWS ApppSync API, choose custom schema
-
Define the following schema
type Query {
getTranslatedSentence(sentence: String!, code: String!): TranslatedSentence
}
type TranslatedSentence {
sentence: String!
}
- Create a lambda function data source
- Click Data Sources
- Click NEW
- Give the data source a name, choose AWS Lambda Function as the Data source type
- For the region, choose the region where you created the Lamba function
- For the Function ARN, choose the Lambda function you would like to use
- For the Role, choose New Role
- Add a resolver to the getTranslatedSentence query.
- Click on Schema in the left menu
- On the Right (under Data Types), click on Attach next to the
getTranslatedSentence
field. - For the Data Source name, choose the new data source we just created
- Click Save
- Add AppSync configuration to the project
Next, we need to edit the aws-exports.js file to specify our AppSync configuration. In the root directory of the React Native project, update aws-exports.js with your AppSync credentials:
const awsmobile = {
'aws_appsync_graphqlEndpoint': 'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql',
'aws_appsync_region': 'us-east-1',
'aws_appsync_authenticationType': 'API_KEY',
'aws_appsync_apiKey': 'da2-xxxxxxxxxxxxxxxxxxxxxxxxxx',
}