Skip to content

Commit f2e3cdb

Browse files
authored
Merge pull request #1 from MITLibraries/create-repo-structure
Create basic repository structure
2 parents 150d2c6 + 3e1fd2d commit f2e3cdb

File tree

14 files changed

+244
-0
lines changed

14 files changed

+244
-0
lines changed

.github/dependabot.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# To get started with Dependabot version updates, you'll need to specify which
2+
# package ecosystems to update and where the package manifests are located.
3+
# Please see the documentation for all configuration options:
4+
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
5+
6+
version: 2
7+
updates:
8+
# Maintain dependencies for GitHub Actions
9+
- package-ecosystem: "github-actions"
10+
directory: "/"
11+
schedule:
12+
interval: "daily"
13+
14+
# Maintain dependencies for npm
15+
- package-ecosystem: "pipenv"
16+
directory: "/"
17+
schedule:
18+
interval: "daily"

.github/pull-request-template.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
### What does this PR do?
2+
3+
Describe the overall purpose of the PR changes. Doesn't need to be as specific as the
4+
individual commits.
5+
6+
### Helpful background context
7+
8+
Describe any additional context beyond what the PR accomplishes if it is likely to be
9+
useful to a reviewer.
10+
11+
Delete this section if it isn't applicable to the PR.
12+
13+
### How can a reviewer manually see the effects of these changes?
14+
15+
Explain how to see the proposed changes in the application if possible.
16+
17+
Delete this section if it isn't applicable to the PR.
18+
19+
### Includes new or updated dependencies?
20+
21+
YES | NO
22+
23+
### What are the relevant tickets?
24+
25+
Include links to Jira Software and/or Jira Service Management tickets here.
26+
27+
### Developer
28+
29+
- [ ] All new ENV is documented in README (or there is none)
30+
- [ ] Stakeholder approval has been confirmed (or is not needed)
31+
32+
### Code Reviewer
33+
34+
- [ ] The commit message is clear and follows our guidelines
35+
(not just this pull request message)
36+
- [ ] There are appropriate tests covering any new functionality
37+
- [ ] The documentation has been updated or is unnecessary
38+
- [ ] The changes have been verified
39+
- [ ] New dependencies are appropriate or there were no changes

.github/workflows/ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: CI
2+
on: push
3+
jobs:
4+
test:
5+
uses: mitlibraries/.github/.github/workflows/python-shared-test.yml@main
6+
lint:
7+
uses: mitlibraries/.github/.github/workflows/python-shared-lint.yml@main

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.9.12

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM public.ecr.aws/lambda/python:3.9
2+
3+
# Copy function code
4+
COPY . ${LAMBDA_TASK_ROOT}/
5+
6+
# Install dependencies
7+
RUN pip3 install pipenv
8+
RUN pipenv requirements > requirements.txt
9+
RUN pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
10+
11+
# Default handler. See README for how to override to a different handler.
12+
CMD [ "lambdas.my_function.lambda_handler" ]

Makefile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
SHELL=/bin/bash
2+
DATETIME:=$(shell date -u +%Y%m%dT%H%M%SZ)
3+
4+
### Dependency commands ###
5+
6+
install: ## Install dependencies and CLI app
7+
pipenv install --dev
8+
9+
update: install ## Update all Python dependencies
10+
pipenv clean
11+
pipenv update --dev
12+
13+
### Test commands ###
14+
15+
test: ## Run tests and print a coverage report
16+
pipenv run coverage run --source=lambdas -m pytest -vv
17+
pipenv run coverage report -m
18+
19+
coveralls: test
20+
pipenv run coverage lcov -o ./coverage/lcov.info
21+
22+
### Code quality and safety commands ###
23+
24+
lint: bandit black mypy pylama safety ## Run linting, code quality, and safety checks
25+
26+
bandit:
27+
pipenv run bandit -r lambdas
28+
29+
black:
30+
pipenv run black --check --diff .
31+
32+
mypy:
33+
pipenv run mypy lambdas
34+
35+
pylama:
36+
pipenv run pylama --options setup.cfg
37+
38+
safety:
39+
pipenv check
40+
pipenv verify

Pipfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
8+
[dev-packages]
9+
bandit = "*"
10+
black = "*"
11+
coverage = "*"
12+
coveralls = "*"
13+
mypy = "*"
14+
pylama = {extras = ["all"], version = "*"}
15+
pytest = "*"
16+
17+
[requires]
18+
python_version = "3.9"

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# python-lambda-template
2+
3+
A template repository for creating Python lambda functions.
4+
5+
## Repo setup (delete this section and above after initial function setup)
6+
7+
1. Rename "my_function" to the desired initial function name across the repo. (May be helpful to do a project-wide find-and-replace).
8+
2. Update Python version if needed (note: AWS lambda cannot currently support versions higher than 3.9).
9+
3. Install all dependencies with `make install` to create initial Pipfile.lock with latest dependency versions.
10+
4. Add initial function description to README and update initial required ENV variable documentation as needed.
11+
5. Update license if needed (check app-specific dependencies for licensing terms).
12+
6. Check Github repository settings:
13+
- Confirm repo branch protection settings are correct (see [dev docs](https://mitlibraries.github.io/guides/basics/github.html) for details)
14+
- Confirm that all of the following are enabled in the repo's code security and analysis settings:
15+
- Dependabot alerts
16+
- Dependabot security updates
17+
- Secret scanning
18+
19+
# my_function
20+
21+
Description of the function/functions.
22+
23+
## Development
24+
25+
- To install with dev dependencies: `make install`
26+
- To update dependencies: `make update`
27+
- To run unit tests: `make test`
28+
- To lint the repo: `make lint`
29+
30+
## Required ENV
31+
32+
- `WORKSPACE` = Set to `dev` for local development, this will be set to `stage` and `prod` in those environments by Terraform.
33+
34+
## Running locally
35+
36+
<https://docs.aws.amazon.com/lambda/latest/dg/images-test.html>
37+
38+
- Build the container:
39+
40+
```bash
41+
docker build -t my_function:latest .
42+
```
43+
44+
- Run the default handler for the container:
45+
46+
```bash
47+
docker run -p 9000:8080 my_function:latest
48+
```
49+
50+
- Post to the container:
51+
52+
```bash
53+
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
54+
```
55+
56+
- Observe output:
57+
58+
```
59+
"You have successfully called this lambda!"
60+
```
61+
62+
## Running a different handler in the container
63+
64+
If this repo contains multiple lambda functions, you can call any handler you copy into the container (see Dockerfile) by name as part of the `docker run` command:
65+
66+
```bash
67+
docker run -p 9000:8080 my_function:latest lambdas.<a-different-module>.lambda_handler
68+
```

lambdas/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""lambdas package."""

lambdas/my_function.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import json
2+
import logging
3+
4+
logger = logging.getLogger(__name__)
5+
logger.setLevel(logging.DEBUG)
6+
7+
8+
def lambda_handler(event: dict, context: object) -> str: # noqa
9+
logger.debug(json.dumps(event))
10+
return "You have successfully called this lambda!"

0 commit comments

Comments
 (0)