Slide 1

Slide 1 text

AWS Lambda with Serverless and Java Silicon Valley Code Camp 2017 Manish Pandit @lobster1234

Slide 2

Slide 2 text

About Manish Pandit Director of Platform Engineering @Marqeta Twitter : @lobster1234 Blog: lobster1234.github.io LinkedIn: in/mpandit

Slide 3

Slide 3 text

Serverless Serverless is the finest grain abstraction in distributed computing 100% managed with zero infrastructure concerns Lowest cost compared to Servers and Containers (if designed correctly!)

Slide 4

Slide 4 text

Why Serverless Ideal fit for microservices architectures - functions have limited, well defined scopes Pricing - you pay for what you use No need to worry about infrastructure

Slide 5

Slide 5 text

AWS Lambda Function as a Service Simple Pricing Model - Memory * time Event Sources - DynamoDB, S3, API Gateway, Cloudwatch, SNS... Security via VPC and IAM Roles

Slide 6

Slide 6 text

Serverless Framework Node.js based CLI Makes working with FaaS much, much easier Simple, YAML based configuration abstracts Cloudformation Supports many cloud providers - AWS, OpenWhisk, GCP, Azure Supports stages Popular, feature rich, well documented

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Install bash-3.2$ npm install serverless -g + [email protected] bash-3.2$ serverless --version 1.23.0

Slide 9

Slide 9 text

Configure AWS - Option 1 bash-3.2$ aws configure AWS Access Key ID [****************4MTA]: AWS Secret Access Key [****************g2uU]: Default region name [us-east-1]: Default output format [json]: bash-3.2$

Slide 10

Slide 10 text

Configure AWS - Option 2 bash-3.2$ serverless config credentials --provider aws --key XXXX --secret YYY If you already have a default profile…. Serverless: Setting up AWS... Serverless: Failed! ~/.aws/credentials already has a "default" profile. Use the overwrite flag ("-o" or "--overwrite") to force the update

Slide 11

Slide 11 text

Create a Project bash-3.2$ serverless create --template aws-java-maven Serverless: Generating boilerplate... Serverless: Successfully generated boilerplate for template: "aws-java-maven" Serverless: NOTE: Please update the "service" property in serverless.yml with your service name bash-3.2$

Slide 12

Slide 12 text

The Project Structure bash-3.2$ tree . . ├── pom.xml ├── serverless.yml └── src └── main ├── java │ └── com │ └── serverless │ ├── ApiGatewayResponse.java │ ├── Handler.java │ └── Response.java └── resources └── log4j.properties 6 directories, 6 files

Slide 13

Slide 13 text

serverless.yml service: aws-java-maven # NOTE: update this with your service name provider: name: aws runtime: java8 package: artifact: target/hello-dev.jar functions: hello: handler: com.serverless.Handler # The following are a few example events you can configure # NOTE: Please make sure to change your handler code to work with those events # Check the event documentation for details # events: # - http: # path: users/create # method: get

Slide 14

Slide 14 text

Handler Class package com.serverless; import … public class Handler implements RequestHandler, ApiGatewayResponse>

Slide 15

Slide 15 text

handleRequest Method @Override public ApiGatewayResponse handleRequest(Map input, Context context) { BasicConfigurator.configure(); LOG.info("received: " + input); Response responseBody = new Response("Go Serverless v1.x! Your function executed successfully!", input); return ApiGatewayResponse.builder() .setStatusCode(200) .setObjectBody(responseBody) .setHeaders(Collections.singletonMap("X-Powered-By", "AWS Lambda & serverless")) .build(); }

Slide 16

Slide 16 text

