Skip to content

boolean-uk/java-cloud-aws-day-3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

JAVA Cloud AWS - Day Three

Learning Objectives

  • 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.

Instructions

  1. Fork this repository.
  2. Clone your fork to your machine.
Previous Core Activity

Deploy Backend Using AWS Lambda and API Gateway

Steps

  1. 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.
  2. Add Function Logic:

    • Add a simple backend function to respond to API requests:
  3. 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.
  4. 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".

Core Activity

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.

Use Terraform to Automate Lambda Deployment

Prerequisites:

-[] Ensure that Terraform CLI is installed and AWS CLI is configured.

Steps

  1. 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
      }
  2. 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 .
    
  3. Run Terraform:

    • Initialize Terraform:
    terraform init
    • Apply the configuration:
    terraform apply
  4. 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.

(Optional) Split Functionalities into Multiple Lambda Functions

Steps

  1. 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.
  2. Update API Gateway:

    • In API Gateway, add new routes like /register and /order, linking each route to its respective Lambda function.
  3. 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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published