How to configure continuous integration for aws lambda with java using the services of aws aws-codebuild, codedeploy, cloudformation

Alex Diaz :

I am implementing a lambda function with the tool of continuous integrations of aws . CodeSource , CodeBuild CodePipeLine.

After set up all, when i test the lambda the result is

{
  "errorMessage": "Class not found: com.ad.client.App",
  "errorType": "java.lang.ClassNotFoundException"
}

Class not found: com.ad.client.App: java.lang.ClassNotFoundException
java.lang.ClassNotFoundException: com.ad.client.App
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)



All stage of Pipeline are succeeded(Source , Build , Deploy)

If a load the jar directly in the lambda console the result is the correct I review the log of the build and found this:

[Container] 2019/06/13 13:09:38 Running command echo THE PATH WORK IS !!! 
THE PATH WORK IS !!! 

[Container] 2019/06/13 13:09:38 Running command pwd 
/codebuild/output/src748698927/src 

[Container] 2019/06/13 13:09:38 Running command echo The list of file is !! 
The list of file is !! 

[Container] 2019/06/13 13:09:38 Running command ls 
Readme.md 
buildspec.yml 
dependency-reduced-pom.xml 
ftc-client.iml 
outputtemplate.yaml 
pom.xml 
src 
target 
template.yaml 

[Container] 2019/06/13 13:09:38 Running command echo CODE BUILD SRC DIRECTORY 
CODE BUILD SRC DIRECTORY 

[Container] 2019/06/13 13:09:38 Running command echo $CODEBUILD_SRC_DIR 
/codebuild/output/src748698927/src 

INFO] skip non existing resourceDirectory /codebuild/output/src748698927/src/src/main/resources 

In some portion of code show me that the path src is duplicated, i don't know if it has something to related with the problem

My config files are:

template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Ftc-client
Resources:
  FtcClientFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: com.ad.client.App::handleRequest
      Runtime: java8
      CodeUri: ./
      Events:
        MyFtcClientApi:
          Type: Api
          Properties:
            Path: /client
            Method: GET

buildspec.yml

version: 0.2

phases:
  install:
    runtime-versions:
      java: openjdk8
  build:
    commands:
      - echo Build started on `date`
      - mvn test
      - export BUCKET=my-bucket-for-test
      - aws cloudformation package --template-file template.yaml --s3-bucket $BUCKET --output-template-file outputtemplate.yaml
    finally:
      - echo THE PATH WORK IS !!!
      - pwd
      - echo The list of file is !!
      - ls
      - echo CODE BUILD SRC DIRECTORY
      - echo $CODEBUILD_SRC_DIR
  post_build:
    commands:
      - echo Build completed on `date`
      - mvn package
artifacts:
  files:
    - target/ftc-client-1.0-SNAPSHOT.jar
    - template.yaml
    - outputtemplate.yaml
  discard-paths: yes

The source code structure is :
/fclient/src/main/java/com/ad/App.java
/tclient/buildspec.yml
/fclient/pom.xml
/fclient/template.yaml

I want to make this but with Java : https://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html

thank for everyone whom can give me a cue

Alex Diaz :

This is the solution - it was necessary to unzip the jar in root of my code:

version: 0.2
phases:
  install:
    runtime-versions:
      java: openjdk8
  pre_build:
    commands:
      - echo Test started on `date`
      - mvn clean compile test
  build:
    commands:
      - echo Build started on `date`
      - export BUCKET=my-bucket-for-test
      - mvn package shade:shade
      - mv target/ftc-client-1.0-SNAPSHOT.jar
      - unzip ftc-client-1.0-SNAPSHOT.jar
      - rm -rf target tst src buildspec.yml pom.xml ftc-client-1.0-SNAPSHOT.jar
      - aws cloudformation package --template-file template.yaml --s3-bucket $BUCKET --output-template-file outputtemplate.yaml
  post_build:
    commands:
      - echo Build completed on `date` !!!
artifacts:
  files:
    - target/ftc-client-1.0-SNAPSHOT.jar
    - template.yaml
    - outputtemplate.yaml

https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=130905&siteId=1