- Understand how to deploy a backend using AWS Lambda and Spring Boot.
- Learn how to expose Lambda functions using API Gateway.
- Automate AWS Lambda deployments using Terraform.
- (Optional) Split backend functionalities into separate Lambda functions, triggered via HTTP calls.
- Fork this repository.
- Clone your fork to your machine.
Previous Core Activity
-
Create a Lambda Function:
- Open the AWS Management Console and navigate to Lambda.
- Click Create function.
- Choose Author from scratch.
- Name the function (e.g., MyBackendFunction).
- Select the JAVA runtime.
- Click Create function.
-
Add Function Logic:
- Add a simple backend function to respond to API requests:
-
Set Up API Gateway:
- Navigate to API Gateway in AWS Console.
- Click Create API.
- Choose HTTP API and click Build.
- Name your API (e.g., MyBackendAPI).
- Under Integrations, select Lambda and choose your Lambda function.
- Click Next and deploy your API.
-
Test the API:
- Open a browser and visit the URL generated by API Gateway (
https://your-api-id.execute-api.region.amazonaws.com/register
). - Verify that it returns the message:
"Hello, /register"
.
- Open a browser and visit the URL generated by API Gateway (
Rather than re-invent the wheel we're going to make use of some of AWS' own documentation to introduce the Serverless Java Container as a way of using Spring with AWS Lambda and API Gateway to create a serverless API environment.
-
Main Repo for Serverless is here
-
The Quick Start Guide for deploying a Spring 3 App with a Serverless Container is here
-
The Sample Application can be found here
-
Take some time this morning, to rework either the Todo App or a Spring App of your own in order to deploy it via AWS Lambda and the API Gateway as documented in these.
-[] Ensure that Terraform CLI is installed and AWS CLI is configured.
-
Write Terraform Configuration:
- In your project folder, create a file named main.tf with the following content:
provider "aws" { region = "eu-north-1" } resource "aws_iam_role" "lambda_role" { name = "lambda_role" assume_role_policy = jsonencode({ Version = "2012-10-17", Statement = [{ Action = "sts:AssumeRole", Effect = "Allow", Principal = { Service = "lambda.amazonaws.com" } }] }) } resource "aws_lambda_function" "backend" { filename = "backend.zip" function_name = "MyBackendFunction" role = aws_iam_role.lambda_role.arn handler = "Backend::Backend.Function::FunctionHandler" runtime = "java" -- JAVAENVIRONMENT source_code_hash = filebase64sha256("backend.zip") } resource "aws_api_gateway_rest_api" "api" { name = "MyBackendAPI" } resource "aws_api_gateway_resource" "resource" { rest_api_id = aws_api_gateway_rest_api.api.id parent_id = aws_api_gateway_rest_api.api.root_resource_id path_part = "register" } resource "aws_api_gateway_method" "method" { rest_api_id = aws_api_gateway_rest_api.api.id resource_id = aws_api_gateway_resource.resource.id http_method = "GET" authorization = "NONE" } resource "aws_api_gateway_integration" "integration" { rest_api_id = aws_api_gateway_rest_api.api.id resource_id = aws_api_gateway_resource.resource.id http_method = aws_api_gateway_method.method.http_method type = "AWS_PROXY" integration_http_method = "POST" uri = aws_lambda_function.backend.invoke_arn }
-
Package Your Lambda Code:
- Publish your Lambda function using the following commands:
dotnet publish -c Release -o out --JAVA COMMAND TO PUBLISH cd out zip -r backend.zip .
-
Run Terraform:
- Initialize Terraform:
terraform init
- Apply the configuration:
terraform apply
-
Verify Lambda and API Gateway Deployment:
- Check the API Gateway endpoint URL in AWS and ensure it is connected to the Lambda function by visiting
https://your-api-id.execute-api.region.amazonaws.com/register
.
- Check the API Gateway endpoint URL in AWS and ensure it is connected to the Lambda function by visiting
-
Create Additional Lambda Functions:
- Create separate Lambda functions to handle different tasks. For example, create one function for user registration and another for order processing.
-
Update API Gateway:
- In API Gateway, add new routes like
/register
and/order
, linking each route to its respective Lambda function.
- In API Gateway, add new routes like
-
Test the API:
- Verify that different routes trigger different Lambda functions. For instance:
/register
calls the user registration function./order
calls the order processing function.