# lambda-sound-recordings

This project contains lambdas used for the management and delivery of sound recordings.

## Setup
* Install [docker](https://docs.docker.com/get-docker/)
* Clone this repository
* Navigate to the lambda you want to work on in `lambda/`
* run aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 086679231553.dkr.ecr.us-east-1.amazonaws.com

## Running

### Start Container
```
$ docker compose up --build -d function
```

### Execute Function
Use the HTTP client of your choice. The body of the request is the `event` passed into the function.

```
curl --request POST \
  --url http://localhost:9000/2015-03-31/functions/function/invocations \
  --header 'Content-Type: application/json' \
  --data "$(cat tests/sample_event.json)"
```

### Load Changes
The `src` directory and `config.py` are mounted into the container via the `docker-compose.yaml` configuration. This means you don't need to re-build the entire container to see changes. **However** the container will need to be restarted. This process should be as quick as restarting uwsgi when making changes to a web application.
```
$ docker compose restart function
```
:zap: You should add this command to a post-write hook in your preferred editor to automatically restart.

## Linting and Testing

### Run Container
```
$ docker compose up --build lint-and-test # for tests
$ docker compose up --build function # to run container
```

### Set pytest Options
Sometimes you may want to customize how tests run using [pytest options](https://docs.pytest.org/en/stable/usage.html) when doing development locally, only running a subset of tests using `-k` for example. Simply edit `TEST_ARGS` value in `docker-compose.yaml`, **without** quotes, to set the options you need.

:warning: Careful not to commit changes

### Skip Linting
When developing locally, you might not want to worry about linting when working to make tests pass. Simple edit the `SKIP_LINT` value in `docker-compose.yaml` to be `1`.

:warning: Careful not to commit changes

### Capture Exit Code
Unless the container itself crashes, or is killed, the exit code will be `0`. When the exit code of `lint-and-test.sh` script matters, a pull-request builder for example, run tests with the following flags.
```
$ docker compose up --exit-code-from lint-and-test --abort-on-container-exit --build lint-and-test
```

### AWS Issues
Note the containers should use env vars to set access to AWS. These should be located in the docker-compose.yaml file. If they are missing, find one in which they are present and copy. Make sure to run awsume for the production account e.g. `awsume prod` in the same shell you're working in, before building the image and running the container. 

Learn more about awsume [here](https://www.notion.so/AWS-Access-f841b9dd815d4443a80e96a86c92cd2f?source=copy_link#f11f08d09f16493a8a3e9dbf38d87164) 
