# lambda-ddex-ingester

This repository contains a collection of lambdas that are used in conjunction with an AWS State Machine that handles the ingestion of DDEX releases into The Orchard. 

### Environment management
Each lambda is dockerised so environment management should be taken care of. View the Makefile for all commands, e.g. `make env`, `make lint` and `make test`.

### Environment variables for local testing
A `.env` file must be created in each lambda subdirectory if system variables are not to be utilised.

`cp .env.shadow .env`

### Running tests
Tests are run with Pytest and can be run using `make test`

### Accessing AWS
AWS credentials must be requested from Devops for Dev and QA environments, which are required for executing the lambdas.

You must set up an AWS credentials file here:

`~/.aws/credentials`

Which must contain these blocks for each environment you wish to use:

```
[orchard-prod]

aws_access_key_id = XXXX

aws_secret_access_key = XXXX
```

Note that PROD and QA are within the same environment.

### Running lambdas locally

1. Utilise `aws-mfa` to generate a session token. (see `Useful aliases` for suggestions)

2. These values must be used in the local `.env` or system variables like so:
```
    AWS_ACCESS_KEY_ID=XXXX
    AWS_SECRET_ACCESS_KEY=XXXX
```

These variables may or may not already exist in your `.env.shadow` settings.

3. In the relevant lambda directory, execute `make run`
4. Invoke the lambda with your tool of choice (e.g. Postman) to this URL:

`http://localhost:9000/2015-03-31/functions/function/invocations`

### Useful development tools
https://github.com/warrensbox/terraform-switcher
https://github.com/broamski/aws-mfa

### Running lambda tests locally:
1. Checkout out this repository
2. Create a local env environment:
    `make env`
3. Activate environment
    `source env/bin/activate`
4. Run tests
    `make test`

See this stack overflow for reference:

https://stackoverflow.com/questions/68090955/test-an-aws-lambda-locally-using-docker-container-image

### Pushing to this repo
Please fork this repo and create a PR from your own branch:
`git remote add <name> <git@thisforkedrepo...>`


### Useful aliases
To use these aliases, place them into a relevant alias directory such as `~/.bashrc`:
```
alias mfa-dev="aws-mfa --profile=orchard-dev --device arn:aws:iam::103233932089:mfa/<UPDATEUSERHERE> --long-term-suffix none --short-term-suffix mfa"

alias mfa-prod="aws-mfa --profile=orchard-prod --device arn:aws:iam::437795906767:mfa/<UPDATEUSERHERE> --long-term-suffix none --short-term-suffix mfa"
```
### Deployment pipeline
The deployment pipeline is managed by Jenkinsfile in the root of this repository.
The link to the Jenkins pipeline is here:
https://pipeline.theorchard.io/job/theorchard/job/lambda-ddex-ingester/job/master/
