Auto Retweet Using AWS
Lambda
Srushith R
@SrushithR
CodeOps Technologies
Krishna Kishore (KK)
How will the application
handle server hardware failure?
How can I control
Serversaccess from my servers?
When should I decideto
scale out my servers?
When should I decideto
scale up myservers?
What size servers are
right for my budget?
How much remaining
capacity do my
servers have?
(AAHHHHHHHHH!!)
How will I keep
my server OS
patched?
How many
servers should I
budget for?
Content source: https://www.slideshare.net/AmazonWebServices/aws-reinvent-2016-getting-started-with-
serverless-architectures-cmp211
2
• VMs
• Machine as the unit of scale
• Abstracts the hardware
• Containers
• Application as the unit of scale
• Abstracts the OS
• Serverless
• Functions as the unit of scale
• Abstracts the language runtime
AWS Lambda
Amazon ECS
Amazon EC2
What is Serverless
computing?
Content source: https://www.slideshare.net/AmazonWebServices/aws-reinvent-2016-
getting-started-with-serverless-architectures-cmp211
3
AWS Lambda 4
Topics covered: AWS
Lambda
 Overview
 Key features
 Working with Lambda
 Developing, deploying and testing on Lambda
 Demo
 Logging
 Benefits
 Use cases
5
Lambda: Overview
 AWS Lambda is a serverless compute service that
runs your code in response to events and
automatically manages the underlying compute
resources for you
 It executes your code only when needed and scales
automatically, from a few requests per day to
thousands per second
6
Content source: https://aws.amazon.com/documentation/
Lambda: Overview
 You pay only for the compute time you consume -
there is no charge when your code is not running
 With AWS Lambda, you can run code for virtually any
