Building and pushing a container image to ECR
If we consider a simple API using Python and FastAPI, shown next, we need to first package that up into a Docker image locally. We can then test if it is working locally before we push it to ECR. I’ve chosen Python and FastAPI as they are very simple to get up and running, but you can create the container using any language or framework.
The Python code in the main.py
file is shown next:
#!/usr/bin/env python3 '''simple API server that returns Hello World''' from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Hello World"}
We will also need a requirements.txt
file, which will have the following entries:
nyio==3.6.1 click==8.1.3 fastapi==0.83.0 h11==0.13.0 httptools==0.5.0 idna==3.3 importlib-metadata==4.12.0 pydantic==1.10.2 python-dotenv==0.21.0 PyYAML==6.0 sniffio==1.3.0 starlette==0.19.1 typing...