Tech Behind It

Using AWS for API Development and Deployment

AWS for API Development and Deployment

What would our life be without APIs? Probably some horror movie if we consider everything they do for us! An Application Programming Interface, or API, is a way to seamlessly connect different systems that need to collaborate and work with one another.

APIs have become essential in today’s highly digitized world, which is why a lot of companies rely on them to build their desired digital environments. There are plenty of APIs available in the market today, so chances are you can search and find the one that suits your needs. But what happens when you can’t find a perfect match? Well, you obviously build it!

To make your API dream come true, we’ve compiled a list of instructions on how best to create an API using the Amazon Web Services (AWS) management console.

How to create API with AWS

The codes we’ve prepared for you are very straightforward, but we’ll guide you step by step nonetheless so you can take better advantage of them.

The very first step in creating an API is creating the Lambda function. To do this, log in to your  AWS Management Console and click on Lambda to create a new function. This will open a new window with three options. Go for the author from scratch and name the function. By doing so, you’ll have the API create the password for you.

Now that you’ve created the Lambda function, you should go to Function Code and write the function you desire:

import json

import random

def generate_password(length, n_capitals, n_numbers):

capitals = random.choices(‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’, k = n_capitals)

numbers = random.choices(‘0123456789’, k = n_numbers)

letters = random.choices(‘abcdefghijklmnopqrstuvwxyz’, k = length – n_capitals – n_numbers)

password = capitals + numbers + letters

random.shuffle(password)

return str(“”.join(password))

def lambda_handler(event, context):

if not ‘length’ in event or not ‘n_capitals’ in event or not ‘n_numbers’ in event:

return {

‘statusCode’: 200,

‘body’: json.dumps(‘Error: Please specify all parameters (length, n_capitals, n_numbers).’)

}

password = generate_password(event[‘length’], event[‘n_capitals’], event[‘n_numbers’])

return {

‘statusCode’: 200,

‘body’: password

}

To do this, go to the Test menu and click on Configure test events from the dropdown list. This will open a new window with two options. After choosing the Create new test event and naming it, you’ll have to specify the input for your test in the dictionary. Finally, you can click on create.

The brief description could look like this:

{

“length”: 10,

“n_capitals”: 2,

“n_numbers”: 2

}

Now it’s time for the final test. Simply click on the Test button, and you should see the password in the dictionary “body”.

And now, it’s time for REST. That’s right, in capital letters, not the real rest, so you’re not done yet. Open the AWS Management Console and go to API Gateway. Below the WEBSocket API, you’ll see the REST API box. Click on it, and you’ll get to create a New API and choose a name for it.

The following step is creating a POST method. Go for the Lambda Function integration type, and give it a name.

Once you’re done, you should see four Response boxes nicely outlined.

Then, go to Actions (next to Resources) on the top bar, and choose Deploy API from the dropdown menu. You can name it “fond”, for instance. After you click Deploy, the API Gateway should present a URL to find your API.

There’s no Comment starting automated testing here, so you’ll have to do it on your own. To make sure you’ve done everything right, you can copy this code into your Python Notebook. Don’t worry,, though — this will make the API send you a new password.

import requests

import json

securePasswordGeneratorAPI = “https://70kalhsa3c.execute-api.eu-west-3.amazonaws.com/fond”

data = {

“length”: 10,

“n_capitals” :2,

“n_numbers”: 2

}

response = requests.post(securePasswordGeneratorAPI, json.dumps(data))

resp = json.loads(response.content)

generatedPassword = resp[“body”]

print(generatedPassword)

It’s not too hard, is it? Yet, it brings so many advantages.

API business benefits

Some of the main reasons why you’re going to find APIs so useful are:

Building a REST API with AWS is a real piece of cake with the above code, and you can get to enjoy the advantages relatively quickly.

Exit mobile version