Difference between AWS Network ACL and Security Group

If you have many instances, managing the firewalls using Network ACL can be very useful. Otherwise, with Security group, you have to manually assign a security group to the instances.

State: Stateful or Stateless
Security groups are stateful: This means any changes applied to an incoming rule will be automatically applied to the outgoing rule. e.g. If you allow an incoming port 80, the outgoing port 80 will be automatically opened.
Network ACLs are stateless: This means any changes applied to an incoming rule will not be applied to the outgoing rule. e.g. If you allow an incoming port 80, you would also need to apply the rule for outgoing traffic.

Rules: Allow or Deny
Security group support allow rules only (by default all rules are denied). e.g. You cannot deny a certain IP address from establishing a connection.
Network ACL support allow and deny rules. By deny rules, you could explicitly deny a certain IP address to establish a connection example: Block IP address from establishing a connection to an EC2 Instance.

Rule process order
All rules in a security group are applied whereas rules are applied in their order (the rule with the lower number gets processed first) in Network ACL.
i.e. Security groups evaluate all the rules in them before allowing a traffic whereas NACLs do it in the number order, from top to bottom.

Defense order
Network ACL first layer of defense, whereas Security group is second layer of the defense for inbound/ingress traffic.
Security group first layer of defense, whereas Network ACL is second layer of the defense for outbound/egress traffic.

Subnet can have only one NACL, whereas Instance can have multiple Security groups.

credit: https://medium.com/awesome-cloud/aws-difference-between-security-groups-and-network-acls-adc632ea29ae

How to Sort a Dictionary by Value in Python

I have implemented the sorting of dictionary using the for loop and lambda function using the Test Driven Data / TDD approach.
Here is the simple code.

@Author: Aseem Jain
@Linkedin: https://www.linkedin.com/in/premaseem/
@Github: https://github.com/premaseem/pythonLab/tree/master/challenge

Sort map/dictionary by keys and values


def sol(m):
    s_k ={}
    s_v ={}
    # On 1
    for k in sorted(m.keys()):
        s_k[k] = m.get(k)

    for v in sorted(m.values()):
        for k in m:
            if v == m.get(k):
                s_v[k] = v

    return (s_k,s_v)

def sol1(m):
    s_k_t = sorted(m.items(),key=lambda i:i[0])
    s_v_t = sorted(m.items(),key=lambda i:i[1])

    s_k_m = {}
    for k,v in s_k_t: s_k_m[k]=v

    s_v_m = {}
    for k,v in s_v_t: s_v_m[k]=v

    return (s_k_m,s_v_m)

def sol2(m):
    s_k_t = sorted(m.items(),key=lambda i:i[0])
    s_v_t = sorted(m.items(),key=lambda i:i[1])
    def tuple_to_map(t):
        new_m = {}
        for k,v in t:
        return new_m
    return (tuple_to_map(s_k_t),tuple_to_map(s_v_t))

# Test with given map and expected sorted values 
map = { 3:15, 0:10, 5:50, 7:25 }
sorted_key = { 0:10, 3:15, 5:50, 7:25 }
sorted_values = { 5:50, 7:25, 0:10, 3:15}