type of application or backend service - all with zero
administration
7
Content source: https://aws.amazon.com/documentation/
Lambda: Key features 8
Extend Other
AWS Services
with Custom
Logic
Build Custom
Back-end
Services
Completely
Automated
Administration
Built-in Fault
Tolerance
Automatic
Scaling
Run Code at
Edge Locations
Integrated
Security Model
Classical Architecture 9
Content source: https://blog.couchbase.com/serverless-faas-aws-lambda-java/
Serverless Architecture
Content source: https://blog.couchbase.com/serverless-faas-aws-lambda-java/
10
Lambda integrated with
other services
11
Content source: https://www.slideshare.net/AmazonWebServices/aws-lambda-and-the-serverless-cloud
Triggers for Lambda 12
Content source: https://www.slideshare.net/AmazonWebServices/aws-lambda-and-the-serverless-cloud
Working with Lambda 13
Content source: https://aws.amazon.com/lambda/
Bring your own code
• Node.js, Java, Python
• Bring your own libraries
(even native ones)
Simple resource model
• Select power rating from
128 MB to 1.5 GB
• CPU and network
allocated proportionately
• Reports actual usage
Flexible authorization
• Securely grant access to
resources, including
VPCs
• Fine-grained control
over who can call your
functions
Flexible use
• Call or send events
• Integrated with other
AWS services
• Build whole serverless
ecosystems
Using Lambda 14
Lambda execution
environment
15
Linux kernel version –
4.4.51-
40.60.amzn1.x86_64
Public Amazon Linux AMI
version (AMI name: amzn-
ami-hvm-
2016.03.3.x86_64-gp2)
Image source: https://en.wikipedia.org/wiki/Linux_kernel
Lambda runtime
Lambda supports the following runtime versions:
16
Node.js
• v4.3.2
• v6.10.2
Java
• Java 8
Python
• Python
3.6
• Python
2.7
.NET Core
• .NET
Core
1.0.1 (C#)
Note: If you are using any native binaries in your code, make sure they are compiled in this
environment. Also note that only 64-bit binaries are supported on AWS Lambda
Three ways to create a
Lambda function
17
Content source: https://aws.amazon.com/documentation/
Lambda
AWS Lambda
console (web
User
Interface)
AWS SDK
(Software
Development
Kit)
AWS CLI
(Command
Line Interface)
Deploying you code 18
There are three ways to deploy your code :
Logging in AWS Lambda
19
Your Lambda function can contain logging statements. AWS Lambda
writes these logs to CloudWatch. Logging can be done in two ways:
Content source: https://aws.amazon.com/documentation/
Logging
Print statements
Language specific
logging libraries
Amazon CloudWatch logs
 You can use Amazon CloudWatch Logs to monitor,
store, and access your log files from AWS
Lambda. You can then retrieve the associated log
data from CloudWatch Logs
 Archive Log Data—You can use CloudWatch Logs to
store your log data in highly durable storage
20
Content source: https://aws.amazon.com/documentation/
Accessing CloudWatch logs
for lambda
21
Content source: https://www.slideshare.net/AmazonWebServices/aws-lambda-and-the-serverless-cloud
Lambda limits 22
Content source: http://docs.aws.amazon.com/lambda/latest/dg/limits.html
1
No Servers to
Manage
2
Continuous
Scaling
3
Subsecond
Metering
Benefits 23
Lambda use cases 24
Content source: https://www.slideshare.net/AmazonWebServices/aws-lambda-and-the-serverless-cloud
25
DEMO!
Python code sample 26
The code is available in the github repo:
https://github.com/CodeOpsTechnologies/Real_time_retweets
Prerequisites for the zip file:
1. Python-twitter module:
The module must be installed into a separate folder. Use
the modified pip command:
pip install --target <target folder path> python-twitter
Note: The python code uses boto3 module, but installing it is not
required as it is available as a default module in AWS Lambda
Steps to deploy code to
Lambda
1. Create a zip file with the python code and dependency
modules (python-twitter).
2. Create a Lambda function
 Upload the zip file
 Define environment variables and encrypt them
3. Create an event to schedule the lambda every one hour
27
Create Lambda 28
Encrypt Environment Variables 29
Enable encryption to
encrypt the environment
variables
choose an
encryption key
Lambda function handler 30
handler is of the format:
<python code file
name>.lambda_handler
IAM role with logging
permissions
Input to the Lambda 31
twitter user name you
want to retweet from
(without ‘@’)
Schedule an event 32
choose ‘rules’ under
‘events’ in CouldWatch
select ‘schedule’
33
schedule a fixed rate or as
a cron expression
Schedule an event
Select target Lambda 34
select the target
Lambda function
Pass the input event 35
select ‘constant’ and add
the input event in JSON
format
Create! 36
Schedule rule 37
Output 38
Serverless Summit
Join us at India’s first
conference on serverless
technologies!
39
Scheduled Retweets Using AWS Lambda

Scheduled Retweets Using AWS Lambda

  • 1.
    Auto Retweet UsingAWS Lambda Srushith R @SrushithR CodeOps Technologies Krishna Kishore (KK)
  • 2.
    How will theapplication handle server hardware failure? How can I control Serversaccess from my servers? When should I decideto scale out my servers? When should I decideto scale up myservers? What size servers are right for my budget? How much remaining capacity do my servers have? (AAHHHHHHHHH!!) How will I keep my server OS patched? How many servers should I budget for? Content source: https://www.slideshare.net/AmazonWebServices/aws-reinvent-2016-getting-started-with- serverless-architectures-cmp211 2
  • 3.
    • VMs • Machineas the unit of scale • Abstracts the hardware • Containers • Application as the unit of scale • Abstracts the OS • Serverless • Functions as the unit of scale • Abstracts the language runtime AWS Lambda Amazon ECS Amazon EC2 What is Serverless computing? Content source: https://www.slideshare.net/AmazonWebServices/aws-reinvent-2016- getting-started-with-serverless-architectures-cmp211 3
  • 4.
  • 5.
    Topics covered: AWS Lambda Overview  Key features  Working with Lambda  Developing, deploying and testing on Lambda  Demo  Logging  Benefits  Use cases 5
  • 6.
    Lambda: Overview  AWSLambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you  It executes your code only when needed and scales automatically, from a few requests per day to thousands per second 6 Content source: https://aws.amazon.com/documentation/
  • 7.
    Lambda: Overview  Youpay only for the compute time you consume - there is no charge when your code is not running  With AWS Lambda, you can run code for virtually any type of application or backend service - all with zero administration 7 Content source: https://aws.amazon.com/documentation/
  • 8.
    Lambda: Key features8 Extend Other AWS Services with Custom Logic Build Custom Back-end Services Completely Automated Administration Built-in Fault Tolerance Automatic Scaling Run Code at Edge Locations Integrated Security Model
  • 9.
    Classical Architecture 9 Contentsource: https://blog.couchbase.com/serverless-faas-aws-lambda-java/
  • 10.
    Serverless Architecture Content source:https://blog.couchbase.com/serverless-faas-aws-lambda-java/ 10
  • 11.
    Lambda integrated with otherservices 11 Content source: https://www.slideshare.net/AmazonWebServices/aws-lambda-and-the-serverless-cloud
  • 12.
    Triggers for Lambda12 Content source: https://www.slideshare.net/AmazonWebServices/aws-lambda-and-the-serverless-cloud
  • 13.
    Working with Lambda13 Content source: https://aws.amazon.com/lambda/
  • 14.
    Bring your owncode • Node.js, Java, Python • Bring your own libraries (even native ones) Simple resource model • Select power rating from 128 MB to 1.5 GB • CPU and network allocated proportionately • Reports actual usage Flexible authorization • Securely grant access to resources, including VPCs • Fine-grained control over who can call your functions Flexible use • Call or send events • Integrated with other AWS services • Build whole serverless ecosystems Using Lambda 14
  • 15.
    Lambda execution environment 15 Linux kernelversion – 4.4.51- 40.60.amzn1.x86_64 Public Amazon Linux AMI version (AMI name: amzn- ami-hvm- 2016.03.3.x86_64-gp2) Image source: https://en.wikipedia.org/wiki/Linux_kernel
  • 16.
    Lambda runtime Lambda supportsthe following runtime versions: 16 Node.js • v4.3.2 • v6.10.2 Java • Java 8 Python • Python 3.6 • Python 2.7 .NET Core • .NET Core 1.0.1 (C#) Note: If you are using any native binaries in your code, make sure they are compiled in this environment. Also note that only 64-bit binaries are supported on AWS Lambda
  • 17.
    Three ways tocreate a Lambda function 17 Content source: https://aws.amazon.com/documentation/ Lambda AWS Lambda console (web User Interface) AWS SDK (Software Development Kit) AWS CLI (Command Line Interface)
  • 18.
    Deploying you code18 There are three ways to deploy your code :
  • 19.
    Logging in AWSLambda 19 Your Lambda function can contain logging statements. AWS Lambda writes these logs to CloudWatch. Logging can be done in two ways: Content source: https://aws.amazon.com/documentation/ Logging Print statements Language specific logging libraries
  • 20.
    Amazon CloudWatch logs You can use Amazon CloudWatch Logs to monitor, store, and access your log files from AWS Lambda. You can then retrieve the associated log data from CloudWatch Logs  Archive Log Data—You can use CloudWatch Logs to store your log data in highly durable storage 20 Content source: https://aws.amazon.com/documentation/
  • 21.
    Accessing CloudWatch logs forlambda 21 Content source: https://www.slideshare.net/AmazonWebServices/aws-lambda-and-the-serverless-cloud
  • 22.
    Lambda limits 22 Contentsource: http://docs.aws.amazon.com/lambda/latest/dg/limits.html
  • 23.
  • 24.
    Lambda use cases24 Content source: https://www.slideshare.net/AmazonWebServices/aws-lambda-and-the-serverless-cloud
  • 25.
  • 26.
    Python code sample26 The code is available in the github repo: https://github.com/CodeOpsTechnologies/Real_time_retweets Prerequisites for the zip file: 1. Python-twitter module: The module must be installed into a separate folder. Use the modified pip command: pip install --target <target folder path> python-twitter Note: The python code uses boto3 module, but installing it is not required as it is available as a default module in AWS Lambda
  • 27.
    Steps to deploycode to Lambda 1. Create a zip file with the python code and dependency modules (python-twitter). 2. Create a Lambda function  Upload the zip file  Define environment variables and encrypt them 3. Create an event to schedule the lambda every one hour 27
  • 28.
  • 29.
    Encrypt Environment Variables29 Enable encryption to encrypt the environment variables choose an encryption key
  • 30.
    Lambda function handler30 handler is of the format: <python code file name>.lambda_handler IAM role with logging permissions
  • 31.
    Input to theLambda 31 twitter user name you want to retweet from (without ‘@’)
  • 32.
    Schedule an event32 choose ‘rules’ under ‘events’ in CouldWatch select ‘schedule’
  • 33.
    33 schedule a fixedrate or as a cron expression Schedule an event
  • 34.
    Select target Lambda34 select the target Lambda function
  • 35.
    Pass the inputevent 35 select ‘constant’ and add the input event in JSON format
  • 36.
  • 37.
  • 38.
  • 39.
    Serverless Summit Join usat India’s first conference on serverless technologies! 39

Editor's Notes