Open1

mongoDB(Amazon dynamoDB)利用サービス構築

Teru roomTeru room

mongoDB(Amazon dynamoDB)利用サービス構築

AWS DynamoDB

リモートリポジトリ

main.py
from fastapi import FastAPI
from pydantic import BaseModel
from services.acc_hogehoge import (
      hogehoge, hogehogeUpdate
    ,get_hogehoge_data, put_hogehoge_data 
    , update_hogehoge_data, delete_hogehoge_data
    )
from services.acc_foofoo import (
      foofoo
    , get_foofoo_data, put_foofoo_data 
    , update_foofoo_data, delete_foofoo_data
    )
from services.acc_baabaa import (
      get_baabaa_data, put_baabaa_data
    )
app = FastAPI()

@app.get("/")
def read_root():
  return {"App Name": "HR Industries 2.0"}

@app.get("/hogehoge/{id}/{name}")
async def get_hogehoge(id: str, name: str):
  hogehoge_data = get_hogehoge_data(id, name)
  return hogehoge_data

@app.get("/foofoo/{ID_individual}/{name_氏名}")
async def get_foofoo(ID_individual: str, name_氏名: str):
  foofoo_data = get_foofoo_data(ID_individual, name_氏名)
  return foofoo_data

@app.post("/foofoo/")
async def get_foofoo(item_data: dict):
  foofoo_data=get_foofoo_data(item_data)
  return foofoo_data

@app.get("/baabaa/{hogehoge_ID}/{JD_Number}")
async def get_hogehoge(hogehoge_ID: str, JD_Number: str):
  baabaa_data = get_baabaa_data(hogehoge_ID, JD_Number)
  return baabaa_data

@app.put("/hogehoge/")
async def create_hogehoge(hogehoge: hogehoge):
  response = put_hogehoge_data(hogehoge.model_dump())
  return response

@app.put("/foofoo/")
async def create_foofoo(item_data: dict):
  response = put_foofoo_data(item_data)
  return response

@app.put("/baabaa/")
async def create_baabaa(item_data: dict):
  response = put_baabaa_data(item_data)
  return response

@app.patch("/hogehoge/{id}/{name}")
async def update_hogehoge(id: str, name: str, hogehoge_update: hogehogeUpdate):
  response = update_hogehoge_data(id, name
                                 , hogehoge_update.update_expression
                                 , hogehoge_update.expression_attribute_values)
  return response

@app.delete("/hogehoge/{id}/{name}")
async def delete_hogehoge(id: str, name: str):
    response = delete_hogehoge_data(id, name)
    return response

@app.delete("/foofoo/delete/")
async def delete_foofoo(item_data: dict):
    response = delete_foofoo_data(item_data)
    return response
requirements.txt
fastapi
mangum
uvicorn
boto3
argparse
requests
pymongo

Dockerファイルを作成する

Dockerfile_hogehoge
FROM --platform=linux/amd64 tiangolo/uvicorn-gunicorn-fastapi:python3.11
#FROM --platform=linux/amd64 python:3.11.4-slim-buster

# 作業ディレクトリを設定する
WORKDIR /app

