AWS Lambda Hands-On| Part-2

  • Cloning from Docker Image.
  • Launching Container from the Docker Image.
  • SAM INIT to setup a sample project for Lambda.
  • SAM BUILD to compile & build the Java based Lambda project.
  • SAM DEPLOY to deploy Lambdas to AWS Stack.
  • Creating our first Lambda function and managing it’s lifecycle.
  • Creating Lambda function to receive simple parameter in request.
  • Anatomy of any SAM Template.yaml file.
  • Sneak peak under the hoods of SAM DEPLOY command.
  • Destroying entire AWS Stack using AWS CLI.
docker cp peaceful_allen:/adityaLearningLambda .
sudo apt-get update && apt-get install maven
sam build
  • Note that, the above endpoint belongs to the API-Gateway and in this case, hitting to the gateway would eventually trigger the Lambda function and the above response is well coming from the Lambda function.
  • This is the IP address where the lambda function is being run dynamically when the lambda environment is created for a few seconds, that IP address of virtual machine or the machine on the cloud.
root@9090f8d966d0:/adityaLearningLambda# cat template.yamlAWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
adityaLearningLambda

Sample SAM Template for adityaLearningLambda

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 20

Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: HelloWorldFunction
Handler: helloworld.App::hello
Runtime: java8
MemorySize: 512
  • We only have a Resource being defined inside the yaml and there is a Handler that we have defined here : ClassName is “App” and MethodName is “hello”.
  • Also observe that, we have allocated 512MB of RAM to this Lambda function.
package helloworld;

public class App {
public String hello() {
return "First Awesome hello from Lambda Functions..";
}
}
sam build
sam deploy
package helloworld;

public class App {
public String hello(String firstParameter) {
return "Executed lambda function along with paramtere in request.." + firstParameter;
}
}
sam build
sam build
sam deploy
sam logs -n HelloWorldFunction --stack-name adityaLambdaFirstStack --tail
java.lang.RuntimeException: An error occurred during JSON parsingCaused by: java.io.UncheckedIOException: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
  • The very first line “AWS template format version” tells which version of cloud formation template we are using.
  • Then this “Transform” element tells that, this template is of type AWS serverless template and it needs to be transformed to cloud formation.
Description: >
adityaLearningLambda

Sample SAM Template for adityaLearningLambda

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: HelloWorldFunction
Handler: helloworld.App::hello
Runtime: java8
MemorySize: 512
  • HelloWorldFunction” is the name of the resource type.
  • AWS::Serverless::Function” is the type of the resource.
  • CodeUri → Here the code can be found. This is the folder, where you have to look inside the build directory for our code.
  • Handler → Here, we define the package name, the class name and then the name of the function
  • Runtime → Here, we can define the Java version that we want to use. If you want to change it to Java Eleven, you simply change this from eight to eleven.
  • MemorySize → Here, we define the memory of 512 MB for our Lambda function.
Globals:
Function:
Timeout: 20
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
aws cloudformation delete-stack --stack-name adityaLambdaFirstStack

--

--

--

Software Engineer for Big Data distributed systems

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Decentra-Lotto & Chainlink VRF

A Web2 Developer Explores Web3: Part 2

Complete Explanation on SQL Joins and Unions With Examples in PostgreSQL

Output from our workshop at Agile Cambridge 2017

New Vaults Up With Meso.Finance!

Importance of effective bug reporting

Why do we have process anyway?

New Features In Cmd Make Access Control on Linux Easier, Faster, and More Compliant

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
aditya goel

aditya goel

Software Engineer for Big Data distributed systems

More from Medium

AWS ETL: Save HTTP POST Payload into S3 Bucket as Parquet files

Installing AWS CLI with Dockers

Use Python logging module in AWS Lambda to send messages to CloudWatch and SNS

How to create a Serverless Cron Job in AWS Batch?