Slides for Jan 29, 2020 Chicago Rust Meetup talk where I discuss using Rust based Lambdas in AWS. I don't believe the talk is being recorded so might not make 100% sense, but left links on most pages for more info. Hope this is useful to folks.
5. Less Stuff I Gotta Do...Less Stuff I Gotta Do...
https://blogs.oracle.com/developers/functionsasaservice:evolution,usecases,andgettingstarted2 . 2
10. A λ is a "handler" function YOU provideA λ is a "handler" function YOU provide
that runs in an AWS container "somewhere"that runs in an AWS container "somewhere"
so you focus on your code not plumbingso you focus on your code not plumbing
3 . 3
18. Cheap!Cheap!
In Theory...
Then $0.0000166667 GB/Second after that...
First million invocations free...
Then $0.20/million after that...
400,000 GB-Seconds free...
Plus
Plus
Other AWS Costs (Databases, Data
Transfer...)
3 . 7
19. Mo RAM Mo $Mo RAM Mo $
https://aws.amazon.com/lambda/pricing/3 . 8
20. Pricing is "complicated"...Pricing is "complicated"...
https://medium.com/@zackbloom/serverlesspricingandcostsawslambdaandlambdaedge169bfb58db75
Just Lambda Lambda + API GW
263 Billiable Line Items Per Region Just for Lambda before you add the "other stuff"
3 . 9
25. Cold StartCold Start
REPORT RequestId: 6f127cc4-c2d7-4422-9490-774092cf5042 Duration:
1.36 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory
Used: 35 MB Init Duration: 28.56 ms
1
REPORT RequestId: 6ad595b5-d679-42e2-b790-ab48811cf9cb Duration:
0.87 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory
Used: 35 MB
1
First invocation add Startup Time
Additional runs don't incur overhead
3 . 14
26. Always logs to CloudwatchAlways logs to Cloudwatch
Each instance gets its own Log
Stream in Cloudwatch Logs
https://aws.amazon.com/cloudwatch/pricing/
Don't be noisy,
CWL are $$$$
3 . 15
36. Digging Deeper - SerDe!Digging Deeper - SerDe!
pub type Handler<E, O> = fn(E, Context) -> Result<O, HandlerError>1
You implement:
E JSON that you want to provide to your Lambda function as
input.
O It is the JSON representation of the object returned by the
Lambda function. This is present only if the invocation type is
RequestResponse.
5 . 2
46. On a Mac crossOn a Mac cross
compile with Dockercompile with Docker
$ docker pull clux/muslrust
$ docker run -v $PWD:/volume --rm -t clux/muslrust cargo build --release
$ zip -j hello-world.zip ./target/x86_64-unknown-linux-musl/release/bootstrap
1
2
3
4
Can be kinda slow...
7 . 1
47. On a Mac crossOn a Mac cross
compile directlycompile directly
$ rustup target add x86_64-unknown-linux-musl
$ brew install filosottile/musl-cross/musl-cross
$ ln -s /usr/local/bin/x86_64-linux-musl-gcc /usr/local/bin/musl-gcc
$ cat .cargo/config
[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"
$ cargo build --target=x86_64-unknown-linux-musl --release
$ file target/x86_64-unknown-linux-musl/release/bootstrap
.../bootstrap: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically l
$ zip -j hello-world.zip ./target/x86_64-unknown-linux-musl/release/bootstrap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
^-- Correct!
<-- Takes 30 min - get coffee!
https://aws.amazon.com/blogs/opensource/rustruntimeforawslambda/7 . 2
52. Must Grant PermissionsMust Grant Permissions
for Services to invokefor Services to invoke
$ aws lambda add-permission
--function-name hello-world
--action lambda:InvokeFunction
--statement-id sqs
--principal sqs.amazonaws.com
$ aws lambda get-policy --function-name hello-world | jq -r '.Policy' | prettier --stdin --parser json
{
"Version": "2012-10-17",
"Id": "default",
"Statement": [
{
"Sid": "sqs",
"Effect": "Allow",
"Principal": { "Service": "sqs.amazonaws.com" },
"Action": "lambda:InvokeFunction",
"Resource": "arn:aws:lambda:us-east-2:123456789012:function:hello-world"
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Default rules are nobody has permissions
to do anything - even AWS Services 7 . 7
53. Or deploy with fav toolsOr deploy with fav tools
resource "aws_lambda_permission" "sqs-invokes-hello-world" {
function_name = aws_lambda_function.hello-world.function_name
action = "lambda:InvokeFunction"
statement_id = "sqs"
principal = "sqs.amazonaws.com"
}
1
2
3
4
5
6
7 . 8
54. Serverless Application ModelServerless Application Model
https://aws.amazon.com/serverless/sam/
Command Line Tools
Processes YAML-ish files into CloudFormation Templates
Builds, packages, uploads to s3 for deployment, deploys via
CloudFormation
AWS Only
Local Execution Environment via Docker
brew tap aws/tap
brew install aws-sam-cli
1
2
8 . 1
59. SAM Local TestingSAM Local Testing
https://aws.amazon.com/serverless/sam/
Uses Docker lambci/lambda:provided container as
runtime environment
8 . 6
60. Using Docker DirectlyUsing Docker Directly
https://hub.docker.com/r/lambci/lambda/
$ docker run --rm
-v "$PWD/target/x86_64-unknown-linux-musl/release/":/var/task:ro,delegated
lambci/lambda:provided
handler
'{"firstName": "Steve"}'
START RequestId: a7ac181c-ded5-1b69-5f5e-a7f1f3d30c64 Version: $LATEST
2020-01-27 17:29:55,923 INFO [lambda_runtime::runtime] Received new event with AWS req
2020-01-27 17:29:55,924 INFO [lambda_runtime::runtime] Response for a7ac181c-ded5-1b6
END RequestId: a7ac181c-ded5-1b69-5f5e-a7f1f3d30c64
REPORT RequestId: a7ac181c-ded5-1b69-5f5e-a7f1f3d30c64 Init Duration: 59.08 ms Duratio
{"message":"Hello, Steve!"}
1
2
3
4
5
6
7
8
9
10
11
12
Can still use Docker environments to test if using
other means of deployment (i.e. terraform, etc)
8 . 7
61. LocalStackLocalStack
https://localstack.cloud/
Mock AWS Services for local development
(override API endpoints to point at local Docker containers)
Free and Pro Tiers
(Pro gets more services, tools, and supports a great project)
Can also be used to
run your Lambdas
8 . 8
62. ServerlessServerless
https://serverless.com/
Command Line Tools
Processes (more) YAML-ish files into CloudFormation
Templates - mixed syntax since cf functions can be used
Builds, packages, uploads to s3 for deployment
Multi-Cloud Support
Local Execution Environment via Docker
Paid PRO version includes:
dashboard
monitoring
alerts
ci/cd
rainbows
unicorns
9 . 1
63. Serverless vs SAMServerless vs SAM
https://sanderknape.com/2018/02/comparingawssamwithserverlessframework/
Check out Sander's blog post for great
comparison
In the end its all just
json/yaml
representations
9 . 2
67. HTTP API GatewayHTTP API Gateway
https://aws.amazon.com/blogs/compute/announcinghttpapisforamazonapigateway/
Cheaper Option
Fewer Dials to Set
https://docs.aws.amazon.com/apigateway/latest/developerguide/httpapivsrest.htmlv1 vs v2:
10 . 4
68. AWS XRay TracingAWS XRay Tracing
https://docs.aws.amazon.com/xray/latest/devguide/awsxray.html
Lots of moving parts!
Know what's going on!
Can configure lambda to have xray - just
use the SDK to send data
Or use some non-AWS alternative
(i.e. Espagon)
10 . 5
69. Map Reduce in λMap Reduce in λ
http://pywren.io/
S3 instead of HDFS
Lambdas instead of
Servers
10 . 6
70. Big Data with ServerlessBig Data with Serverless
https://www.slideshare.net/AmazonWebServices/buildingbigdataapplicationswithserverlessarchitecturesjune2017awsonlinetechtalks10 . 7
74. More Isn't Always BetterMore Isn't Always Better
Figure out the best bang for the buck -- don't guess, use data!
https://github.com/alexcasalboni/awslambdapowertuning
And figure out your POST-free-tier costs before you go too far down this path
Google: lambda hidden costs 10 . 11