# 既存のディレクトリを削除
RUN rm -rf /app/*

# 必要なパッケージをインストールする
RUN apt-get update && apt-get install -y git
ADD "https://www.random.org/cgi-bin/randbyte?nbytes=10&format=h" /dev/null
RUN git clone https://ghp_xxxxxxxxxxxxxxxxxxxxxxx@github.com/git-acount/mongodb.git .
RUN pip install --upgrade pip
RUN pip3 install -r requirements.txt
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt

# アプリケーションを実行する
ENV AWS_DEFAULT_REGION=us-east-1
ENV AWS_ACCESS_KEY_ID=YYYYYYYYYYYYYYYYY
ENV AWS_SECRET_ACCESS_KEY=oiwniconioweniocnewiociowncni
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]

CI/CD用シェルスクリプトファイルを作る

  • update_repo.sh
    echo 'git add .
    git commit -m "regular update"
    git push -u origin main' > update_repo.sh
    chmod +x update_repo.sh 
    
  • remove_container.sh
    echo 'CONTAINER_NAME=$1
    docker stop $(docker ps -a -q --filter ancestor=$CONTAINER_NAME-fastapi --format="{{.ID}}")
    docker rm $(docker ps -a -q --filter ancestor=$CONTAINER_NAME-fastapi --format="{{.ID}}")
    docker rmi $(docker images $CONTAINER_NAME-fastapi --format "{{.ID}}")
    docker rmi -f $(docker images -q)
    docker ps -a && docker images' > remove_container.sh
    chmod +x remove_container.sh 
    
  • build_run_container.sh
    echo 'CONTAINER_NAME=$1
    cd ~/aws/mongodb/
    docker build -t $CONTAINER_NAME-fastapi -f Dockerfile_$CONTAINER_NAME .
    docker run --platform linux/amd64 -d -p 80:80 --name $CONTAINER_NAME-container $CONTAINER_NAME-fastapi' > build_run_container.sh
    chmod +x build_run_container.sh 
    
  • cicd.sh
    echo 'CONTAINER_NAME=$1
    ./update_repo.sh
    ./remove_container.sh $CONTAINER_NAME
    ./build_run_container.sh $CONTAINER_NAME' > cicd.sh
    chmod +x cicd.sh 
    
  • 実行する
    ./cicd.sh hogehoge
    

FastAPIサービスを参照する

http://localhost:80/docs

FastAPIサービスを実行する

  • GETサービス
    http://localhost:80/hogehoge/idididididid/ほげはげでぶ
    
    :::details実行結果
    {"id":"idididididid","Features":{"ああああああああ":true,"あああああ":true,"ああああああ":true,"うううううう":true,"ああああああああ":true,"ああああああ":true,"ええええ":true,"うううう":true,"ええええ":true,"ええええ":"","ええええ":true,"えええええ":true,"ああああ":true,"ああああ":true,"ああああ":true,"ああああああああ/5以下)":true,"ああああ":true,"ああああ":true,"ああああ":true,"ああああ":true,"ああああ":true},"name":"ほげ","overview":{"mis_vis_val":"ああああ","num_of_emp":111111,"capital":11111111,"rep_name":"ああああ ああああ","biz_details":"ああああ\ああああ\ああああ","house_number":"2-22-55","establish_yyyy-mm-dd":"1111-11-11","zip_code":"1111-1111","pref_name":"東京都","city_name":"母区","town_name":"ぐぐぐぐ","tel_no":"","hp_url":"https://ssssssss.com/"}}
    
    :::

AWS EC2に展開

  • EC2インスタンスを開始し、ログインする
    aws ec2 describe-instances --region us-east-1 --query "Reservations[*].Instances[*].InstanceId" --output text --profile hrindconf
    aws ec2 start-instances --instance-ids i-dddddddddddddd --region us-east-1 --profile hrindconf
    cd ~/aws/mongodb
    ssh -i "~/aws/fastapi.pem" ec2-user@$(aws ec2 describe-instances --instance-ids i-dddddddddddddd --query "Reservations[*].Instances[*].PublicDnsName" --output text)
    
    実行結果
    A newer release of "Amazon Linux" is available.
    Version 2023.5.20240722:
    Version 2023.5.20240730:
    Version 2023.5.20240805:
    Version 2023.5.20240819:
    Version 2023.5.20240903:
    Run "/usr/bin/dnf check-release-update" for full release and version update info
     ,     #_
     ~\_  ####_        Amazon Linux 2023
    ~~  \_#####\
    ~~     \###|
    ~~       \#/ ___   https://aws.amazon.com/linux/amazon-linux-2023
     ~~       V~' '->
      ~~~         /
        ~~._.   _/
           _/ _/
         _/m/'
    Last login: Wed Jul 24 00:59:01 2024 from 58.70.100.115
    
  • AWS EC2 のインスタンスIDi-ddddddddddddddのパブリック IPv4 DNSを得るAWS CLIコマンド
    aws ec2 describe-instances --instance-ids i-dddddddddddddd --query "Reservations[*].Instances[*].PublicDnsName" --output text
    
  • EC2インスタンスからログアウトし停止する
    • EC2:ログアウト
      logout
      
    • ホスト:停止
      aws ec2 stop-instances --instance-ids i-dddddddddddddd --region us-east-1 --profile hrindconf
      
      実行結果
      {
        "StoppingInstances": [
            {
                "CurrentState": {
                    "Code": 64,
                    "Name": "stopping"
                },
                "InstanceId": "i-dddddddddddddd",
                "PreviousState": {
                    "Code": 16,
                    "Name": "running"
                }
            }
        ]
      }