I’m trying to do a CI/CD project to deploy a Flask application on an EC2 using the aws code pipeline and github, but I’m getting the following error during Deploy step in Code Pipeline. I tried several configurations in the buildspec file and I couldn’t solve it. When accessing the EC2 directory, none of my application files are being loaded. I understand that the error is due to searching for the file and it not existing, and in fact it doesn’t exist.
My project has the following structure:
my_flask_app/
├── app.py
├── requirements.txt
├── appspec.yml
├── buildspec.yml
├── scripts/
│ ├── install_dependencies.sh
│ ├── set_permissions.sh
│ ├── start_server.sh
│ └── validate_service.sh
app.py
from flask import Flask, request, render_template_string
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
result = ''
if request.method == 'POST':
num1 = float(request.form.get('num1'))
operator = request.form.get('operator')
num2 = float(request.form.get('num2'))
if operator == '+':
result = num1 + num2
elif operator == '-':
result = num1 - num2
elif operator == '*':
result = num1 * num2
elif operator == '/':
result = num1 / num2
else:
result = 'Operador inválido'
return render_template_string('''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculadora</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 50px;
}
input, select {
margin: 5px;
}
</style>
</head>
<body>
<h1>Calculadora Simples</h1>
<form method="POST">
<input type="number" name="num1" placeholder="Primeiro número" required>
<select name="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="number" name="num2" placeholder="Segundo número" required>
<button type="submit">Calcular</button>
</form>
<h2>Resultado: {{ result }}</h2>
</body>
</html>
''', result=result)
if __name__ == '__main__':
app.run(debug=True)
buildspec.yml
version: 0.2
phases:
install:
runtime-versions:
python: 3.9
# commands:
# - python -m venv venv
# - . venv/bin/activate
# - pip install --upgrade pip
# - pip install -r requirements.txt
pre_build:
commands:
- echo "Running tests..."
- python -m pytest tests/ || true
build:
commands:
- echo "Building the project..."
post_build:
commands:
- echo "Build completed"
artifacts:
files:
- '**/*'
- appspec.yml
- 'scripts/**/*'
discard-paths: true
name: my_flask_app.zip
appspec.yaml
files:
- source: /
destination: /home/ubuntu/my_flask_app
hooks:
BeforeInstall:
- location: scripts/install_dependencies.sh
timeout: 300
runas: ubuntu
AfterInstall:
- location: scripts/set_permissions.sh
timeout: 300
runas: ubuntu
ApplicationStart:
- location: scripts/start_server.sh
timeout: 300
runas: ubuntu
ValidateService:
- location: scripts/validate_service.sh
timeout: 300
runas: ubuntu
EC2 created, configured with CodeDeploy Agent running:
IAM Role assigned to the instance:
The role has the following permissions:
The artifacts that are created at the stage outputs remain without format (I thought they would be .zip as configured during pipeline creation)