【问题标题】:Cloud formation lambda not picking jar from code buildCloudformation lambda 未从代码构建中选择 jar
【发布时间】:2018-03-11 10:42:33
【问题描述】:

我尝试使用代码管道来自动化代码部署。它使用 wiki 中提到的 Git Hub -> Code Build -> Cloud Formation

AWS Automation of Lambda

this thread 建议的一些更改之后,我设法让管道运行

但是,每当我使用代码管道时,Lambda 测试都会失败,提示找不到该类。

为了验证,我直接在AWS lambda控制台上传了jar,效果很好。

我还验证了由 S3“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】:

    最后,经过几次尝试,我找到了一个可能的解决方案,用于使用 aws 代码构建、云形成和 lambda 进行打包。

    关键在于代码构建会为工件中提到的所有文件创建一个包装器压缩包

    这是必须提供给 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'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 2019-07-16
      • 2022-07-21
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多