Notes and examples for the Sydney Java Meetup's Lambda Workshop
- Java SE JDK 8 (https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)
- IntelliJ / Eclipse (https://www.jetbrains.com/idea/download)
- workshop demos done in IntelliJ
- SAM CLI (https://github.com/awslabs/aws-sam-cli/blob/develop/docs/installation.rst)
- requires Python and Docker
- (Optional) An AWS account to deploy your code to the cloud!
- Enthusiasm!
- Initialize SAM CLI Java project
sam init -r java8 -n starry-skies
- Run
cd starry-skies mvn clean test
- Open in IntelliJ for some IDE goodness
idea .
- Remember to fix the module Java versions if IntelliJ didn't pick them up
- Build the package
mvn package
At this point we should have an uber-jar in starry-skies/target/HelloWorld-1.0.jar.
- Run the service:
sam local start-api
- Start a browser: http://127.0.0.1:3000/hello
-
Add a 'remote' debug configuration in IntelliJ / equivalent Eclipse
-
Put a breakpoint in App::handler
-
Run the service:
sam local start-api -d 9292
-
Start a browser:
(the local lambda will pause waiting for you to attach a debugger)
Take a break for questions and getting everyone up to the same place.
Let's upgrade the default SAM CLI implementation with some generics and stronger typing.
Let's make our function do something fun!
Let's deploy our functions to AWS cloud and invoke them from the browser.
Let's add some metrics to our function and show them on a dashboard.
private void writeMetric(String name, double value) {
try {
final AmazonCloudWatch cw =
AmazonCloudWatchClientBuilder.defaultClient();
MetricDatum datum = new MetricDatum()
.withMetricName(name)
.withUnit(StandardUnit.Milliseconds)
.withValue(value);
PutMetricDataRequest request = new PutMetricDataRequest()
.withNamespace("VISUALISING_PERFORMANCE")
.withMetricData(datum);
PutMetricDataResult putMetricDataResult = cw.putMetricData(request);
System.out.println(putMetricDataResult.getSdkResponseMetadata());
} catch (Exception e) {
System.out.println("Failed to write CloudWatch metric." + e.getMessage());
e.printStackTrace();
}
}