Skip to content

Commit

Permalink
feature/deployment with copilot (#1)
Browse files Browse the repository at this point in the history
* Use Python 3.10

* Deploy the app with copilot

* Set up the local development server
  • Loading branch information
nakamichiworks authored Feb 5, 2023
1 parent 4f752ee commit 3492f40
Show file tree
Hide file tree
Showing 22 changed files with 267 additions and 131 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.envrc
.venv
notebooks/
9 changes: 3 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
app/.chalice/deployed/
notebooks/
.envrc
# Should be generated from config.template.json
config.json
# Should be generated from poetry.lock
requirements.txt
notebooks/
# Should be generated from manifest.template.yml
copilot/**/manifest.yml


# Byte-compiled / optimized / DLL files
Expand Down
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.9
3.10
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM python:3.10.9-slim-bullseye

WORKDIR /code

ENV POETRY_HOME="/opt/poetry"
ENV PATH="$POETRY_HOME/bin:$PATH"

RUN apt-get update && \
apt-get install --no-install-recommends -y curl && \
curl -sSL https://install.python-poetry.org/ | python -

COPY ./pyproject.toml /code/pyproject.toml
COPY ./poetry.lock /code/poetry.lock
RUN poetry config virtualenvs.create false && \
poetry install --only main

RUN curl -sSL https://install.python-poetry.org | python3 - --uninstall && \
apt-get remove -y curl && \
apt-get autoremove -y && \
apt-get clean

COPY ./app /code/app

EXPOSE 80
CMD ["uvicorn", "app.app:app", "--host", "0.0.0.0", "--port", "80"]
33 changes: 13 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,26 @@

1. Install python packages by `poetry install`.

2. Create your slack app, install it to your slack workspace, and get the bot token and signing secret. For details, read [the official tutorial](https://slack.dev/bolt-python/tutorial/getting-started-http).
2. Create your slack app, install it to your slack workspace, and get the bot token and signing secret. For details, read [the Slack official tutorial](https://slack.dev/bolt-python/tutorial/getting-started-http).

## Development

## Deployment
Run the deployment script.
You can run a development server by the following command.

- Development environment
```bash
./scripts/run_dev_server.sh
```

```bash
export SLACK_BOT_TOKEN_DEV=<your-dev-bot-token>
export SLACK_SIGNING_SECRET_DEV=<your-dev-signing-secret>
export OPENAI_ORGANIZATION=<your-org>
export OPENAI_API_KEY=<your-api-key>
./deploy.sh dev
```
## Deployment

- Production environment
The app is deployed to AWS AppRunner by using [`copilot`](https://aws.github.io/copilot-cli/) command.

```bash
export SLACK_BOT_TOKEN=<your-bot-token>
export SLACK_SIGNING_SECRET=<your-signing-secret>
export OPENAI_ORGANIZATION=<your-org>
export OPENAI_API_KEY=<your-api-key>
./deploy.sh prod
```
```bash
./scripts/update_manifest.sh
copilot svc deploy
```

After the first deployment, properly configure the settings below. Follow [the official tutorial](https://slack.dev/bolt-python/tutorial/getting-started-http#setting-up-events) for details.
After the first deployment, properly configure the settings below. Follow [the Slack official tutorial](https://slack.dev/bolt-python/tutorial/getting-started-http#setting-up-events) for details.

- Interactivity & Shortcuts Request URL
- Event Subscriptions Request URL
Expand Down
32 changes: 0 additions & 32 deletions app/.chalice/config.template.json

This file was deleted.

26 changes: 10 additions & 16 deletions app/app.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
from chalice import Chalice
from slack_bolt.adapter.aws_lambda.chalice_handler import ChaliceSlackRequestHandler
from fastapi import FastAPI, Request
from slack_bolt.adapter.fastapi import SlackRequestHandler

from chalicelib.slack import bolt_app
from .lib.slack import bolt_app

app = Chalice(app_name="openai-bot")
slack_handler = ChaliceSlackRequestHandler(bolt_app, app)
slack_handler.clear_all_log_handlers()
app = FastAPI()
slack_handler = SlackRequestHandler(bolt_app)


@app.route("/_health")
def health():
@app.get("/_health")
async def health():
return {"status": "ok"}


@app.route(
"/slack/events",
methods=["POST"],
content_types=["application/x-www-form-urlencoded", "application/json"],
)
def slack_events():
request = app.current_request
@app.post("/slack/events")
async def slack_events(request: Request):
if request.headers.get("X-Slack-Retry-Num") is not None:
return {"status": "retry request ignored"}
response = slack_handler.handle(request)
response = await slack_handler.handle(request)
return response
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions copilot/.workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application: openai-bot
61 changes: 61 additions & 0 deletions copilot/backend/manifest.template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# The manifest for the "backend" service.
# Read the full specification for the "Request-Driven Web Service" type at:
# https://aws.github.io/copilot-cli/docs/manifest/rd-web-service/

# Your service name will be used in naming your resources like log groups, App Runner services, etc.
name: backend
# The "architecture" of the service you're running.
type: Request-Driven Web Service

image:
# Docker build arguments.
# For additional overrides: https://aws.github.io/copilot-cli/docs/manifest/rd-web-service/#image-build
build: Dockerfile
# Port exposed through your container to route traffic to it.
port: 80

# http:
# healthcheck:
# path: /
# healthy_threshold: 3
# unhealthy_threshold: 5
# interval: 10s
# timeout: 5s

# Number of CPU units for the task.
cpu: 1024
# Amount of memory in MiB used by the task.
memory: 2048

# Connect your App Runner service to your environment's VPC.
# network:
# vpc:
# placement: private


# Enable tracing for the service.
# observability:
# tracing: awsxray

# Optional fields for more advanced use-cases.
#
variables: # Pass environment variables as key value pairs.
LOG_LEVEL: info
SLACK_BOT_TOKEN: {{ SLACK_BOT_TOKEN }}
SLACK_USER_TOKEN: {{ SLACK_USER_TOKEN }}
SLACK_SIGNING_SECRET: {{ SLACK_SIGNING_SECRET }}
OPENAI_ORGANIZATION: {{ OPENAI_ORGANIZATION }}
OPENAI_API_KEY: {{ OPENAI_API_KEY }}

#
# tags: # Pass tags as key value pairs.
# project: project-name

# You can override any of the values defined above by environment.
# environments:
# dev:
# variables:
# LOG_LEVEL: debug
# SLACK_BOT_TOKEN: {{ SLACK_BOT_TOKEN_DEV }}
# SLACK_USER_TOKEN: {{ SLACK_USER_TOKEN_DEV }}
# SLACK_SIGNING_SECRET: {{ SLACK_SIGNING_SECRET_DEV }}
21 changes: 21 additions & 0 deletions copilot/environments/dev/manifest.template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The manifest for the "test" environment.
# Read the full specification for the "Environment" type at:
# https://aws.github.io/copilot-cli/docs/manifest/environment/

# Your environment name will be used in naming your resources like VPC, cluster, etc.
name: dev
type: Environment

# Import your own VPC and subnets or configure how they should be created.
# network:
# vpc:
# id:

# Configure the load balancers in your environment, once created.
# http:
# public:
# private:

# Configure observability for your environment resources.
observability:
container_insights: false
8 changes: 0 additions & 8 deletions destroy.sh

This file was deleted.

Loading

0 comments on commit 3492f40

Please sign in to comment.