Build bash-3.2$ mvn clean install [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building hello dev [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.666 s [INFO] Finished at: 2017-10-07T01:34:10-07:00 [INFO] Final Memory: 16M/214M [INFO] ------------------------------------------------------------------------

Slide 17

Slide 17 text

Deploy bash-3.2$ serverless deploy Serverless: Packaging service... Serverless: Creating Stack... Serverless: Checking Stack create progress... Serverless: Stack create finished... Serverless: Uploading CloudFormation file to S3... Serverless: Uploading artifacts... Serverless: Validating template... Serverless: Updating Stack... Serverless: Checking Stack update progress... ............... Serverless: Stack update finished... Service Information service: aws-java-maven stage: dev region: us-east-1 stack: aws-java-maven-dev api keys: None endpoints: None functions: hello: aws-java-maven-dev-hello

Slide 18

Slide 18 text

Invoke bash-3.2$ serverless invoke -f hello { "statusCode": 200, "body": "{\"message\":\"Go Serverless v1.x! Your function executed successfully!\",\"input\":{}}", "headers": { "X-Powered-By": "AWS Lambda & serverless" }, "isBase64Encoded": false } bash-3.2$

Slide 19

Slide 19 text

Execution 1.An event source generates an event, or a trigger 2.A new container is spun off or an existing one is reused to launch the runtime 3.The function (script if interpreted language, binary if compiled) is executed 4.The runtime exits

Slide 20

Slide 20 text

Invoke Local bash-3.2$ serverless invoke local -f hello Serverless: In order to get human-readable output, please implement "toString()" method of your "ApiGatewayResponse" object. 0 [main] INFO com.serverless.Handler - received: {} com.serverless.ApiGatewayResponse@77cd7a0 bash-3.2$

Slide 21

Slide 21 text

Using an HTTP Event Source service: aws-java-maven # NOTE: update this with your service name provider: name: aws runtime: java8 package: artifact: target/hello-dev.jar functions: hello: handler: com.serverless.Handler events: - http: path: /hello method: get

Slide 22

Slide 22 text

Deploy with a new stage! bash-3.2$ serverless deploy --stage prod Serverless: Packaging service... Serverless: Creating Stack... Serverless: Checking Stack create progress… Serverless: Updating Stack... Serverless: Checking Stack update progress... Serverless: Stack update finished... Service Information service: aws-java-maven stage: prod region: us-east-1 stack: aws-java-maven-prod api keys: None endpoints: GET - https://f582xxq581.execute-api.us-east-1.amazonaws.com/prod/hello functions: hello: aws-java-maven-prod-hello

Slide 23

Slide 23 text

Test bash-3.2$ curl https://f582xxq581.execute-api.us-east-1.amazonaws.com/prod/hello {"message":"Go Serverless v1.x! Your function executed successfully!","input":{"resource":"/hello","path":"/hello","httpMethod":"GET","headers":{"Accept":"*/*","Cl oudFront-Forwarded-Proto":"https","CloudFront-Is-Desktop-Viewer":"true","CloudFront-Is-Mobile- Viewer":"false","CloudFront-Is-SmartTV-Viewer":"false","CloudFront-Is-Tablet-Viewer":"false","CloudFront- Viewer-Country":"US","Host":"f582xxq581.execute-api.us-east-1.amazonaws.com","User- Agent":"curl/7.51.0","Via":"1.1 3cc911e7eb2df956e3f7c8f27c198a0b.cloudfront.net (CloudFront)","X-Amz-Cf- Id":"rngpB7WCzOt_XnMXzDc0Pv5i9Eq7_ZA1olb08W84xi5Of8Od4VvVVw==","X-Amzn-Trace-Id":"Root=1-59d895b4- 375b518e32e7a6866c80fa0a","X-Forwarded-For":"69.181.73.181, 205.251.214.68","X-Forwarded-Port":"443","X- Forwarded- Proto":"https"},"queryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{" path":"/prod/hello","accountId":"xxxxxxxxxxxx","resourceId":"07sycy","stage":"prod","requestId":"cab13cdc- ab3c-11e7-b85c- 0fc68f7d77c1","identity":{"cognitoIdentityPoolId":null,"accountId":null,"cognitoIdentityId":null,"caller":nu ll,"apiKey":"","sourceIp":"69.181.73.181","accessKey":null,"cognitoAuthenticationType":null,"cognitoAuthenti cationProvider":null,"userArn":null,"userAgent":"curl/7.51.0","user":null},"resourcePath":"/hello","httpMeth od":"GET","apiId":"f582xxq581"},"body":null,"isBase64Encoded":false}} bash-3.2$

Slide 24

Slide 24 text

Serverless Logs bash-3.2$ serverless logs -f hello START RequestId: 32c366cc-ab41-11e7-bcba-6f7680b04a52 Version: $LATEST 2017-10-07 09:23:38 <32c366cc-ab41-11e7-bcba-6f7680b04a52> INFO com.serverless.Handler:20 - received: {} 0 [main] INFO com.serverless.Handler - received: {} END RequestId: 32c366cc-ab41-11e7-bcba-6f7680b04a52 REPORT RequestId: 32c366cc-ab41-11e7-bcba-6f7680b04a52 Duration: 411.34 ms Billed Duration: 500 ms Memory Size: 1024 MB Max Memory Used: 63 MB START RequestId: e41254f3-ab41-11e7-ab1b-750906e00517 Version: $LATEST 2017-10-07 09:28:34 INFO com.serverless.Handler:20 - received: {} END RequestId: e41254f3-ab41-11e7-ab1b-750906e00517 REPORT RequestId: e41254f3-ab41-11e7-ab1b-750906e00517 Duration: 9.92 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 63 MB

Slide 25

Slide 25 text

Serverless Metrics bash-3.2$ serverless metrics -f hello Service wide metrics October 6, 2017 2:21 AM - October 7, 2017 2:21 AM Invocations: 4 Throttles: 0 Errors: 0 Duration (avg.): 206.84ms bash-3.2$

Slide 26

Slide 26 text

Serverless Metrics w/ Stages bash-3.2$ serverless metrics --stage dev -f hello hello October 6, 2017 2:29 AM - October 7, 2017 2:29 AM Invocations: 8 Throttles: 0 Errors: 0 Duration (avg.): 120.89ms bash-3.2$ serverless metrics --stage prod -f hello hello October 6, 2017 2:29 AM - October 7, 2017 2:29 AM Invocations: 6 Throttles: 0 Errors: 0 Duration (avg.): 187.97ms

Slide 27

Slide 27 text

Serverless Info bash-3.2$ serverless info Service Information service: aws-java-maven stage: dev region: us-east-1 stack: aws-java-maven-dev api keys: None endpoints: None functions: hello: aws-java-maven-dev-hello

Slide 28

Slide 28 text

Serverless Info w/Stages bash-3.2$ serverless info --stage prod Service Information service: aws-java-maven stage: prod region: us-east-1 stack: aws-java-maven-prod api keys: None endpoints: GET - https://f582xxq581.execute-api.us-east-1.amazonaws.com/prod/hello functions: hello: aws-java-maven-prod-hello bash-3.2$

Slide 29

Slide 29 text

Remove Stack bash-3.2$ serverless remove Serverless: Getting all objects in S3 bucket... Serverless: Removing objects in S3 bucket... Serverless: Removing Stack... Serverless: Checking Stack removal progress... .... Serverless: Stack removal finished… bash-3.2$

Slide 30

Slide 30 text

Remove Stack w/Stages bash-3.2$ serverless remove --stage prod Serverless: Getting all objects in S3 bucket... Serverless: Removing objects in S3 bucket... Serverless: Removing Stack... Serverless: Checking Stack removal progress... ................ Serverless: Stack removal finished… bash-3.2$ curl https://f582xxq581.execute-api.us-east-1.amazonaws.com/prod/hello curl: (35) SSL peer handshake failed, the server most likely requires a client certificate to connect bash-3.2$

Slide 31

Slide 31 text

Lambda Tips 1. Write your code in a way that it can be tested without an event source 2. Lambda cannot execute for longer than 5 minutes 3. Coordinating many Lambdas can be difficult, use AWS Step Functions 4. Do not persist anything on the disk - it may not be available 5. Be aware of cold start - JVM will take a while to start 6. Do not write lambdas for everything - if a service is high throughput, you’re better off with ECS or EC2 given the costs 7. Lambdas can be tied to VPCs and have IAM execution roles 8. Serverless supports API Gateway Proxy Integration by default - everything gets passed to the function

Slide 32

Slide 32 text

Resources Serverless Framework - https://serverless.com AWS Lambda - https://aws.amazon.com/lambda My Tutorial - https://lobster1234.github.io/categories/serverless

Slide 33

Slide 33 text

Questions Manish Pandit @lobster1234 lobster1234.github.io