Deep Dive on Serverless Application
Development
Danilo Poccia, Technical Evangelist
@danilop
© 2015, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Agenda
What is a Serverless Application?
What do we need to think about when building one?
• Bundling and Deploying
• Continuous Integration & Continuous Delivery
• Versioning, Stages, Variables
• Metrics, Monitoring, Logs, and Profiling
What are
Serverless Applications?
Serverless means…
No servers to provision Scales with usage
or manage
Never pay for idle Availability and fault
tolerance built in
Serverless application
EVENT SOURCE FUNCTION SERVICES (ANYTHING)
Changes in
data state Node.js
Python
Requests to Java
endpoints C#
Changes in
resource state
Example event sources that trigger AWS Lambda
DATA STORES ENDPOINTS
Amazon S3 Amazon Amazon Amazon Amazon AWS IoT AWS Step Amazon
DynamoDB Kinesis Cognito API Gateway Functions Alexa
CONFIGURATION REPOSITORIES EVENT/MESSAGE SERVICES
AWS AWS CloudTrail AWS Amazon Amazon Amazon SNS Cron events
CloudFormation CodeCommit CloudWatch SES
… and a few more with more on the way!
API Gateway
AWS
AWS Lambda
functions
API Gateway
Cache
Mobile Apps
Endpoints on
Amazon EC2
Internet Amazon
Websites
CloudFront
All publicly
accessible
Services endpoints
Amazon Any other
CloudWatch AWS service
Monitoring
Common use cases
</>
Web Backends Data Chatbots Amazon IT
Applications Processing Alexa Automation
• Static • Apps & • Real time • Powering • Powering • Policy engines
websites services chatbot logic voice-enabled
• MapReduce apps • Extending
• Complex web • Mobile AWS services
apps • Batch • Alexa Skills
• IoT Kit • Infrastructure
• Packages for management
Flask and
Express
Bundling and Deploying
Serverless Applications
Building a deployment package
Node.js & Python Java C# (.NET Core)
• .zip file consisting of • Either .zip file with all • Either .zip file with all
your code and any code/dependencies, code/dependencies,
dependencies or standalone .jar or a standalone .dll
• Use npm/pip to • Use Maven / Eclipse • Use NuGet /
install libraries IDE plugins VisualStudio plugins
• All dependencies • Compiled class & • All assemblies (.dll)
must be at root level resource files at root at root level
level, required jars in
/lib directory
AWS CloudFormation
Create templates of your infrastructure
CloudFormation provisions AWS resources
based on dependency needs
Version control/replicate/update templates like
code
Integrates with development, CI/CD,
management tools
JSON and YAML supported
CloudFormation template
AWSTemplateFormatVersion: '2010-09-09' - arn:aws:iam::aws:policy/AmazonDynamoDBReadOnlyAccess
Resources: - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
GetHtmlFunctionGetHtmlPermissionProd: AssumeRolePolicyDocument:
Type: AWS::Lambda::Permission Version: '2012-10-17'
Properties: Statement:
Action: lambda:invokeFunction - Action:
Principal: apigateway.amazonaws.com - sts:AssumeRole
FunctionName: Effect: Allow
Ref: GetHtmlFunction Principal:
SourceArn: Service:
Fn::Sub: arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ServerlessRestApi}/Prod/ANY/* - lambda.amazonaws.com
ServerlessRestApiProdStage: ServerlessRestApiDeployment:
Type: AWS::ApiGateway::Stage Type: AWS::ApiGateway::Deployment
Properties: Properties:
DeploymentId: RestApiId:
Ref: ServerlessRestApiDeployment Ref: ServerlessRestApi
RestApiId: Description: 'RestApi deployment id: 127e3fb91142ab1ddc5f5446adb094442581a90d'
Ref: ServerlessRestApi StageName: Stage
StageName: Prod GetHtmlFunctionGetHtmlPermissionTest:
ListTable: Type: AWS::Lambda::Permission
Type: AWS::DynamoDB::Table Properties:
Properties: Action: lambda:invokeFunction
ProvisionedThroughput: Principal: apigateway.amazonaws.com
WriteCapacityUnits: 5 FunctionName:
ReadCapacityUnits: 5 Ref: GetHtmlFunction
AttributeDefinitions: SourceArn:
- AttributeName: id Fn::Sub: arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ServerlessRestApi}/*/ANY/*
AttributeType: S ServerlessRestApi:
KeySchema: Type: AWS::ApiGateway::RestApi
- KeyType: HASH Properties:
AttributeName: id Body:
GetHtmlFunction: info:
Type: AWS::Lambda::Function version: '1.0'
Properties: title:
Handler: index.gethtml Ref: AWS::StackName
Code: paths:
S3Bucket: flourish-demo-bucket "/{proxy+}":
S3Key: todo_list.zip x-amazon-apigateway-any-method:
Role: x-amazon-apigateway-integration:
Fn::GetAtt: httpMethod: ANY
- GetHtmlFunctionRole type: aws_proxy
- Arn uri:
Runtime: nodejs4.3 Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-
GetHtmlFunctionRole: 31/functions/${GetHtmlFunction.Arn}/invocations
Type: AWS::IAM::Role responses: {}
Properties: swagger: '2.0'
ManagedPolicyArns:
AWS Serverless Application Model (SAM)
CloudFormation extension optimized for
serverless
New serverless resource types:
functions, APIs, and tables
Supports anything CloudFormation supports
Open specification (Apache 2.0)
CloudFormation template
AWSTemplateFormatVersion: '2010-09-09' - arn:aws:iam::aws:policy/AmazonDynamoDBReadOnlyAccess
Resources: - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
GetHtmlFunctionGetHtmlPermissionProd: AssumeRolePolicyDocument:
Type: AWS::Lambda::Permission Version: '2012-10-17'
Properties: Statement:
Action: lambda:invokeFunction - Action:
Principal: apigateway.amazonaws.com - sts:AssumeRole
FunctionName: Effect: Allow
Ref: GetHtmlFunction Principal:
SourceArn: Service:
Fn::Sub: arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ServerlessRestApi}/Prod/ANY/* - lambda.amazonaws.com
ServerlessRestApiProdStage: ServerlessRestApiDeployment:
Type: AWS::ApiGateway::Stage Type: AWS::ApiGateway::Deployment
Properties: Properties:
DeploymentId: RestApiId:
Ref: ServerlessRestApiDeployment Ref: ServerlessRestApi
RestApiId: Description: 'RestApi deployment id: 127e3fb91142ab1ddc5f5446adb094442581a90d'
Ref: ServerlessRestApi StageName: Stage
StageName: Prod GetHtmlFunctionGetHtmlPermissionTest:
ListTable: Type: AWS::Lambda::Permission
Type: AWS::DynamoDB::Table Properties:
Properties: Action: lambda:invokeFunction
ProvisionedThroughput: Principal: apigateway.amazonaws.com
WriteCapacityUnits: 5 FunctionName:
ReadCapacityUnits: 5 Ref: GetHtmlFunction
AttributeDefinitions: SourceArn:
- AttributeName: id Fn::Sub: arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ServerlessRestApi}/*/ANY/*
AttributeType: S ServerlessRestApi:
KeySchema: Type: AWS::ApiGateway::RestApi
- KeyType: HASH Properties:
AttributeName: id Body:
GetHtmlFunction: info:
Type: AWS::Lambda::Function version: '1.0'
Properties: title:
Handler: index.gethtml Ref: AWS::StackName
Code: paths:
S3Bucket: flourish-demo-bucket "/{proxy+}":
S3Key: todo_list.zip x-amazon-apigateway-any-method:
Role: x-amazon-apigateway-integration:
Fn::GetAtt: httpMethod: ANY
- GetHtmlFunctionRole type: aws_proxy
- Arn uri:
Runtime: nodejs4.3 Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-
GetHtmlFunctionRole: 31/functions/${GetHtmlFunction.Arn}/invocations
Type: AWS::IAM::Role responses: {}
Properties: swagger: '2.0'
ManagedPolicyArns:
SAM template
AWSTemplateFormatVersion: '2010-09-09’
Transform: AWS::Serverless-2016-10-31
Resources:
GetHtmlFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://flourish-demo-bucket/todo_list.zip
Handler: index.gethtml
Runtime: nodejs4.3
Policies: AmazonDynamoDBReadOnlyAccess
Events:
GetHtml:
Type: Api
Properties:
Path: /{proxy+}
Method: ANY
ListTable:
Type: AWS::Serverless::SimpleTable
AWS commands – Package & Deploy
Package
•Creates a deployment package (.zip file)
•Uploads deployment package to an Amazon S3 bucket
•Adds a CodeUri property with S3 URI
Deploy
•Calls CloudFormation ‘CreateChangeSet’ API
•Calls CloudFormation ‘ExecuteChangeSet’ API
Versioning, Stages, Variables
Function versioning and aliases
• Versions = immutable copies of
code + configuration
• Aliases = mutable pointers to
versions
Lambda Function Lambda Function
Version $LATEST Version 123
• Development against $LATEST
version
• Each version/alias gets its own
ARN
• Enables rollbacks, staged Lambda Function Lambda Function Lambda Function
promotions, “locked” behavior for DEV Alias BETA Alias PROD Alias
client
API Gateway Stages
Stages are named links to a deployed
version of your API
Recommended for managing API lifecycle
• dev/test/prod
• alpha/beta/gamma
Support for parameterized values via
stage variables
Lambda Environment Variables
Key-value pairs that you can dynamically pass to your
function
Available via standard environment variable APIs such as
process.env for Node.js or os.environ for Python
Can optionally be encrypted via KMS
• Allows you to specify in IAM what roles have access to the keys
to decrypt the information
Useful for creating environments per stage (i.e. dev,
testing, production)
API Gateway Stage Variables
• Stage variables act like environment variables
• Use stage variables to store configuration values
• Stage variables are available in the $context object
• Values are accessible from most fields in API Gateway
• Lambda function ARN
• HTTP endpoint
• Custom authorizer function name
• Parameter mappings
Stage variables and Lambda alias for stages
Using Stage Variables in API Gateway together with Lambda function Aliases
helps you manage a single API configuration and Lambda function for multiple
stages
myLambdaFunction My First API
1 Stage variable = lambdaAlias
2
3 = prod Prod
4 lambdaAlias = prod
5 Beta
lambdaAlias = beta
6 = beta
Dev
7 lambdaAlias = dev
8 = dev
Manage Multiple Versions and Stages of your APIs
Works like a source repository – clone your API to create a new version:
API 1
(v1) Stage (dev)
Stage (prod)
API 2
(v2)
Stage (dev)
Continuous Integration &
Continuous Delivery for
Serverless Applications
AWS CodeBuild
Fully managed build service that compiles source code,
runs tests, and produces software packages
Scales continuously and processes multiple builds
concurrently
You can provide custom build environments suited to
your needs via Docker images
Only pay by the minute for the compute resources you
use
Launched with CodePipeline and Jenkins integration
buildspec.yml Example
version: 0.1
environment_variables:
plaintext:
"INPUT_FILE": "saml.yaml”
"S3_BUCKET": "”
phases:
install:
commands:
- npm install
pre_build:
commands:
- eslint *.js
build:
commands:
- npm test
post_build:
commands:
- aws cloudformation package --template $INPUT_FILE --s3-
bucket $S3_BUCKET --output-template post-saml.yaml
artifacts:
type: zip
files:
- post-saml.yaml
- beta.json
buildspec.yml Example
version: 0.1
environment_variables: • Variables to be used by phases of
plaintext:
"INPUT_FILE": "saml.yaml” build
"S3_BUCKET": "”
phases:
• Examples for what you can do in
install: the phases of a build:
commands: • You can install packages or run
- npm install
pre_build:
commands to prepare your
commands: environment in ”install”.
- eslint *.js • Run syntax checking,
build: commands in “pre_build”.
commands:
- npm test • Execute your build
post_build: tool/command in “build”
commands: • Test your app further or ship a
- aws cloudformation package --template $INPUT_FILE --s3- container image to a repository
bucket $S3_BUCKET --output-template post-saml.yaml
artifacts: in post_build
type: zip
files: • Create and store an artifact in S3
- post-saml.yaml
- beta.json
Where to Focus Your Tests:
UI 10%
Service 20%
Unit 70%
What service and release step corresponds with which tests?
UI
Test
Service
Third Party
Tooling
Build
Unit AWS CodeBuild
AWS CodePipeline
Continuous delivery service for fast and
reliable application updates
Model and visualize your software release
process
Builds, tests, and deploys your code every time
there is a code change
Integrates with third-party tools and AWS
AWS CodePipeline
MyApplication
Source
Source
GitHub
Build
CodeBuild StagePipeline
AWS CodeBuild Action
Transition
Deploy
JavaApp
Elastic Beanstalk
AWS CodePipeline
MyApplication
Source
Source
GitHub
Build
CodeBuild NotifyDevelopers
AWS CodeBuild Lambda
Parallel actions
Deploy
JavaApp
Elastic Beanstalk
AWS CodePipeline
MyApplication
Source
Source
GitHub
Build
CodeBuild NotifyDevelopers
AWS CodeBuild Lambda
Sequential actions
TestAPI
Runscope
Deploy
JavaApp
Elastic Beanstalk
AWS CodePipeline
MyApplication
Build
CodeBuild
AWS CodeBuild
Staging-Deploy
JavaApp
Elastic Beanstalk
QATeamReview
Manual Approval
Manual Approvals
Review
Prod-Deploy
JavaApp
Elastic Beanstalk
Deploy via CodePipeline
Pipeline flow:
1. Commit your code to a source code repository
2. Package in CodeBuild
3. Use CloudFormation actions in CodePipeline to
create or update stacks via SAM templates
Optional: Make use of ChangeSets
4. Make use of specific stage/environment
parameter files to pass in Lambda variables
5. Test our application between stages/environments
Optional: Make use of Manual Approvals
AWS CodeStar
New!
Metrics, Monitoring,
Logs, and Profiling
Serverless Applications
Amazon CloudWatch
• Gain system-wide visibility into resource utilization,
application performance, and operational health
• Collect and track metrics with CloudWatch Metrics
• Collect and monitor log files with CloudWatch Logs
• Set alarms and send messages to SNS
• Automatically react changes via CloudWatch Events
CloudWatch Metrics
Lambda API Gateway
• Default (free) metrics: • Default (free) metrics at Stage
• Invocations level:
• Count
• Duration
• 4XXError
• Throttles • 5XXError
• Errors • Latency
• Iterator Age • IntegrationLatency
• CacheHitcount
• CacheMissCount
• Create custom metrics from inside
your application using “put-metric” • Detailed metrics
API call. • Same set of metrics at method
level
• Can be enabled globally or only for
specific methods
CloudWatch Logs
Lambda Logging
• Logging directly from your code
• Basic request information included
API Gateway Logging
• 2 Levels of logging, ERROR and INFO
• Optionally log method request/body content
• Set globally in stage, or override per method
Log Pivots
• Build metrics based on log filters
• Jump to logs that generated metrics
Custom
CloudWatch
Dashboards
AWS X-Ray
• Identify performance bottlenecks and errors
• Pinpoint issues to specific service(s) in your
application
• Identify impact of issues on users of the
application
• Visualize the service call graph of your
application
Service map
Trace view
Putting it all together!
• Bundling and Deploying
• Continuous Integration & Continuous Delivery
• Versioning, Stages, Variables
• Metrics, Monitoring, Logs, and Profiling
Building on Lambda Functions
Padraig O’Brien - Luciano Mammino
{
“name”: “Padraig”,
“job”: “engineer”,
“twitter”: “@Podgeypoos79”,
“extra”: [
“NodeSchool organiser”,
“LeanCoffee organiser”,
“Tons of secret projects!”
]
}
{
“name”: “Luciano”,
“job”: “engineer”,
“twitter”: “@loige”,
“Website”: “loige.co”
“side-projects”: [
“Node.js Design Patterns”,
“Fullstack Bulletin”
]
}
• UK energy supplier
• ESB funded startup (25 people)
• Targets energy intensive customers
• Trading platform / billing / forecasting
Technology adoption by industry
Source: BCG, Boston Consulting Group, 2016
Common startup goals
• Quick and agile
• High quality software
Our challenge
• Grow customer base
• Get to market fast
• Bringing digital disruption
in the energy industry
The Standing data service
● Download, process & store
industry data
● Integration layer
(REST Api) Get more
details!
First Design OVER-SIMPLIFIED
Recurring task
• Software provisioning
• Security patches
• Be on call for down
times
AZ a AZ b AZ c
Drawbacks
• Many moving parts
• Steep learning curve
• Significantly long time to market
Second (and current) design
Dev. Experience
• Write business logic
• Define triggers
(API Gateway or Schedule)
• Deploy as Lambda
Some lessons learned
• Infrastructure as code is hard
• Lambda Function auto-scaling FTW!
• Beware of soft limits
Some lessons learned
• Local development was challenging
• Orchestration of lambda functions
• Managed service, less hassle
Some lessons learned
• Got to focus on delivering.
• Cost of $8 a month.
• Best practices for larger projects are hard to find.
What’s up next (AWS Step Functions)
What’s up next
• AWS X-Ray
Improve debugging experience
• AWS Glue (ETL)
Simplify data import and synchronization
• Amazon AI services & platforms
Predicting electricity costs and consumption
Thank you
Talk to us at our Partner’s stand S28
www.planet9energy.com
Next steps
• See https://aws.amazon.com/serverless for reference
architectures, samples, and links to more content!
• Explore the AWS SAM specification on GitHub
• Visit the Lambda console, download a blueprint, and get
started building your own Serverless Applications
• Send us your questions, comments, and feedback on the
AWS Lambda Forums.
Conclusion
Lambda is a fundamental
component of modern
application architectures
It has a place in everything
from data processing to
simple web apps
Thank you!
@danilop