Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 70

Amazon Web Services

AWS Lambda
AWS Lambda is an event-driven, serverless
computing platform provided by Amazon as a
part of Amazon Web Services. It is a computing
service that runs code in response to events and
automatically manages the computing resources
required by that code. It was introduced in
November 2014.
AWS Lambda
• lets you run code without provisioning or
managing servers.

• the lambda code is executed just when it is


needed

• is an event-driven
AWS Toolkit for Eclipse 2.0
• Go to Eclipse EE -> Help - > Eclipse Marketplace
AWS Toolkit for Eclipse 2.0
• Type aws and chose AWS Toolkit for Eclipse 2.0, then
click on Install button
AWS Toolkit for Eclipse 2.0
• Click on Confirm button
AWS Toolkit for Eclipse 2.0
• Click on I accept the terms… and then click on Finish.
• After Eclipse Restart add your logging aws credentials
AWS Lambda Java Project
• Chose New AWS Lambda Java Project…
AWS Lambda Java Project
• Fill the fields like this:
AWS Lambda Java Project
• Fill the fields like this:
AWS Lambda Java Project
• In the pom.xml we can see the dependencies
to amazon web services were added.
AWS Lambda Java Project
• We will create an application that generate a
random number
• Edit LambdaFunctionHandler.java like this:
package com.amazonaws.lambda.demo;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import java.util.Random;

public class LambdaFunctionHandler implements RequestHandler<Object, String>


{

@Override
public String handleRequest(Object input, Context context) {
context.getLogger().log("Input: " + input);

// create instance of Random class


Random rand = new Random();

// Generate random integers in range 0 to 999


int rand_int = rand.nextInt(1000);

return "The random number is " + rand_int;


}

}
AWS Lambda Java Project
• Comment this line of code
package com.amazonaws.lambda.demo;

import java.io.IOException;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import com.amazonaws.services.lambda.runtime.Context;

/**
* A simple test harness for locally invoking your Lambda function handler.
*/
public class LambdaFunctionHandlerTest {

private static Object input;

@BeforeClass
public static void createInput() throws IOException {
// TODO: set up your sample input object here.
input = null;
}

private Context createContext() {


TestContext ctx = new TestContext();

// TODO: customize your context here if needed.


ctx.setFunctionName("Your Function Name");

return ctx;
}

@Test
public void testLambdaFunctionHandler() {
LambdaFunctionHandler handler = new LambdaFunctionHandler();
Context ctx = createContext();

String output = handler.handleRequest(input, ctx);

// TODO: validate output here if needed.


// Assert.assertEquals("Hello from Lambda!", output);
}
}
AWS Lambda Java Project
• Right click on the Project ->Run As->Maven Clean
AWS Lambda Java Project
• Right click on the Project ->Run As->Maven Install
AWS Lambda Java Project
• In case of build failed check if you have Jdk 1.8 at
Java Build Path
Here is the jar installed
AWS Lambda
1. go to https://www.awseducate.com/signin/SiteLogin and
enter your email and password from the amazon’s email.
2. Click on AWS Account and then on AWS Educate Starter
Account
AWS Lambda
3. click on AWS Console

4. chose lambda from Find Services


AWS Lambda
• Click on Create function
AWS Lambda
• Type a function name and chose Java 8 as
runtime
AWS Lambda
• Click on “Create a new role from AWS policy
template”, type a role name, then add as
policy “ Simple microservice permissions”
AWS Lambda
• click on Upload button and select the demo-1.0.0.jar from
yourWorkspacePath\LambdaExampleProject\target\
• select Java 8 as Runtime
• modify the Handler
• click on Save button

com.amazonaws.lambda.demo.LambdaFunctionHandler::handleRequest
AWS Lambda
• Click on “Configure a new test events”
AWS Lambda
• Add a test event, delete de content of the Json
file and click on Save.
AWS Lambda
• Click on Test button and you should see
something like this.
AWS Lambda API Gateway
• Click on Add trigger button.
AWS Lambda API Gateway
• Chose API Gateway and configure like this:
AWS Lambda API Gateway
• You can test the API endpoint now. Click on
the link and refresh the page few times.
AWS API Gateway
There is another way to create an API endpoint.
This methods will be presented in the following
slides.
AWS API Gateway
1. go to https://www.awseducate.com/signin/SiteLogin and
enter your email and password from the amazon’s email.
2. Click on AWS Account and then on AWS Educate Starter
Account
AWS API Gateway
3. click on AWS Console

