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 123.201.57.39 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.

Occurrence
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

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{
    name:String
    marks:Number
    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:
writer.write(str(data))

exit(0)