Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1646 lambda #401

Merged
merged 41 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
7c73d9f
fix(infra): change destination for public scores & config of cloudfront
nutrina Sep 12, 2023
80c2d90
fix(api,infra): fix bug in and removed acl from task exporting scores
nutrina Sep 13, 2023
a43f027
feat(infra,api): add dockerfile for AWS lambda
nutrina Sep 14, 2023
980a5a6
feat(load-test): adjustments for running submit-passport as lambda
nutrina Sep 22, 2023
b1ce22f
feat(infra): forgot to add lambda function
nutrina Sep 22, 2023
0d6e1ee
feat(infra,api): update readme in the load_test folder
nutrina Sep 25, 2023
aee198f
feat(api): adding lambda to ci and building lambda
tim-schultz Oct 5, 2023
f2b0374
chore(infra): wip lambda build
tim-schultz Oct 5, 2023
c6d3afd
fix(infra): succesfully building image
tim-schultz Oct 5, 2023
2f31385
feat(lambdas): wip submit passport lambda auth and rate limiting with…
tim-schultz Oct 6, 2023
6af9b76
feat(api): moves submit passport lambda adds logger
aminah-io Oct 6, 2023
0b8a5d0
chore(infra): add image from ci
tim-schultz Oct 6, 2023
7a98599
fix(ci): lambda ecr
tim-schultz Oct 6, 2023
aea8b5d
fix(ci): aws_lambda
tim-schultz Oct 6, 2023
27c24e6
fix(infra): fix docker file build
tim-schultz Oct 6, 2023
3dc5437
fix(infra): adjust docker file to point at module, add requirements.txt
tim-schultz Oct 6, 2023
5b6cf2e
fix(api): fix build for lambda image
nutrina Oct 6, 2023
2ea085d
chore(api): test lambda rate limiting and auth
tim-schultz Oct 6, 2023
37cf727
chore(api): rename submit-passport module and cleanup
tim-schultz Oct 6, 2023
97e1ffd
chore(api): add lambda testing readme
tim-schultz Oct 6, 2023
66ebf2a
fix(api): fix module resolution for pytest
tim-schultz Oct 6, 2023
5813cb1
fix(ci): update ECR name
tim-schultz Oct 6, 2023
7f1238d
feat(infra): move lambda definition to service and add to prod
tim-schultz Oct 6, 2023
270020f
chore(ci): add comments and correct ECR name alias
tim-schultz Oct 6, 2023
e2a0b5f
chore(ci): add lambda to prod ci
tim-schultz Oct 7, 2023
daf3055
fix(infra): build image uri using pulumi
tim-schultz Oct 9, 2023
96c0961
fix(infra): use private ECR image for lambda
tim-schultz Oct 9, 2023
2b75b40
fix(infra): login to private ECR
tim-schultz Oct 9, 2023
eef7319
fix(ci): update ECR to private
tim-schultz Oct 9, 2023
f6db24a
fix(ci): region
tim-schultz Oct 9, 2023
fbc1efe
fix(ci): use env for image url
tim-schultz Oct 9, 2023
ac3c82f
fix(ci): name image using env variable
tim-schultz Oct 9, 2023
c0731b5
fix(ci): update region
tim-schultz Oct 9, 2023
ddbf00d
fix(ci): hard code us-west-2
tim-schultz Oct 9, 2023
fc8d94c
fix(api): update how x-api-key is parsed and validated
tim-schultz Oct 9, 2023
aafef03
fix(api): fix parsing of body request
tim-schultz Oct 10, 2023
6f16ca5
chore(api, ci): fix test and prod ci
tim-schultz Oct 10, 2023
2fb04d7
fix(api): remove rate limiting test due to conflict
tim-schultz Oct 10, 2023
13f0a27
chore(infra): add v2 endpoint
tim-schultz Oct 10, 2023
b6afd61
Merge branch 'main' into 1646_lambda
tim-schultz Oct 10, 2023
56656e9
feat(api): fix tests, remove unused request handler
nutrina Oct 10, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions .github/workflows/api-promote-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,56 @@ jobs:
outputs:
dockerTag: ${{ steps.vars.outputs.sha_short }}

