GithubHelp home page GithubHelp logo

lokeshsp / appsync-lambda-ai Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dabit3/appsync-lambda-ai

0.0 2.0 0.0 882 KB

Demo of using a GraphQL resolver to hit a lambda function, then hit a few AI services, and return the response.

License: Apache License 2.0

JavaScript 43.74% Python 13.35% Java 12.78% Objective-C 30.14%

appsync-lambda-ai's Introduction

AI Enabled GraphQL with AWS AppSync

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.

Getting Started

React Native setup

  1. clone the project
git clone https://github.com/dabit3/appsync-lambda-ai.git
  1. change directories into the project & install depencies
cd appsync-lambda-ai
yarn || npm install

Creating an S3 bucket

  1. Visit https://s3.console.aws.amazon.com/ and click Create bucket

  2. Give the bucket a name and choose defaults for all other options

  3. Update line 46 in App.js to use the bucket name you just created (replace YOURBUCKETNAME):

const mp3Url = `https://s3.amazonaws.com/YOURBUCKETNAME/${sentence}`

Setting up the Lambda function

  1. In the AWS dashboard, go to the Lambda console

  2. Create a new function by clicking on Create Function

  3. Give the function a name, choose the runtime as Node.js 8.10, choose Create a custom role for the role, give the role a name of lambda_ai_role & click Allow.

  4. Click Create Function

  5. 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
  1. 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, replace YOURBUCKETNAME with the bucket name you used when you created the S3 bucket.

  2. 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

AWS AppSync Setup

  1. Create a new AWS ApppSync API, choose custom schema

  2. Define the following schema

type Query {
  getTranslatedSentence(sentence: String!, code: String!): TranslatedSentence
}

type TranslatedSentence {
  sentence: String!
}
  1. 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
  1. 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
  1. 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',
}

appsync-lambda-ai's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.