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

Automate all the things! Postman + Python + Burp macros for the win —  Part 1

Database Hacking Part — 3

The Ultimate Guide to Building Slack Workflows in 2020

Provisioning an Office 365 group with an approval flow and Azure functions-part 1

Managing Shiny authentication with Django

Memento Design Pattern

How to Develop Flutter App for Beginners & Benefits

Learning Python 10 minutes a day #11

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 Lambda Hands-On| Part-3

Installing and Configuring the AWS CLI

AWS IAM: Resource Based Policies

How to Deploy an Application to AWS Fargate