bookmark_borderConfirm AWS Elastic Beanstalk Deployment Progress

Amazon Web Services Elastic Beanstalk

Use case:
You are using Amazon Web Service(AWS) Elastic Beanstalk to handle your deployment process via some git version control pipelines and there is a step that needs to run after your code has been deployed successfully.

There are a lot of solutions that can be used e.g CloudWatch, Lambda Function,.. etc. but I decided to invent my own solution that is easy, interesting and will cost 0$ :))



Idea:
Use a unique value on pipeline level (e.g pipeline build number) and add it to new code before upload it to server then call some endpoint or file to check this value. while this value does not equal out the new unique value in the pipeline then the deployment not finished yet and we should wait!


How to add new value to new code?
– we will create special endpoint or file in our project
– in the pipeline before upload new code to server change value of this file eg. deploy_version.txt or endpoint {api_link}/current_version
e.g: set some variable value with a new unique value. (you can do that using e.g sed Unix-like command)

ex.

we have a static endpoint that returns JSON result with the value of currentVersion variable that placed in e.g `version.js`

const currentVersion='123';

in the pipeline before upload code step:
- sed -i 2"s/currentVersion='123'/currentVersion='$BITBUCKET_BUILD_NUMBER'/g" src/version.js


How to validate deployment progress?

create a bash script that will send curl request to that endpoint to validate if current version from the endpoint(currently deployed code) is equal to new unique pipeline value or not.

how can this bash script look like? email me for more details(khaledalam.net@gmail.com)


bookmark_borderAllocate memory to work as swap space on VMs

When physical RAM is already in use, VM instances use swap space as a short-term replacement for physical RAM.

Contents of RAM that aren’t in active use or that aren’t needed as urgently as other data or instructions can be temporarily paged to a swap file. This frees up RAM for more immediate use.

Resolution

Calculate the swap space size

As a general rule, calculate swap space according to the following:

Amount of physical RAMRecommended swap space
2 GB of RAM or less2x the amount of RAM but never less than 32 MB
More than 2 GB of RAM but less than 32 GB4 GB + (RAM – 2 GB)
32 GB of RAM or more1x the amount of RAM
Note: Swap space should never be less than 32 MB.

In this example dd command, the swap file is 4 GB (128 MB x 32):  

$ sudo dd if=/dev/zero of=/swapfile bs=128M count=32

2.    Update the read and write permissions for the swap file:

$ sudo chmod 600 /swapfile

3.    Set up a Linux swap area:

$ sudo mkswap /swapfile

4.    Make the swap file available for immediate use by adding the swap file to swap space:  

$ sudo swapon /swapfile

5.    Verify that the procedure was successful:

$ sudo swapon -s

6.    Enable the swap file at boot time by editing the /etc/fstab file.

Open the file in the editor:

$ sudo nano /etc/fstab

Add the following new line at the end of the file, save the file, and then exit:

/swapfile swap swap defaults 0 0

Done )

run $ htop and check swp [ ] section.

bookmark_borderINSTALL AND RUN FLASK(PYTHON) ON UBUNTU 19.04 on VpsServer

Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks.


Step #0:
Signup (I appreciate if you use my referral link)
https://www.vpsserver.com/?affcode=21edd2c7bade
after your account becomes verified, create a Ubuntu 19.04 server.

Step #1:
Install python pip and flask by run:
$ sudo apt install python-pip
$ sudo apt install python3-pip
$ pip install -U Flask
$ pip3 install -U Flask

Step #2:
Add your project files on the server (ex. from GitHub using clone command)
in this tutorial let’s use a very simple flask app example that prints Hello, World! and save the app in (ex. /var/www/flask):

$ mkdir /var/www/flask
$ cd /var/www/flask
$ nano app.py
copy below code and paste it in app.py file using CTRL + SHIFT + V keys.

# app.py file

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, World!"

then press ( CTRL+ X )-> Y -> ENTER.


Step #3:
Run the server by run:
$ env FLASK_APP=app.py flask run -h xxx.xxx.xxx
change xxx.xxx.xxx to your server ip

output:

Serving Flask app "app.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://xxx.xxx.xxx:5000/ (Press CTRL+C to quit)