test_data = [
    (map, (sorted_key,sorted_values) )

for given, expected in test_data:
    assert sol(given) == expected
    assert sol1(given) == expected
    print(f"Test passed for: given {given} and expected = {expected}")

Python: Data driven testing

I love doing TDD – Test driven development.

Sharing a small code construct to include variety of data set in a concise manner without depending on any library

def double(n):
    return n*2

test_data = [
    (2 ,4),
    (4 ,8),

for given, expected in test_data:
    assert expected == double(given)
    print(f"Test passed for: given {given} and expected = {expected}")

Output on console
Test passed for: given 2 and expectation = 4
Test passed for: given 4 and expectation = 8

How to sort custom objects in Type Script

Imagine you have a student class and you need to sort it reverse order of marks field to calculate ranks of students in the class.

class Student{
    constructor(name:string, marks:number) {
        this.name = name
        this.marks = marks

Student objects as part of the array. 

var students:Array<Student> = [
    new Student("aseem",47),
    new Student("prem",97),
    new Student("john",100)


sort students by comparing them based on marks field. 

console.log(students.sort( (a,b)=> a.marks > b.marks ? -1:1 ))

here, we used the arrow function which takes a and b objects as params, then we used the ternary operator to compare marks and return the number as -1 or 1 as the result of comparison. 

Is semicolon needed in Java Script? horror of ;

Short answer is NO in recent times, here is why
The semicolon is used to separate statements, but it can be omitted if the statement is followed by a line break.
The semicolon is needed, only when you have two or more statements on the same line, not other wise 🙂 

    var i = 0; i++        // <-- semicolon obligatory
                          //     (but optional before newline)
    var i = 0             // <-- semicolon optional
        i++               // <-- semicolon optional

Python script to take mongodb backup

@Author: Aseem Jain
@profile: https://www.linkedin.com/in/premaseem/

import os
import pymongo

# configure credentials / db name
db_user = os.environ["MONGO_ATLAS_USER"]
db_pass = os.environ["MONGO_ATLAS_PASSWORD"]
db_name = "sample_mflix"

connection_string = f"mongodb+srv://{db_user}:{db_pass}@sharedcluster.lv3wx.mongodb.net/{db_name}?retryWrites=true&w=majority"

client = pymongo.MongoClient(connection_string)
db = client[db_name]

# create database back directory with db_name
os.makedirs(db_name, exist_ok=True)

# list all tables in database
tables = db.list_collection_names()

# dump all tables in db
for table in tables:
print("exporting data for table", table )
data = list(db[table].find())
# write data in json file
with open(f"{db.name}/{table}.json","w") as writer:


User defined custom object sorting in python

@Author: Aseem Jain
@profile: https://www.linkedin.com/in/premaseem/

Title: Sort the class or custom object and not regular int / string using in build sort funcitons
Sort custom object can be done in 2 ways
1. Static way : class implementing < less then __lt__(self,other)
2. Dynamic way is by providing lambda func with key.
class User:
def __init__(self,name,age):
self.name = name
self.age = age

def __repr__(self):
return self.name + str(self.age)

def __lt__(self, other):
return self.age < other.age

# Sorting String
ls = ["zeema", "aseemjain", "see",str(3),str(13),"$$"]
#output: ['$$', '3', 'aseemjain', 'see', 'zeema']

# Reverse soring
# output: ['zeema', 'see', 'aseemjain', '3', '$$']

# sorting Custom object
users = [User("aseem",16),User("aseemprem",56),User("premaseem",23),User("zseem",36),User("nseem",106) ]
users.sort(key = lambda x: x.name)
# output : [aseem16, aseemprem56, nseem106, premaseem23, zseem36]

# create new sorted list
newlist = sorted(users, key=lambda x: x.age, reverse=True)
# output: [nseem106, aseemprem56, zseem36, premaseem23, aseem16]

Github link: https://github.com/premaseem/pythonPlayground/blob/master/algorithm_and_datastructure/sorting/custom_object_sorting.py

Yoga Instructor Certificate

I am now a certified Yoga Teacher, thanks to Sri Sri Yoga, a certified school under the Ministry of AYUSH, Govt. of India & Yoga Alliance Registered Yoga School. I scored 97% marks in exams.

It was a 200 hour, 8 weeks online Yoga teachers training program. I was able to lose extra weight from my body and mind ( ie stress / Chitta Vritattis / Vikalpa) and got an entirely new way of life. All my digestion related issues are gone. I feel calmer throughout the day. Learned so much about my own body ( muscles, bone, ligaments, tendons ) and mind (modulations, stresses, obstacles).

After finishing the training, I realized why the entire world is learning and practicing yoga to maintain their mental and physical health.

If you’re interested in learning, how Yoga can help you, feel free to contact me.

My online yoga studio link: https://yogawithprem.wordpress.com/

My Facebook yoga page: https://www.facebook.com/fitnesswithprem

My youtube channel link: https://www.youtube.com/channel/UCbor429hZLmDeL7nzz8eQQw

With love,
Prem Aseem Jain

Wish you Happy New Year 2021

I wish you all a happy and healthy new year 2021.

Tell me what do you feel about 2020 in the comments.

2020 wasn’t bad for me, it was just different and taught us a lot especially how to leverage technology to connect and get the job done.

I used 2020 to go within myself

I felt a deeper connection with family members in 2020.

I also finished another programming certification in 2020.

I finish Sri sri yoga teachers training program, which helped me get rid of extra weight on the body and stress on the mind.

I did several Art of living meditation programs

I hope 2021 would come with freedom without restriction to wear masks 😉

#newyear2020 #programming #yoga #mentalhealthmatters #premaseem