Search…
Example

Define an API

1
# main.py
2
3
from fastapi import FastAPI
4
from pydantic import BaseModel
5
6
app = FastAPI()
7
8
class Data(BaseModel):
9
msg: str
10
11
@app.post("/")
12
def handle_async(data: Data):
13
return data
Copied!

Create a Dockerfile

1
FROM python:3.8-slim
2
3
RUN pip install --no-cache-dir fastapi uvicorn
4
COPY main.py /
5
6
CMD uvicorn --host 0.0.0.0 --port 8080 main:app
Copied!

Build an image

1
docker build . -t hello-world
Copied!

Run a container locally

1
docker run -p 8080:8080 hello-world
Copied!

Make a request

1
curl -X POST -H "Content-Type: application/json" -d '{"msg": "hello world"}' localhost:8080
Copied!

Login to ECR

1
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com
Copied!

Create a repository

1
aws ecr create-repository --repository-name hello-world
Copied!

Tag the image

1
docker tag hello-world <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/hello-world
Copied!

Push the image

1
docker push <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/hello-world
Copied!

Configure a Cortex deployment

1
# cortex.yaml
2
3
- name: hello-world
4
kind: AsyncAPI
5
pod:
6
containers:
7
- name: api
8
image: <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/hello-world
Copied!

Create a Cortex deployment

1
cortex deploy
Copied!

Wait for the API to be ready

1
cortex get --watch
Copied!

Get the API endpoint

1
cortex get hello-world
Copied!

Make a request

1
curl -X POST -H "Content-Type: application/json" -d '{"msg": "hello world"}' http://***.amazonaws.com/hello-world
Copied!

Get the response

1
curl http://***.amazonaws.com/hello-world/<REQUEST_ID>
Copied!
Last modified 2mo ago