Building a Mirco-service with Lambda, API Gateway & S3
In this lab, we will creating a micro-service based webpage which shall button tied to API gateway which in turns tied to Lambda function which shall return some value as response whenever the button is clicked from frontend
Here’s an architectural diagram for it
- Let’s start creating this with Lambda function
- Input the Name of lambda function
Choose Python 3.7 as a runtime
Role: Create a new role from one or more template
And give the role a name
- Now copy below code inside lambda editor & save it
def lambda_handler(event, context): print("In lambda handler") resp = { "statusCode": 200, "headers": { "Access-Control-Allow-Origin": "*", }, "body": "Dabeer Shaikh" } return resp
- Now from service menu go to API Gateway service
- Choose below option and create an API Gateway
- After creation of API Gateway you will get a window like below
- Now from Actions lets create a method
- This shall be a GET method since we wanna get some response from our lambda function
- On left side after creating GET method, please do below setting and input your lambda function name
This ties your API Gateway with Lambda
- Now deloy your API
- You shall get an API URL try to open it, this shall invoke our lambda function and try to get response from the Lambda function
- Response from Lambda
- Now we need to intregate this API URL with webpage, for this download this WEBPAGE
Once the index.html file is downloaded open it in Notepad and replace the API URL with your API URL
- Now once you have edited the API URL in index.html page, upload that file to a S3 bucket
- Make the index.html file public and open it in web browser
- It will open up a page similar to below
Now if you click on “Click ME” you are hitting API URL which in turns shall invoke a lambda function and get a reponse from it
- This is response is from lambda
- You can delete all resources created during this lab