Cloudformation lambda 没有从 codebuild 中挑选 jar

Cloud formation lambda not picking jar from code build

我尝试使用 Code Pipeline 来自动化代码部署。它使用 Git Hub -> Code Build -> Cloud Formation,如 wiki

中所述

AWS Automation of Lambda

建议的一些更改后,我设法获得了管道 运行

然而,每当我使用代码管道时,Lambda 测试都会失败,提示未找到 class。

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

我还验证了由 aws code build 在 S3 "MyAppBuild" 文件夹中构建的 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 code build、cloud formation 和 lambda 进行打包的可能解决方案。

重点是代码构建为工件中提到的所有文件创建了一个 wrapper 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'