# Fansifter-serverless

Fansifter-serverless is a project of [serverless.com](https://serverless.com) framework. Which consists of lambda functions, AppSyncSchema, settings for cognito, appsync, dynamodb and s3.

## Installation

Please visit [Get Started serverless](https://serverless.com/framework/docs/getting-started/) for initial setup.

## Requirements
Before being able to deploy, you have to have AWS configure in your local machine. Install `aws cli`,
and run `aws configure` (or follow those instructions - https://serverless.com/framework/docs/providers/aws/guide/credentials/)

Be careful if you already have configured default credentials. While we can set up custom profile, it means 
all developers need to set it up in the same manner. Right now it's more convenient for all if it's default.
 

## Deploy

NB! it is important to first run `npm install` locally before running sls deploy. [Here's](https://stackoverflow.com/questions/42880987/serverless-framework-with-aws-lambda-error-cannot-find-module) why 

Make sure you're running the later version of npm (>6). You might run into weird problems
with Ubuntu 18, see [here](https://askubuntu.com/questions/1036278/npm-is-incorrect-version-on-latest-ubuntu-18-04-installation) why.


Run this command to deploy
```bash
sls deploy
```

In case you have multiple profiles, use this
`sls deploy --aws-profile fansifter`

If this fails, first try upgrading your local serverless with `serverless-upgrade` and try again.

## Project structure
* `functions` folder consists of all `js` lambda functions we are using.
* `resources` folder consists of all the settings, `appsync`, `cognito`, `dynamodb` table properties and `s3-bucket` properties.
* `serverless.yml` file is main settings file for the whole project. Visit [serverless.yml](https://serverless.com/framework/docs/providers/aws/guide/serverless.yml/) for complete understanding of the serverless structure.


## Adding new appsync mutations and queries

1. Add new function file to `/functions` dir

2. Add a new function to `resources/appsync/schema.yml`, depending whether you want to add Mutation or Query. In the schema
you declare the variables that are being sent via GraphQL from frontend.

3. Add a new mutation resolver to `resources/appsync/mutation_resolvers/mutation.yml`. There's a lot of boilerplate there,
notice if you paste a new block, you need to make changes in 4 places. The FieldName in the mutation resolver 
is what you define in the appsync schema in step 2. Datasource is what you define in step 5.

4. Add the new function block under functions in `/serverless.yml`. The things under VPC should remain unchanged.

5. Create a new Lambda function to `resoures/appsync/datasources.yml`. There's a lot of boilerplate there, notice if you paste 
a new block, you need to make naming changes in 3 places.

6. Add a policy to `resources/appsync/role.yml` for the new Lambda function you defined in step 5,

## Packaging Lambda layer for python modules

TODO!