Password Generator REST API with AWS Lambda and AWS API Gateway

Password Generator REST API with AWS Lambda and AWS API Gateway

Create a strong password. Specify the length as a request query parameter.

ยท

3 min read

Introduction ๐Ÿ‘‹

Password Generator REST API leverages AWS Lambda proxy integration and AWS API Gateway to generate a strong password (not less than 8 characters) on each API request. The API user may specify the length of the desired password. A Python function on AWS Lambda generates the password using a combination of upper case, lower case, numbers and special characters.

Project Demo ๐ŸŽฅ

Objectives ๐Ÿ˜‡

  • Create a password-generating Lambda function for the API backend.
  • Create and test a Password Generator API with Lambda proxy integration.

Prerequisites ๐Ÿ“Œ

  • AWS IAM user account with management console access.
  • Postman

The Development ๐Ÿ’ป ๐Ÿ’ป

Create Password Generator AWS Lambda Function

The Lambda function returns the JSON response for a request with a valid password length:

{
    "Your password is: " + password + " Thanks for using the Password Generator"
}

The Lambda function returns the JSON response for a request with an invalid password length:

{
    "Your password must have at least 8 characters"
}
  • Open Lambda service on the AWS Management Console. Ensure that you are signed into your IAM user account. I am using US East (N. Virginia) us-east-1 region. Click on Functions on the left side panel and click on Create Function.
  • Select Author from scratch. In Basic information, set the Function name as PasswordGeneratorLambdaProxyIntegration. Select Python 3.9 as the function's language. Lambda basic information
  • In Permissions, open Change default execution role dropdown. Select Create a new role from AWS policy templates. Set the role name as PasswordGeneratorLambdaBasicExecutionRole.
  • Click Create Function.

Create Lamda Function

  • Copy/paste the python code below into code editor update lambda code.png
import json
import random
import string

def lambda_handler(event, context):

    print("Welcome to Password Generator")

    # Set the password length with constraints for the password length
    password_length = int(event['queryStringParameters']['length'])
    if password_length < 8:
        return {
        'statusCode': 200,
        'body': json.dumps('Your password must have at least 8 characters.')
    }
    else:
        # define the characters
        lower = string.ascii_lowercase
        upper = string.ascii_uppercase
        num = string.digits
        symbols = string.punctuation

        all_characters = lower + upper + num + symbols

        # generate a password string
        temp = random.sample(all_characters, password_length)
        password = "".join(temp)    

        return {
        'statusCode': 200,
        'body': json.dumps("Your password is: " + password + " Thanks for using the Password Generator")
    }
  • Click Deploy

Create Password Generator API

  • Open the AWS API Gateway console.
  • Select Create API. Select a type of API. For this project, we are using a public REST API. Select Build. choose API type
  • Set the API name as PasswordGeneratorLambdaSimpleProxy and then click Create API. create API
  • In Resources on the left-hand panel, Create Resource for the root / of the API under Actions. Set the Resource Name as generatepassword. create resource
  • Select /generatepassword from the resource list. Click Actions. Click Create Method. Select ANY and click on the checkmark.
  • Select Use Lambda Proxy integration. Type in characters in Lambda Function text box. This generates a dropdown of your Lambda functions. Select PasswordGeneratorLambdaProxyIntegration. create method
  • Click Save.

    API Testing on Postman

  • Select Deploy API under Actions.
  • Set the stage name as test. test stage

  • Click Deploy.

invoke URL

  • Copy the invoke URL and paste it on Postman for testing. Include the name of the resource on the URL. Set the query parameters for the password length.

password length error

password length valid

References ๐Ÿ“™

Conclusion ๐Ÿ”š

Test the project URL using the web browser as well. Set the appropriate query parameter for the password length. y5r9r96qdc.execute-api.us-east-1.amazonaws...

Comments, questions or suggestions related to this tutorial? Please share them with me. I'll be delighted to hear from you.

Cheers! ๐ŸŽŠ ๐Ÿป ๐ŸŽŠ ๐Ÿป ๐ŸŽŠ ๐Ÿป

Did you find this article valuable?

Support Uffa Modey by becoming a sponsor. Any amount is appreciated!

ย