Here is how you can create and deploy your own AWS Lambda Java function in less than 1 minute (with Linux).
First, let’s install the JDK:
sudo apt-get update
sudo apt-get install -y default-jdk
Then let’s create a working directory
mkdir HelloWorldFunction
cd HelloWorldFunction
Let’s create now the source code file for our Lambda (HelloWorldFunction.java):
cat > HelloWorldFunction.java << EOL
public class HelloWorldFunction {
public static String handler() {
return “Hello World!”;
}
}
EOL
Let’s compile the Lambda and then generate a JAR file
javac HelloWorldFunction.java
jar cvf HelloWorldFunction.jar HelloWorldFunction.class
Last but not least, let’s create the function in AWS (you need to create an IAM execution role for the Lambda and specify it in the command)
aws lambda create-function — function-name HelloWorldFunction — runtime java11 — handler HelloWorldFunction::handler — zip-file fileb://HelloWorldFunction.jar — role arn:aws:iam::XXXXXXXXXXXX:role/role-for-java-lambda
That’s it :-)