CyberKeeda In Social Media

Creating Random password generator using AWS Lambda Function


AWS Lambda


AWS Lambda is an serverless computing platform served by Amazon  Amazon Web Services. It is a PAAS computing service that runs code in response to events and automatically manages the computing resources required by that code.



So this tutorial is just a walk through to
  • How to run your python code from AWS Lambda
  • How to make use of AWS lambda to create tiny python application
  • Python based random strong password generator hosted on AWS Lambda.
  • How to create AWS Lambda Function for Python

Let's begin.
In order to run python code from Lambda function, the first thing we do is to create a Handler within Lambda function, which will trigger the ineer python function using event and context

A basic lambda function handler looks like the below code.

def handler_name(event, context): 
    ...
    return some_value

Now let's focus on the above code.

  • handler_name : type (String ) It could be anything, but there is great use of it further.
  • event : type (dictionary) It's the way through which we can feed variable to our inner python code/programme.
  • context : In simple terms it will be used for the logging and debugging purpose.
Above things are just theory, i'm quite sure once i let you know how to run our python code using above defined parameters, you will use it then further for you.

Lets toggle down to Lambda Service from AWS Console and create a function.

Services --> Compute --> Lambda --> Functions --> Create Function


















Select Author from Scratch and proceed to fill the basic information for your new lambda function and then click on create function.

Function name : Enter a name that describes the purpose of your function, it must be anything.
Here i will be creating a lambda function to generate random password, hence we will name it as "Password_Generator"

Run time : Choose your desired language, we will  python 3.7 here.

Permissions : Lambda function need appropriate IAM role to run, for now choose option "Create a new role with basic lambda permissions"

Permissions --> Choose or create execution role --> Create a new role with basic lambda permissions --> Create Function




Once function created, it will redirect into the below looking page.




Scroll down to section "Function code" and let's paste our python code within the lambda handler function.

from random import choice

def lambda_handler(event, context):
    # TODO implement
    keyboard_char='abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*_-'

    
    
    
    def Gen_Strong_Pass(pass_len,pass_no):
        
        cast_pass_len=int(pass_len)
        cast_pass_no=int(pass_no)
        
        for i in range(pass_no):
            if i == 0:
                print('Here are your', cast_pass_no, 'random', cast_pass_len, 'digit tpasswords ! \n')
            
            hard_pass=''
            for j in range(cast_pass_len):
                hard_pass+=choice(keyboard_char)
            print(hard_pass+'\n')

    Gen_Strong_Pass(event['pass_len'],event['pass_no'])

The top most mandatory function "lambda_handler" can be replaced by anything, and the same must be entered within Handler, inlined to the next text after lambda_function
as to look like lambda_function.lambda_handler




















For example, if we want to name our function lambda_handler as my_lambda_handler.

Handler on the top box must be replaced by lambda_function.my_lambda_handler

We are ready now lets scroll up to "Configure test events"  and add our events, what is events ? a way to define variables using python dictionary... more is written on top.





Now let's replace Event name and the key-value pairs by our own inputs.

Event name : We will name it as "Generate8digit3password" and paste the required key value pairs by our own.


You might be wondering why this key value pair and where it would be used !

Events defined within test events can be called within lambda function as
event['name_of_your_key']  as we defined two key-values as pass_len and pass_no
let's look into our code and the red boxes will give you the idea, where the event values are called.

























Once Test event is created, it will be visible under test events drop down.













Now, we are ready  !

Go back to your function "Password_Generator" choose from your recent saved test events.
and click on test.

Once completed, expand logs to look into log output to see code output as below.





















Conclusion : We have created a simple password generator using python, deployed it into AWS PASS services named as Lambda.
We got a run time infrastructure without any setup.

Tip : You can save more test events like, 9 digit, 16 digit password generator and use it as per your requirements.


1 comment:

  1. "random.choice" is not the best thing to use for generating passwords.

    Python's "secrets" module would be a better option: https://docs.python.org/3/library/secrets.html#secrets.choice

    ReplyDelete

Designed By Jackuna