Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Impl. an AWS Lambda function in Java, building and deploying w/ AWS CodeBuild

1e2ead439777ff94d9b2dd11a0607e01?s=47 Wolf Paulus
October 06, 2018
1.9k

Impl. an AWS Lambda function in Java, building and deploying w/ AWS CodeBuild

Java sometimes doesn't seem to be a 1st class citizen when it comes to AWS Lambda, but with just a few considerations, it's easy to implement an efficient AWS Lambda function in Java. To become useful, we'll also put the Lambda function behind the AWS API Gateway, so that it can be called from the web (i.e. with an HTTP GET or POST request).

Once we have done that we'll push the code into a Github Repository and move the build and deployment process into AWS CodeBuild. This (still new) AWS service for Continuous Integration and Deployment (aka CI/CD) will pull the code from the Git Repo and build the artifact, puts it into an S3 bucket, from which it is deployed into AWS Lambda ...

Come on .. that's cool stuff: Software Development meets Dev Ops.

1e2ead439777ff94d9b2dd11a0607e01?s=128

Wolf Paulus

October 06, 2018
Tweet

Transcript

  1. https://wolfpaulus.com Wolf Pauus Desert Code Camp - 2018 Implementing AWS

    functions in Java, 
 building and deploying with CodePipeline Wolf Paulus https://wolfpaulus.com
  2. https://wolfpaulus.com AWS Lambda functions in Java … 
 Building and

    deploying with AWS CodeBuild Implementing Deploying Invoking Controlling (De-)Serialization Continuous Integration and Deployment API-Gateway Integration
  3. https://wolfpaulus.com Implementing and Invoking an AWS Lambda functions in Java

    … aws_access_key_id and aws_secret_access_key, stored in ~/.aws/credentials IAM user needs the following Permissions: IAMFullAccess, AWSLambdaFullAccess PrimeCheck implements RequestHandler<PrimeRequest, PrimeResponse> package com.amazonaws.services.lambda.runtime; import com.amazonaws.services.lambda.runtime.Context; /** * * Lambda request handlers implement AWS Lambda Function application logic using plain old java objects * as input and output. * * @param <I> The input parameter type * @param <O> The output parameter type */ public interface RequestHandler<I, O> { /** * Handles a Lambda Function request * @param input The Lambda Function input * @param context The Lambda execution environment context object. * @return The Lambda Function output */ public O handleRequest(I input, Context context); } https://github.com/wolfpaulus/PrimeCheck
  4. https://wolfpaulus.com Implementing and Invoking an AWS Lambda functions in Java

    … taking control over (De-)Serialization aws_access_key_id and aws_secret_access_key, stored in ~/.aws/credentials IAM user needs the following Permissions: IAMFullAccess, AWSLambdaFullAccess PrimeCheckStream implements RequestStreamHandler /* Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. */ package com.amazonaws.services.lambda.runtime; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; /** * Low-level request-handling interface, Lambda stream request handlers implement AWS Lambda Function application logic * using input and output stream */ public interface RequestStreamHandler { /** * Handles a Lambda Function request * @param input The Lambda Function input stream * @param output The Lambda function output stream * @param context The Lambda execution environment context object. * @throws IOException */ public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException; } https://github.com/wolfpaulus/PrimeCheckStream
  5. https://wolfpaulus.com Implementing and Invoking an AWS Lambda functions in Java

    … accessible via API Gateway aws_access_key_id and aws_secret_access_key, stored in ~/.aws/credentials IAM user needs the following Permissions: IAMFullAccess, AWSLambdaFullAccess, AmazonAPIGatewayAdministrator Create new policy, attach ./aws/iam-deploy-policy.json and to user's group PrimeCheckAPI implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> /** * Handles a Lambda Function request * * @param requestEvent The Lambda Function input stream * @param context The Lambda execution environment context object. */ @Override public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent requestEvent, final Context context) { final APIGatewayProxyResponseEvent responseEvent = new APIGatewayProxyResponseEvent(); try { final Gson gson = new Gson(); final PrimeRequest request = gson.fromJson(requestEvent.getBody(), PrimeRequest.class); final PrimeResponse response = Prime.check(request.getNumber()); final String body = gson.toJson(response); responseEvent.setBody(body); responseEvent.setStatusCode(200); Log.debug("handleRequest ok " + body); } catch (Exception ex) { responseEvent.setBody(ex.toString()); responseEvent.setStatusCode(500); Log.error(ex.toString()); } return responseEvent; } https://github.com/wolfpaulus/PrimeCheckAPI
  6. https://wolfpaulus.com CICD 4 o more laptop builds https://github.com/wolfpaulus/PrimeCheckCICD PrimeCheckCICD implements

    RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent>
  7. https://wolfpaulus.com Building a Pipeline for Serverless Applications Source Code Repository

    AWS CodeBuild AWS Cloudformation setup guide: https://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html AWS Documentation » AWS Lambda » Developer Guide » AWS Lambda Applications » Automating Deployment of Lambda Applications » Building a Pipeline for Your Serverless Application
  8. https://wolfpaulus.com IAM Account with Permissions: - AmazonS3FullAccess Create AWS CloudFormation

    role and add the AWSLambdaExecute policy Create a Role to Delegate Permissions to an AWS Service add buildspec.yml and template.yml to your project Publish your code at GitHub AWS Documentation » AWS Lambda » Developer Guide » AWS Lambda Applications » Automating Deployment of Lambda Applications » Building a Pipeline for Your Serverless Application Building a Pipeline for Serverless Applications setup guide: https://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html
  9. https://wolfpaulus.com IAM Account with Permissions: - AmazonS3FullAccess AWS Documentation »

    AWS Lambda » Developer Guide » AWS Lambda Applications » Automating Deployment of Lambda Applications » Building a Pipeline for Your Serverless Application Building a Pipeline for Serverless Applications setup guide: https://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html
  10. https://wolfpaulus.com add buildspec.yml and template.yml to your project Publish your

    code at GitHub AWS Documentation » AWS Lambda » Developer Guide » AWS Lambda Applications » Automating Deployment of Lambda Applications » Building a Pipeline for Your Serverless Application Building a Pipeline for Serverless Applications setup guide: https://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html version: 0.1 phases: build: commands: - chmod +x gradlew - ./gradlew buildZip post_build: commands: - aws cloudformation package --s3-bucket codepipeline-us-west-…. artifacts: files: - output-template.yml discard-paths: yes AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: P4 PrimeCheck Serverless App Resources: PostFunction: Type: AWS::Serverless::Function Properties: CodeUri: build/distributions/PrimeCICD-1.0.zip FunctionName: primecheck-cicd Handler: com.techcasita.lambda.Prime Runtime: java8 Timeout: 10 MemorySize: 512 Events: PostEvent: Type: Api Properties: Path: / Method: post
  11. https://wolfpaulus.com Create AWS CloudFormation role and add the AWSLambdaExecute policy

    Create a Role to Delegate Permissions to an AWS Service AWS Documentation » AWS Lambda » Developer Guide » AWS Lambda Applications » Automating Deployment of Lambda Applications » Building a Pipeline for Your Serverless Application Building a Pipeline for Serverless Applications setup guide: https://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html
  12. https://wolfpaulus.com Create your AWS CodePipeline 1. Connect to GitHub: and

    then choose the Repository and Branch 2. Choose AWS CodeBuild as your Build provider 3. In Deployment provider, choose AWS CloudFormation. https://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html AWS Documentation » AWS Lambda » Developer Guide » AWS Lambda Applications » Automating Deployment of Lambda Applications » Building a Pipeline for Your Serverless Application
  13. https://wolfpaulus.com Code Pipeline Cloud Watch Code Build Lambda API Gateway

    Cloud Formation
  14. https://wolfpaulus.com

  15. https://wolfpaulus.com Thanks for coming .. more at https://wolfpaulus.com