0% found this document useful (0 votes)
20 views3 pages

Flask Deployment Doc - Amen

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Flask Deployment Doc - Amen

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

AWS DynamoDB with Flask on AWS EC2 Instance:

Steps to be followed for the Installation:

Step 1:
sudo apt update
sudo apt install python3
pip install flask
Step 2: create a new file
vi app.py

from flask import Flask, render_template, request, redirect, url_for


import boto3
from boto3.dynamodb.conditions import Key
import uuid
import datetime

from dotenv import load_dotenv


load_dotenv() # This loads the variables from .env

app = Flask(__name__)

# AWS DynamoDB Setup


dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('BlogPosts')

@app.route('/')
def index():
# Fetch all blog posts
response = table.scan()
return render_template('index.html', posts=response['Items'])

@app.route('/post/<id>')
def post(id):
# Fetch a single post
response = table.query(
KeyConditionExpression=Key('post_id').eq(id)
)
return render_template('post.html', post=response['Items'][0])

@app.route('/create', methods=['GET', 'POST'])


def create():
if request.method == 'POST':
# Generate a unique ID for the new post
post_id = str(uuid.uuid4())

# Get form data


title = request.form['title']
content = request.form['content']
author = request.form.get('author', 'Anonymous') # Default to 'Anonymous' if no author
provided
tags = request.form['tags']
date = datetime.datetime.now().isoformat()

# Insert new post into DynamoDB


table.put_item(
Item={
'post_id': post_id,
'title': title,
'content': content,
'author': author,
'date': date,
'status': 'published',
'tags': tags
}
)

return redirect(url_for('index'))

return render_template('create.html')

# Add more routes for update and delete

if __name__ == '__main__':
app.run(debug=True)

vi .env.example

AWS_ACCESS_KEY_ID=your-access-key
AWS_SECRET_ACCESS_KEY=your-secret-access-key
AWS_DEFAULT_REGION=your-region

Step 3:
python -m venv venv

Step 4:
pip install -r requirement.txt
Step 5:
flask run

App will be running on http://localhost:5000.

You might also like