Autoscaling With Lambda Part-2

Autoscaling With Lambda Part-2

This blog is a continuation of my previous blog, if you haven't checked that out then click Here.

In this part, we will create an API Gateway to trigger our autoscaling lambda every time our URL gets hit.

Step-1 Make a Lambda function to trigger our Step Function machine which we have created in our previous blog.

Login into your AWS account, go to Lambda service -> Click on create function -> select Author from scratch

Write Function name -> select Runtime (I'll be using python 3.9 ) -> click Architecture (I'll be using x86_64) and finally select appropriate execution role ( I will suggest creating a new role and giving necessary permission to it.

Step-2 Write code to complete the Lambda function

Go to the Code tab and paste the below code -> click on Deploy

import boto3
import json

def lambda_handler(event, context):

    sf = boto3.client('stepfunctions', region_name = 'Region')

    response = sf.start_execution(
    stateMachineArn = 'ARN of your Step Function Machine',
    )

In the above code, put the Region and ARN of the Step Function. You can test out the above Lambda as well and it should launch your Step Function without any hassle.

Step-3 Create an API Gateway

To trigger our Step Function Lambda by our domain we need to create an API Gateway. For now, I'm creating an HTTP API.

Go to API Gateway service -> Click Create API -> Select Build on the HTTP API section.

Then click Add integration -> select Lambda from drop-down -> Select AWS Region then Select Lambda Function which we have created to run our Step Function, finally give your API a name and hit Next.

In the next screen, you will have to Configure Route and choose Method as ANY -> Resource path as "/" -> Integration target should be your Lambda name and hit Next.

Now we will Define Stages Tab and leave everything as it is.

Lastly, we will see Review and Create Tab just hit Create.

Now you have an API click on it and you will see an Invoke URL inside. Hit that URL and your Step Function should run and launch a new EC2 and add that to your desired ASG.

Step-4 Mapping API to a Custom Domain

After Step 3 we can either hit that Invoke URL directly from our code or some other method or we can map that to our Custom Domain.

Mapping it is really easy thanks to AWS.

Again to go the API Gateway service and click Custom domain names then click on Create.

Now type the Custom domain name inside the Domain name section-> select TLS 1.2 -> Endpoint type as Regional and make sure you select the right certification inside the ACM Certificate section, that certificate should be associated with the Custom Domain Name you have chosen above.

Finally hit Create domain name.

The above Step will create a Custom domain name, now select that domain name and go to the API mapping section -> click Configure API mappings

Then Add new mapping select the API we made above -> Stage as $default and hit Save.

Now go to Route53 service -> Select your Hosted Zone -> to create an A-type record choose Alias -> Route traffic to Alias to API Gateway API -> Choose Region then Choose endpoint we have created above.

Finally hit Create records. Now, wait for a few seconds before your record become available and then hit the Domain Name and see your API working and triggering all the Lambda.

We are finally done!!!!!!!!!

We have completed all the steps successfully and I'm so glad that you followed me till the end. Now if I made any mistakes then let me know or anything I can improve on(Afterall we all are human).

ONCE AGAIN THANK YOU SO MUCH FOR READING THIS!!!!!

SEE YOU GUYS IN THE NEXT ONE.