Mastering Serverless: Deploying Applications with AWS Lambda and API Gateway
Overview :-
Deploying a serverless application using #AWS Lambda and #API Gateway involves several steps. Deploying a simple “Hello World” serverless application using AWS Lambda and API Gateway.
Deploying a #serverless application using AWS Lambda and API Gateway involves several steps. In this example, I’ll guide you through deploying a simple “Hello World” serverless application using AWS Lambda and API Gateway.
Step 1: Create a #Lambda Function
Log in to the #AWS Management Console.
Go to the Lambda service.
Click “Create function.”
Choose “Author from scratch.”
Fill in the following details:
Function name: Enter a name for your Lambda function (e.g., “HelloWorld”).
Runtime: Select the runtime for your function (e.g., Node.js, Python, etc.).
Execution role: Create a new role with basic Lambda permission
Click “Create function.”
In the function code section, replace the default code with a simple “Hello World” function:
# lambda_function.py
def handler(event, context):
response = {
"statusCode": 200,
"body": "Hello, World from Python Lambda!"
}
return response
Step 2: Install Terraform
Make sure you have Terraform installed on your system. You can download it from the official website: https://www.terraform.io/downloads.html
Step 3: Write #Terraform Configuration
Create a file named main.tf
and add the following configuration:
provider "aws" {
region = "us-east-1" # Update with your desired region
}
# main.tf
resource "aws_lambda_function" "hello_lambda" {
filename = "lambda_function.zip"
function_name = "HelloWorld"
role = aws_iam_role.lambda_role.arn
handler = "lambda_function.handler" # Update the handler for Python
runtime = "python3.8" # Python runtime
source_code_hash = filebase64sha256("lambda_function.zip")
}
resource "aws_iam_role" "lambda_role" {
name = "lambda_execution_role"
assume_role_policy = jsonencode({
Version = "2012–10–17",
Statement = [
{
Action = "sts:AssumeRole",
Effect = "Allow",
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
}
resource "aws_apigatewayv2_api" "hello_api" {
name = "HelloWorldAPI"
protocol_type = "HTTP"
}
resource "aws_apigatewayv2_integration" "lambda_integration" {
api_id = aws_apigatewayv2_api.hello_api.id
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.hello_lambda.invoke_arn
}
resource "aws_apigatewayv2_route" "hello_route" {
api_id = aws_apigatewayv2_api.hello_api.id
route_key = "GET /hello"
target = "integrations/${aws_apigatewayv2_integration.lambda_integration.id}"
}
resource "aws_apigatewayv2_stage" "prod_stage" {
api_id = aws_apigatewayv2_api.hello_api.id
name = "prod"
}
# outputs.tf
output "api_gateway_url" {
value = "https://${aws_apigatewayv2_api.hello_api.id}.execute-api.us-east-1.amazonaws.com/${aws_apigatewayv2_stage.prod_stage.name}"
}
Step 3: Deploy with #Terraform
In your terminal, navigate to the directory containing your main.tf
file and run the following commands:
terraform init
terraform apply
Run terraform apply
again. Once the deployment is complete, #Terraform will display the output values, including the #API Gateway URL.
Use a tool like curl
or a web browser to access the #API Gateway URL followed by the path to your route.
You should receive a “Hello, World!” response.
We can verify it by checking the #API Gateway in AWS Console
Source-code Link :- “github.com/MahiraTechnology/Mahira-medium.git”
Conclusion :-
This process empowers us to rapidly develop, deploy, and scale applications, all while keeping our costs in check. The combination of Lambda’s event-driven architecture and API Gateway’s ability to securely expose our functions as HTTP endpoints has opened up a world of possibilities for building resilient and efficient serverless applications.
The future of software development lies in serverless architecture, and by understanding how to deploy applications with Lambda and API Gateway, you’re well on your way to becoming a proficient serverless developer