build-submit-passport-lambda:
environment: Staging
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: ${{ inputs.commit }}

- uses: actions/setup-python@v4
with:
python-version: "3.11"

- name: Declare some variables
id: vars
shell: bash
run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_STAGING }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_STAGING }}
aws-region: us-west-2

- name: Login to Amazon ECR Private
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, tag, and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: submit-passport-lambdas
IMAGE_TAG: ${{ steps.vars.outputs.sha_short }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f api/aws_lambdas/submit_passport/Dockerfile ./api/
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
outputs:
dockerTag: ${{ steps.vars.outputs.sha_short }}

deploy-production:
environment: Production
needs: [test, build-api, build-verifier, build-indexer]
needs:
[
test,
build-api,
build-verifier,
build-indexer,
build-submit-passport-lambda,
]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -246,7 +293,8 @@ jobs:
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
DOCKER_GTC_PASSPORT_SCORER_IMAGE: public.ecr.aws/c8n6v8e9/passport-scorer:${{ needs.build-api.outputs.dockerTag }}
DOCKER_GTC_PASSPORT_VERIFIER_IMAGE: public.ecr.aws/c8n6v8e9/passport-verifier:${{ needs.build-verifier.outputs.dockerTag }}
DOCKER_GTC_PASSPORT_INDEXER_IMAGE: public.ecr.aws/t1g3k9q8/passport-indexer:${{ needs.build-indexer.outputs.dockerTag }}
DOCKER_GTC_PASSPORT_INDEXER_IMAGE: public.ecr.aws/c8n6v8e9/passport-indexer:${{ needs.build-indexer.outputs.dockerTag }}
DOCKER_GTC_SUBMIT_PASSPORT_LAMBDA_IMAGE: ${{secrets.PRIVATE_ECR_URL}}:${{ needs.build-submit-passport-lambda.outputs.dockerTag }}
ROUTE_53_ZONE: ${{ secrets.ROUTE53_ZONE_ID }}
ROUTE_53_ZONE_FOR_PUBLIC_DATA: ${{ secrets.ROUTE_53_ZONE_FOR_PUBLIC_DATA }}
DOMAIN: ${{ secrets.DOMAIN }}
Expand Down
50 changes: 49 additions & 1 deletion .github/workflows/api-promote-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,46 @@ jobs:
outputs:
dockerTag: ${{ steps.vars.outputs.sha_short }}

build-submit-passport-lambda:
environment: Staging
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: ${{ inputs.commit }}

- uses: actions/setup-python@v4
with:
python-version: "3.11"

- name: Declare some variables
id: vars
shell: bash
run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_STAGING }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_STAGING }}
aws-region: us-west-2

