forked from ExamProCo/AWS-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
## Install requirements | ||
|
||
```sh | ||
cd app | ||
pip install -r requirements.txt | ||
``` | ||
|
||
## Run without a container | ||
|
||
```sh | ||
python app.py | ||
``` | ||
|
||
This should start the app on port `5000` | ||
|
||
## Test the endpoint for local app | ||
|
||
```sh | ||
curl localhost:5000/api/hello | ||
curl localhost:5000/api/hello?name=Andrew+Brown | ||
``` | ||
|
||
## Build Image | ||
|
||
```sh | ||
docker build -t app ./app | ||
``` | ||
|
||
## Run the Container | ||
|
||
```sh | ||
docker run --rm -p 4567:4567 -it app | ||
``` | ||
|
||
## Test the endpoint for local container app | ||
|
||
```sh | ||
curl localhost:4567/api/hello | ||
curl localhost:4567/api/hello?name=Andrew+Brown | ||
``` | ||
|
||
## Create ECR Repos | ||
|
||
|
||
aws ecr create-repository \ | ||
--repository-name python \ | ||
--image-tag-mutability IMMUTABLE | ||
|
||
aws ecr create-repository \ | ||
--repository-name app \ | ||
--image-tag-mutability MUTABLE | ||
|
||
## Login to ECR | ||
|
||
```sh | ||
aws ecr get-login-password --region ca-central-1 | docker login \ | ||
--username AWS \ | ||
--password-stdin 982383527471.dkr.ecr.ca-central-1.amazonaws.com | ||
``` | ||
|
||
### Pull, Tag and Push Python Image | ||
|
||
|
||
```sh | ||
docker pull python:3.13.0a6-slim-bookworm | ||
docker tag python:3.13.0a6-slim-bookworm 982383527471.dkr.ecr.ca-central-1.amazonaws.com/python:3.13.0a6-slim-bookworm | ||
docker push 982383527471.dkr.ecr.ca-central-1.amazonaws.com/python:3.13.0a6-slim-bookworm | ||
``` | ||
|
||
### Build, tag and push app container | ||
|
||
> Update the Dockerfile to reference our python image in ECR | ||
```sh | ||
docker build -t app . | ||
docker tag app:latest 982383527471.dkr.ecr.ca-central-1.amazonaws.com/app:latest | ||
docker push 982383527471.dkr.ecr.ca-central-1.amazonaws.com/app:latest | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# FROM python:3.13.0a6-slim-bookworm | ||
FROM 982383527471.dkr.ecr.ca-central-1.amazonaws.com/python:3.13.0a6-slim-bookworm | ||
|
||
# Inside Container | ||
# make a new folder inside container | ||
WORKDIR /app | ||
|
||
# Outside Container -> Inside Container | ||
# this contains the libraries want to install to run the app | ||
COPY requirements.txt requirements.txt | ||
|
||
# Inside Container | ||
# Install the python libraries used for the app | ||
RUN pip3 install -r requirements.txt | ||
|
||
# Outside Container -> Inside Container | ||
# . means everything in the current directory | ||
# first period . - /app (outside container) | ||
# second period . /app (inside container) | ||
COPY . . | ||
|
||
EXPOSE ${PORT} | ||
|
||
ENV PYTHONUNBUFFERED=1 | ||
|
||
# CMD (Command) | ||
# python3 -m flask run --host=0.0.0.0 --port=4567 | ||
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0", "--port=4567", "--debug"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from flask import Flask | ||
from flask import request | ||
import os | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route("/api/hello", methods=['GET']) | ||
def hello(): | ||
model = {'errors': None} | ||
name = request.args.get('name') | ||
if not name: | ||
model['errors'] = [{"msg": "Name not provided"}] | ||
else: | ||
model['data'] = {"msg": f"Hello {name}!"} | ||
|
||
if 'errors' in model and model['errors'] is not None: | ||
return model['errors'], 422 | ||
else: | ||
return model['data'], 200 | ||
|
||
if __name__ == "__main__": | ||
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
flask |