Created
October 28, 2024 05:06
-
-
Save vik-y/bb5077b64a42a6b008aae533a9c8ca07 to your computer and use it in GitHub Desktop.
Sample ChatGpt AWS CLI bot
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
# Use an official Python runtime as a parent image | |
FROM python:3.10-slim | |
# Set the working directory in the container | |
WORKDIR /app | |
# Copy the requirements file to the container | |
COPY requirements.txt . | |
# Install the dependencies | |
RUN pip install --no-cache-dir -r requirements.txt | |
RUN apt-get update && \ | |
apt-get install -y \ | |
unzip \ | |
curl \ | |
&& apt-get clean \ | |
&& curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" \ | |
&& unzip awscliv2.zip \ | |
&& ./aws/install \ | |
&& rm -rf \ | |
awscliv2.zip \ | |
&& apt-get -y purge curl \ | |
&& apt-get -y purge unzip | |
# Copy the current directory contents into the container at /app | |
COPY . . | |
# Expose port 8000 to the outside world | |
EXPOSE 8000 | |
# Run the FastAPI application using Uvicorn | |
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] |
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
import subprocess | |
from typing import List | |
from fastapi import FastAPI, HTTPException | |
from pydantic import BaseModel, validator | |
from fastapi import Header, Depends | |
from typing import Optional | |
app = FastAPI() | |
API_TOKEN = "secure_api_token" | |
# Pydantic model for the input | |
class AWSCommand(BaseModel): | |
command: str | |
args: List[str] | |
def verify_token(token: Optional[str] = Header(None, alias="x-api-key")): | |
if token is None or token != API_TOKEN: | |
raise HTTPException(status_code=401, detail="Unauthorized") | |
return token | |
@app.post("/run-aws-cli") | |
def run_aws_cli(command_data: AWSCommand, token: str = Depends(verify_token)): | |
try: | |
# Construct the AWS CLI command | |
cli_command = [command_data.command] + command_data.args | |
# Execute the command and capture output | |
result = subprocess.run(cli_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) | |
if result.returncode != 0: | |
raise HTTPException(status_code=500, detail=f"Error: {result.stderr}") | |
return {"output": result.stdout} | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app, host="0.0.0.0", port=8000) |
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
fastapi | |
uvicorn | |
pydantic | |
boto3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment