Password Generator REST API with AWS Lambda and AWS API Gateway
Create a strong password. Specify the length as a request query parameter.
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.
- 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.
- Copy/paste the python code below into code editor
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.
- Set the API name as PasswordGeneratorLambdaSimpleProxy and then click 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.
- 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.
- Click Save.
API Testing on Postman
- Select Deploy API under Actions.
Set the stage name as test.
Click Deploy.
- 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.
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! ๐ ๐ป ๐ ๐ป ๐ ๐ป