Get started building your own AI Application in 20 minutes

Instantly Get Up and Running

Start developing immediately with pre-configured environments tailored for OpenAI integration. The AMI includes an OpenAI-enabled Flask application installed in the home directory. Simply add your Secret Names and region as variables in the .env file, initiate the Flask application, and quickly start building your own AI application.

NIST CSF compliant development environment

The baked in demo application is integrated with AWS Secrets Manager out of the box, so your secrets are only pulled at runtime, ensuring security at all stages of the Software Development Lifecycle (SDLC). For a full list of the benefits of remote SSH development environments, click here.

Tutorial

Introduction

This tutorial is designed to help you get started with AI application development using the DevOpser Flask AMI integrated with OpenAI, AWS Secrets Manager, and Redis. In about 20 minutes, you will learn how to launch an AI application on AWS, configure your environment securely, and run a Flask application that leverages the power of OpenAI.


For those seeking a streamlined deployment process, we offer an open-source, production-ready Terraform configuration: the OpenAIFlask Quickstart. This configuration automatically deploys your application across two EC2 instances in separate subnets, utilizing an Application Load Balancer with Route53 DNS and SSL support. While this serves as an efficient interim solution for launching your AI application, we're excited to announce our upcoming DevOpser Platform for AI Webhosting, slated for release in Q4 2024. This revolutionary platform will simplify deployment to a single click, abstracting the underlying Terraform complexity. You'll be able to bring your AI application to life effortlessly, as if by magic, without diving into the technical intricacies.

Step 1: Subscribe to the AMI

Click here to navigate to AWS Marketplace and subscribe to the AMI.


  1. Create an AWS account if you don’t have one (a credit card is required).
  2. Click the button above or navigate to the AWS Marketplace and search for the DevOpser Flask AMI.
  3. Click on “Continue to Subscribe” and accept the user agreements.
  4. Click on “Continue to Configuration”

Step 2: Configure and Launch the EC2 Instance

  1. Select the AWS region closest to your users to reduce latency. For example, choose North Virginia.
  2. Click on “Continue to Launch” and then select “Launch through EC2.”
  3. In the EC2 launch template:
    • Name your instance (e.g., “OpenAI Flask”).
    • Select the AMI you just subscribed to.
    • Choose an appropriate instance type:
      • T3 Micro for low budget testing.
      • Medium or larger instances are recommended.
    • If you don’t have an SSH key pair, create one and store it securely (consider using a password manager like 1Password).
    • Select a security group that allows access on ports 80, 443, 8000, and 22 (limiting access to your IP address for security).
  4. Launch the instance and wait for it to initialize.

Step 3: Setup AWS Secrets Manager

  1. In your AWS console, search for “AWS Secrets Manager.”
  2. Click “Store a new secret.”
  3. Select “Other type of secret” and choose “Plaintext.”
  4. Enter your OpenAI API key without any additional formatting.
  5. Name your secret (e.g., “OpenAI_API_Key”) and note it down for later use.
  6. Create another secret for Flask with a randomly generated string.
  7. Ensure that both secrets are created in the same region as your EC2 instance.

Step 4: Connect to Your EC2 Instance

  1. Obtain the public IP address of your EC2 instance.
  2. Configure your SSH access:
    1. Install the Visual Studio Code Remote SSH extension.
    2. Edit your SSH config file to include:
    3. 
          Host openAI_flask
          HostName <Your_Public_IP>
          User ec2-user
          IdentityFile /path/to/your/key.pem
                                  
  3. Use the SSH Remote plugin to connect to your EC2 instance.

Step 5: Configure Python Environment

  1. Activate the Python 3.11 virtual environment:
  2. 
        cd openaiflask
        source /home/ec2-user/py3.11-venv/bin/activate
                            
  3. Install necessary dependencies inside the venv:
  4. 
        pip install -r app/requirements.txt
                            

Step 6: Configure Environment Variables

  1. Add the environment variables (AWS Secret Manager Secret Names):
  2. 
        cp app/.env.example .env
                            
  3. In the file tree, open the newly created .env file and add your secret names and region to the file:
  4. 
        FLASK_APP=run
        FLASK_ENV=development
        OPENAI_SECRET_NAME=<your-secret-name>
        FLASK_SECRET_NAME=<your-secret-name>
        REDIS_URL=redis://localhost:6379/0
        REGION=<your-region>
                            

Step 7: Launch the Flask Application

  1. Start the Flask application using Gunicorn:
  2. 
        gunicorn run:app
                            
  3. Wait approximately 30 seconds for the application to initialize.
  4. Verify the initialization by checking the logs for the OpenAI API key and Flask secret key.

Step 8: Test Your Application

  1. Open your browser and navigate to localhost:8000.
  2. Interact with the application and confirm that it responds appropriately (e.g., try asking it to tell a story).

Conclusion

You have successfully launched an AI application using the DevOpser Flask AMI, configured AWS Secrets Manager for secure API key storage, and set up a Python virtual environment for development. As you progress, consider implementing further security measures and exploring advanced configurations for production environments — we can help you with this with push button terraform deployments — just drop us a line at info@devopser.io. Happy coding!


Subscribe to the AMI