云形成lambda不从代码构建中挑选jar

问题描述 投票:2回答:1

我尝试使用Code Pipeline自动化代码部署。它使用Git Hub - >代码构建 - >云形成,如wiki中所述

AWS Automation of Lambda

this thread提出的一些改变后,我设法让管道运行

但是,每当我使用代码管道时,Lambda测试都无法说明找不到类。

为了验证,我直接在AWS lambda控制台上传了jar,它工作正常。

我还验证了由“MyAppBuild”文件夹中的aws代码构建的jar,它包含zip文件中的target / app-1.0-SNAPSHOT.jar中的jar文件以及我的SamTemplate.yml。

这是SamTemplate.yml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Outputs the time

Parameters:
  SourceBucket:
    Type: String
    Description: S3 bucket name for the CodeBuild artifact
  SourceArtifact:
    Type: String
    Description: S3 object key for the CodeBuild artifact

Resources:
  TimeFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: com.xxx.Hello::handleRequest
      Runtime: java8
      CodeUri:
         Bucket: !Ref SourceBucket
         Key: !Ref SourceArtifact
      Events:
        MyTimeApi:
          Type: Api
          Properties:
            Path: /TimeResource
            Method: GET

这是buildSpec.yaml

version: 0.2
phases:
  build:
    commands:
      - echo Build started on `date`
      - mvn test
  post_build:
    commands:
      - echo Build completed on `date`
      - mvn package
  install:
    commands:
      - aws cloudformation package --template-file SamTemplate.yaml --s3-bucket codepipeline-us-east-1-xxxx
                                       --output-template-file NewSamTemplate.yaml
artifacts:
  type: zip
  files:
    - SamTemplate.yaml
    - target/app-1.0-SNAPSHOT.jar

有什么建议可以尝试吗?我用maven。

aws-lambda amazon-cloudformation aws-codebuild aws-sam-cli
1个回答
0
投票

最后,经过几次尝试后,我发现了一个可能的解决方案,包括aws代码构建,云形成和lambda。

重点是代码构建为工件中提到的所有文件创建了一个包装器zip:

这是必须赋予aws lambda的相同zip文件。为了使aws lambda接受一个zip作为有效,类应该是根文件夹,依赖库应该在libs文件夹中。

所以我设法做了我的构建规范。

version: 0.2

phases:
  install:
    commands:
      - aws cloudformation package --template-file SamTemplate.yaml --s3-bucket codepipeline-us-east-1-XXXXXXXX
                                   --output-template-file NewSamTemplate.yaml
  build:
    commands:
      - echo Build started on `date`
      - gradle build clean
      - gradle test

  post_build:
    commands:
      - echo Build started on `date`
      - gradle build
      - mkdir -p deploy
      - cp -r build/classes/main/* deploy/
      - cp NewSamTemplate.yaml deploy/
      - cp -r build/libs deploy/
      - ls -ltr deploy
      - ls -ltr build
      - echo Build completed on `date`
      - echo Build is complete

artifacts:
  type : zip
  files:
    - '**/*'
  base-directory : 'deploy'
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.