- name: Login to Amazon ECR Private
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, tag, and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: submit-passport-lambdas
IMAGE_TAG: ${{ steps.vars.outputs.sha_short }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f api/aws_lambdas/submit_passport/Dockerfile ./api/
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
outputs:
dockerTag: ${{ steps.vars.outputs.sha_short }}

build-indexer:
environment: Staging
runs-on: ubuntu-latest
Expand Down Expand Up @@ -204,7 +244,14 @@ jobs:

deploy-staging:
environment: Staging
needs: [test, build-api, build-verifier, build-indexer]
needs:
[
test,
build-api,
build-verifier,
build-indexer,
build-submit-passport-lambda,
]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -256,6 +303,7 @@ jobs:
DOCKER_GTC_PASSPORT_SCORER_IMAGE: public.ecr.aws/t1g3k9q8/passport-scorer:${{ needs.build-api.outputs.dockerTag }}
DOCKER_GTC_PASSPORT_VERIFIER_IMAGE: public.ecr.aws/t1g3k9q8/passport-verifier:${{ needs.build-verifier.outputs.dockerTag }}
DOCKER_GTC_PASSPORT_INDEXER_IMAGE: public.ecr.aws/t1g3k9q8/passport-indexer:${{ needs.build-indexer.outputs.dockerTag }}
DOCKER_GTC_SUBMIT_PASSPORT_LAMBDA_IMAGE: ${{secrets.PRIVATE_ECR_URL}}:${{ needs.build-submit-passport-lambda.outputs.dockerTag }}
ROUTE_53_ZONE: ${{ secrets.ROUTE53_ZONE_ID }}
ROUTE_53_ZONE_FOR_PUBLIC_DATA: ${{ secrets.ROUTE_53_ZONE_FOR_PUBLIC_DATA }}
DOMAIN: ${{ secrets.DOMAIN_STAGING }}
Expand Down
53 changes: 53 additions & 0 deletions api/Dockerfile.python3.11
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

# https://pipenv.pypa.io/en/latest/basics/#pipenv-and-docker-containers
FROM public.ecr.aws/lambda/python:3.11 AS base


#########################################################
# Builder
#########################################################
FROM base AS builder

RUN pip install pipenv

# Tell pipenv to create venv in the current directory
ENV PIPENV_VENV_IN_PROJECT=1

ADD Pipfile.lock /usr/src/

WORKDIR /usr/src


# NOTE: If you install binary packages required for a python module, you need
# to install them again in the runtime. For example, if you need to install pycurl
# you need to have pycurl build dependencies libcurl4-gnutls-dev and libcurl3-gnutls
# In the runtime container you need only libcurl3-gnutls

# RUN apt install -y libcurl3-gnutls libcurl4-gnutls-dev
# RUN /root/.local/bin/pipenv lock

RUN pipenv requirements > requirements.txt
# RUN /usr/src/.venv/bin/python -c "import django; print(django.__version__)"
# RUN ls /usr/src/.venv/lib/

#########################################################
# Runtime
#########################################################
FROM base AS runtime

# RUN mkdir -v /usr/src/venv

COPY --from=builder /usr/src/requirements.txt ${LAMBDA_TASK_ROOT}
COPY . ${LAMBDA_TASK_ROOT}


WORKDIR ${LAMBDA_TASK_ROOT}

RUN pip install -r requirements.txt
RUN python -c "import django; print(f\"\n{'*' * 40}\n* DJANGO VERSION: {django.__version__ : <21}*\n{'*' * 40}\n\")"
# ENV PATH="/usr/src/.venv/bin/:${PATH}"


RUN STATIC_ROOT=${LAMBDA_TASK_ROOT}/static SECRET_KEY=secret_is_irelevent_here CERAMIC_CACHE_SCORER_ID=1 DATABASE_URL=sqlite:////dunmmy_db.sqlite3 python manage.py collectstatic --noinput

CMD [ "lambda_function.handler" ]
47 changes: 47 additions & 0 deletions api/aws_lambdas/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Testing the Application Locally with Docker

## 1. Building the Docker Image

First, navigate to the passport-scorer root directory. Then, execute the command below to build the Docker image:

```bash
docker build --platform linux/amd64 -t docker-image:test -f ./api/aws_lambdas/submit_passport/Dockerfile ./api
```

## 2. Running the Docker Image

Once you've successfully built the image, you can run it. Ensure you point to the location of your running PostgreSQL instance. In this context, the database is running in another Docker container:

```bash
docker run -e DATABASE_URL=postgres://passport_scorer:[email protected]:5432/passport_scorer -p 8080:8080 docker-image:test
```

### 3. Making Curl Requests

After you have your application up and running, you can test its endpoints using `curl`. Use the command below to make a request to the instance:

````markdown
```bash
curl -X 'POST' \
'http://localhost:8080/2015-03-31/functions/function/invocations' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"requestContext": {
"elb": {
"targetGroupArn": "arn"
}
},
"httpMethod": "POST",
"path": "/registry/submit-passport",
"queryStringParameters": {},
"headers": {add necessary headers here},
"body": "{\"address\":\"0x868asAe3B27asdF475e41FAdDF9F0cf97fDB71fC\",\"community\":\"24\"}",
"isBase64Encoded": false
}'
```
````

```
```
Empty file added api/aws_lambdas/__init__.py
Empty file.
74 changes: 74 additions & 0 deletions api/aws_lambdas/docker-compose-lambda.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
version: "3.9" # optional since v1.27.0
services:
# redis:
# image: redis
# restart: unless-stopped
# ports:
# - 6379:6379

api:
build:
context: ./api/aws_lambdas/submit-passport
dockerfile: Dockerfile

ports:
- "127.0.0.1:8000:8000"
- 8080:8080

volumes:
- ./api:/var/task

environment:
- CELERY_BROKER_URL=redis://redis:6379/0
- CERAMIC_CACHE_CACAO_VALIDATION_URL=http://verifier:8001/verify
- CERAMIC_CACHE_SCORER_ID=1
- DATABASE_URL=postgres://passport_scorer:passport_scorer_pwd@postgres:5432/passport_scorer

# command: uvicorn scorer.asgi:application --reload --host 0.0.0.0 --port 8002
# command: uvicorn scorer.asgi:application --host 0.0.0.0 --port 8002 --backlog 5000 --workers 6 --log-level debug
# command: gunicorn -w 8 -t 60 -k uvicorn.workers.UvicornWorker --worker-connections 5000 scorer.asgi:application -b 0.0.0.0:8002
# command: gunicorn -w 4 -t 60 -k uvicorn.workers.UvicornWorker scorer.asgi:application -b 0.0.0.0:8002

# worker:
# build: api
# volumes:
# - ./api:/app
# environment:
# - CELERY_BROKER_URL=redis://redis:6379/0
# - CERAMIC_CACHE_SCORER_ID=1
# command: celery -A scorer worker -Q score_passport_passport,score_registry_passport -l DEBUG

# interface:
# build:
# context: ./interface
# dockerfile: Dockerfile

# ports:
# - 3001:3001

# volumes:
# - ./interface:/app
# - /app/node_modules
# - /app/.next

# command: yarn dev

verifier:
build: verifier

postgres:
image: postgres:12.3-alpine
restart: unless-stopped

environment:
POSTGRES_USER: passport_scorer
POSTGRES_PASSWORD: passport_scorer_pwd
POSTGRES_DB: passport_scorer
PGDATA: /db_data

command: postgres -c 'max_connections=2000'
volumes:
- ./postgres_db_data:/db_data

ports:
- 5432:5432
40 changes: 40 additions & 0 deletions api/aws_lambdas/submit_passport/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

# https://pipenv.pypa.io/en/latest/basics/#pipenv-and-docker-containers
FROM public.ecr.aws/lambda/python:3.11 AS base


#########################################################
# Builder
#########################################################
FROM base AS builder

RUN pip install pipenv

# Tell pipenv to create venv in the current directory
ENV PIPENV_VENV_IN_PROJECT=1

ADD Pipfile.lock /usr/src/

WORKDIR /usr/src

RUN pipenv requirements > requirements.txt

#########################################################
# Runtime
#########################################################
FROM base AS runtime

COPY --from=builder /usr/src/requirements.txt ${LAMBDA_TASK_ROOT}
COPY ./ ${LAMBDA_TASK_ROOT}


WORKDIR ${LAMBDA_TASK_ROOT}

RUN pip install -r requirements.txt

RUN python -c "import django; print(f\"\n{'*' * 40}\n* DJANGO VERSION: {django.__version__ : <21}*\n{'*' * 40}\n\")"


RUN STATIC_ROOT=${LAMBDA_TASK_ROOT}/static SECRET_KEY=secret_is_irelevent_here CERAMIC_CACHE_SCORER_ID=1 DATABASE_URL=sqlite:////dunmmy_db.sqlite3 python manage.py collectstatic --noinput

CMD [ "aws_lambdas.submit_passport.submit_passport.handler" ]
Empty file.
Loading