4. chose API Gateway from Find Services


AWS API Gateway
• Click on Create API
AWS API Gateway
• Select REST API and click on Build button
AWS API Gateway
• Chose REST protocol and add an API name
AWS API Gateway
• Click on Resources
AWS API Gateway
• Click on Actions and chose Create Method
AWS API Gateway
• Chose GET method and click on the check sign
AWS API Gateway
• Chose Lambda Function and chose the lambda
function from the combo box that it is already
created, then click on Save button.
AWS API Gateway
• Click on OK button.
AWS API Gateway
• Click on Deploy API
AWS API Gateway
• Select New Stage, add a stage name and then
click on Deploy.
AWS API Gateway
• Click on the URL

• It should be shown something like this:


Test API Gateway
• Create the following .html file and upload it on
S3, in your bucket.
<html> Add Invoke URL from previous slide
<head>
<script>
async function callAwsLambdaFunction() {
fetch('', {
method:'GET'
})
.then(response => response.json())
.then((response)=> {
console.log(response);
document.getElementById("myDiv").innerHTML = response;
});
}
</script>
<title>Test</title>
</head>
<body>
<p> This is a test </p>
<p> Click below button to call API gateway and display result
below!</p>
<p><div id ="myDiv"></div></p>
<button onclick="callAwsLambdaFunction()"> Click here! </button>
</body>
</html>
Test API Gateway
• When you upload the .html file make sure that
you grant public read access to this object.
Test API Gateway
• After you upload the .html file in S3 and click
on it you will see this.
CORS
• Click on Actions, then click on Enable CORS
CORS
• Check GET method and then click on Enable
CORS and replace existing CORS headers
CORS
• Click on Yes, replace existing values
CORS
• Now OPTIONS method should be created.
AWS S3 Static website hosting
• Go to your bucket from S3, then click on
Properties and then click on Static website
hosting
AWS S3 Static website hosting
• Add the name of your .html file and the click
on Save.
Test API Gateway
• Go to your bucket from S3, then click on
your .html file, then click on Object URL
Test API Gateway
• After you press on Click here button you
should see something like this:
Task
• Modify the .html file in order to have 2 text
field when you can introduce your first name
and last name, then change the lambda
function in order to be able to display the first
name and last name after you press on Click
here button.
Lambda - S3 trigger
• A lambda function can be triggered when you
upload a file in S3. You can create the lambda
function in the same project.
Lambda - S3 trigger
• Right click on the project, then click on
New ->Other-> AWS Lambda Function and click
on Next button
Lambda - S3 trigger
• Add a name for the java class and click on
Finish button
Lambda - S3 trigger
• Delete the generated test code and then click
on Maven Install as it was done for the first
lambda function.
Lambda - S3 trigger
• Go to Lambda and click on Create function.
Lambda - S3 trigger
• Add a name and select Java 8
Lambda - S3 trigger
• Upload the generated jar and add the handler
path

com.amazonaws.lambda.demo. S3LambdaHandler::handleRequest
Lambda - S3 trigger
• Click on Add trigger
Lambda - S3 trigger
• Chose S3
Lambda - S3 trigger
• Select your bucket and click on Add button
Lambda - S3 trigger
• Select Permission and click on Edit
Lambda - S3 trigger
• Click on Create a new role and add S3 policy
Lambda - S3 trigger
• Go to your s3 bucket and enable logging.
Lambda - S3 trigger
• After you upload a file into S3 go to aws
console and then go to cloud watch
Lambda - S3 trigger
• Click on Logs, then click on Let’s get started
Lambda - S3 trigger
• After you click on your lambda function you
should see something like this:

You might also like