Showing posts with label AWS. Show all posts
Showing posts with label AWS. Show all posts

Tuesday, March 20, 2018

AWS - Listing users who have not enabled MFA for thier AWS account

Welcome Back!

Well, It's been years since I wrote my last blog.. Life is busy but there is nothing to complain about, as they say - Love the life you live and live the life you love!

Coming back to the topic; today's topic is about a task which I was trying to do since last couple of days..

Task was simple  - List the users who haven't enabled MFA for their AWS account. But integrating boto3 framework with Lambda was the trick.. Hence, thought of sharing the piece of code that I wrote for this task.

Note: You may run this with normal python IDE as well but that will require the AWS credentials.

I hope you will find it useful.

Python code for Lambda function:

##########################

import boto3


def lambda_handler(event, context):
    client = boto3.client('iam')
    users = client.list_users()

    user_list = []
    virutal_enabled =[]


    for key in users['Users']:
        user_list.append(str(key['UserName']))


    for key in users['Users']:
        List_of_MFA_Devices = client.list_mfa_devices(UserName=key['UserName'])
        for key in List_of_MFA_Devices['MFADevices']:
        virutal_enabled.append(str(key['UserName']))
     
    diff_list = [item for item in user_list if not item in virutal_enabled]

    for p in diff_list: print (p)

Tuesday, March 14, 2017

Procedure: Mount S3 Bucket on AWS EC2 Instance

A quick handy how-to:


How to mount S3 bucket on EC2 instance of AWS

S3FS is the S3 file system that will be required here.

Firstly update the packages on Ubuntu machine

Step 1:


# sudo apt-get install build-essential git libfuse-dev libcurl4-openssl-dev libxml2-dev mime-support automake libtool
# sudo apt-get install pkg-config libssl-dev
# git clone https://github.com/s3fs-fuse/s3fs-fuse
# cd s3fs-fuse/
# ./autogen.sh
# ./configure --prefix=/usr --with-openssl
# make
# sudo make install

Step 2:


S3FS needs AWS access key id and secret key to work. Create "passwd-s3fs" file at /etc/passwd-s3fs with content

<AWS Access Key ID>:<AWS Secret Access Key>

Step 3: Change permission


# chmod 640 /etc/passwd-s3fs

Step 4: Create mountpoint:


# mkdir <mount_point>

Step 5: Now mount the s3 bucket


# sudo s3fs bucketname /mys3bucket

Step 6: To check mounted s3 bucket


# df -Th /mys3bucket

# mkdir /mys3bucket

In most cases, you will want S3 to be mounted automatically after each reboot. So, add below line to /etc/fstab

# s3fs#<buket_name> /mys3bucket fuse allow_other,use_cache=/tmp/cache 0 0

For istance: s3fs#jarvis.pc /mys3bucket fuse allow_other,use_cache=/tmp/cache 0 0