Deploy simple app using Beanstalk CodePipeline
Beanstalk using CodePipeline
1 Create a basic Flask app (app.py)
create Environment then install e.g conda create -m venv python=3.10 -y
conda activate venv
Note always make sure you are in environment
Create file requirements.txt List the dependencies:such as Flask=2.1.1
Create a file app.py
pip install -r requirements.txt
from flask import Flask
app = Flask(name)
@app.route('/')
def hello_world():
return 'Hello, World!'
if name == 'main':
app.run(host='0.0.0.0', port=5000)
Step 1: Install Docker on Your SystemFor Windows:Download Docker Desktop from the Docker website.
Install Docker Desktop by following the installer instructions.
During installation, ensure "Install required components for WSL 2" is checked
Once installed, start Docker Desktop.
Confirm Docker is running:
Open a terminal (e.g., PowerShell) and run:
docker --version
This should display the installed Docker version.
Verify Docker InstallationOpen your terminal (PowerShell, Command Prompt, or Bash).
Run the Docker test image:docker run hello-world
This pulls a test image and verifies that Docker is working
Create a Dockerfile
Create a Dockerfile in the project directory:
Use an official Python runtime as a parent image
FROM python:3.9-slim
Set the working directory
WORKDIR /app
Copy the current directory contents into the container
COPY . /app
Install required Python packages
RUN pip install –no –cache -dir -r requirements.txt
Make port 5000 available to the world outside this container
EXPOSE 5000
Define the command to run the app
CMD ["python", "app.py"]
Add a .dockerignore FileCreate a .dockerignore file to exclude unnecessary files:
markdown
Copy code
pycache
*.pyc
*.pyo
Test the Docker Image LocallyBuild the Docker image:docker build -t flask-hello-world .
Run the Docker container:bashCopy codedocker run -p 5000:5000 flask-hello-world
Open your browser and navigate to http://localhost:5000 to see "Hello, World!".
Push the Code to GitHub
Initialize a Git repository in your terminal git init
Add and commit the code: git add.
git commit -m "Initial commit - Flask Hello World”
Deploy Using AWS Elastic Beanstalk and CodePipelineA. Create an Elastic Beanstalk ApplicationLog in to the AWS Management Console.
click I AM then create roles which will serve as instance profiles you will see roles on the left hand side check Base on AWS service under USE CASE choose Ec2 scroll down then click Next
Under Permissions Policies - look for Ec2FullAccess,
Go to Elastic Beanstalk and click Create Application.
Choose the Docker platform and proceed with the default settings.
Set Up CodePipelineCreate a New Pipeline:
Go to CodePipeline and click Create Pipeline.
Give your pipeline a name and choose the default service role.
Source Stage:
Select GitHub (version 2) as the source.
Connect your GitHub account and choose the repository and branch.
Build Stage:
Select AWS CodeBuild.
Create a new build project with the following buildspec (buildspec.yml):yamlversion: 0.2
phases:
build:
commands:
- echo "Building the Docker image..."
- docker build -t flask-hello-world .
Deploy Stage:
Choose Elastic Beanstalk as the deploy provider.
Select your Elastic Beanstalk environment.
Finish and Test:
Complete the pipeline setup and trigger the pipeline.
Visit the Elastic Beanstalk URL to confirm your app is running.