bene@theodo.co.uk
Ben Ellerby
@EllerbyBen
@EllerbyBen
serverless-transformation
@EllerbyBen
@EllerbyBen
@EllerbyBen
💰 Cost reduction
👷♂️ #NoOps
💻 Developers focus on delivering business value
📈 More scalable
🌳 Greener
@EllerbyBen
Lambda
S3
Dynamo
API Gateway
Compute
Storage
Data
API Proxy
Cognito
Auth
SQS
Queue
Step Functions
Workflows
@EllerbyBen
@EllerbyBen
@EllerbyBen
@EllerbyBen
@EllerbyBen
@EllerbyBen
3.7, 3.6, 2.7
@EllerbyBen
def handler_name(event, context):
...
return some_value
Event: The event body
dict, list, str, int, float, or NoneType
Context: Meta Data
dict
@EllerbyBen
def my_handler(event, context):
message = 'Hello {} {}!'.format(event['first_name'],
event['last_name'])
return {
'message' : message
}
@EllerbyBen
A handler function is a function of your code triggered by AWS when your lambda is invoked
@EllerbyBen
@EllerbyBen
npm install -g serverless
serverless create
--template aws-python3
--path myService
@EllerbyBen
serverless.yml
handler.py
@EllerbyBen
import json
def hello(event, context):
body = {
"message": "Go Serverless v1.0! Your function executed successfully!",
"input": event
}
response = {
"statusCode": 200,
"body": json.dumps(body)
}
return response
@EllerbyBen
service: myService
provider:
name: aws
runtime: python3.7
functions:
hello:
handler: handler.hello
@EllerbyBen
➜ sls invoke local -f hello
{
"statusCode": 200,
"body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \"input\": {}}"
}
@EllerbyBen
➜ sls deploy
➜ sls invoke -f hello
{
"statusCode": 200,
"body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \"input\": {}}"
}
@EllerbyBen
@EllerbyBen
@EllerbyBen
@EllerbyBen
@EllerbyBen
Polls database
Celery beat is a scheduler; It kicks off tasks at regular intervals, that are then executed by available worker nodes in the cluster.
@EllerbyBen
SQL DB
App
App
Rabbit MQ
Celery
Celery
Celery
Celery
Beat
@EllerbyBen
SQL DB
App
App
Rabbit MQ
Celery
Celery
Celery
Celery
Beat
@EllerbyBen
@EllerbyBen
AWS Step Functions lets you coordinate multiple AWS services into serverless workflows so you can build and update apps quickly.
@EllerbyBen
@EllerbyBen
AWSTemplateFormatVersion: '2010-09-09'
Description: An example template for a Step Functions state machine.
Resources:
MyStateMachine:
Type: AWS::StepFunctions::StateMachine
Properties:
StateMachineName: HelloWorld-StateMachine
DefinitionString: |-
{
"StartAt": "HelloWorld",
"States": {
"HelloWorld": {
"Type": "Task",
"Resource": "ARN of Function.....",
"End": true
}
}
}
RoleArn: arn-of-role
Tags:
-
Key: "keyname1"
Value: "value1"
-
Key: "keyname2"
Value: "value2"
@EllerbyBen
@EllerbyBen
stepFunctions:
stateMachines:
MyStateMachine:
name: MyMachine
definition:
Comment: "Does something cool"
States:
HelloWorld:
Type: Task
Resource: "HelloLambdaARN"
End: true
@EllerbyBen
@EllerbyBen
@EllerbyBen
@EllerbyBen
@EllerbyBen
@EllerbyBen
Start
Wait
Push
End
@EllerbyBen
Start
Wait
Push
End
@EllerbyBen
You are charged based on the number of state transitions required to execute your application.
Start
Wait
Push
End
12/10/2019
push_email
send_email
schedule_email
@EllerbyBen
stepFunctions:
stateMachines:
EmailFSM:
name: EmailScheduling
definition:
Comment: "Schedules an email"
StartAt: WaitForDueDate
States:
WaitForDueDate:
Type: Wait
TimestampPath: $.dueDate
Next: PushEmail
PushEmail:
Type: Task
Resource: "arn-of-function"
End: true
FSM
@EllerbyBen
service: myService
provider:
name: aws
runtime: python3.7
functions:
hello:
handler: handler.send_email
events:
- sqs:
arn:
Fn::GetAtt:
- EmailQueue
- Arn
resources:
Resources:
EmailQueue:
Type: "AWS::SQS::Queue"
Properties:
QueueName: "EmailQueue${opt:infraStackName}"
SQS
@EllerbyBen
{
"dueDate": "2019-03-26T00:00:00.000Z",
"from ": "bene@theodo.co.uk",
"to": "pycon@theodo.co.uk",
"subject": "serverless"
}
event
@EllerbyBen
def schedule_email(event, context):
sfn = boto3.client('stepfunctions')
sfn_response = sfn.start_execution(
stateMachineArn=os.environ.get('SFN_ARN'),
name=event.schedule,
input=event['body']
)
response = {
"statusCode": 200,
"body": event['body']
}
return response
schedule_email
@EllerbyBen
def push_email(event, context):
client = boto3.client('sqs')
sqs_event = client.send_message(
QueueUrl=os.environ['SQS_URL'],
MessageBody=event['body']
)
response = {
"statusCode": 200,
"body": "EVENT PUSHED"
}
return response
push_email
@EllerbyBen
def send_email(event, context):
message = Mail(
from_email=event['body']['from'],
to_emails=event['body']['to'],
subject=event['body']['subject'],
html_content='Hello PyCon!'
)
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
response = {
"statusCode": 200,
"body": "Email Sent"
}
return response
send_email
Step functions provide a nice way to create workflows
AWS provides good serverless services for python
The serverless framework with plugins can manage event scheduling
@EllerbyBen
serverless-transformation
@EllerbyBen
npm install -g sls-dev-tools
@EllerbyBen