1

我正在使用 CloudFormation 在 lambda 中做一个无服务器应用程序。

在我的 CodeBuild 项目中,我将其设置为压缩输出并将其放置在“myBucket\AWSServerless1.zip”中,并且它确实正确。

现在我正在处理我的 CodePipeline,我参考了原始的 CodeBuild 项目。然而,现在它把它放在 codepipeline-us-west-##### 中。没关系。问题是 .zip 文件有一个 RANDOM 名称。CodePipeline 会忽略我在 CodeBuild 项目中给它的名称。

在 serverless.template 中,我必须指定 CodeUri(出于某种奇怪的原因,这似乎是 CodeBuild 项目的输出)。如果我引用 AWSServerless1.zip,它可以正常工作(但它没有构建到那里,所以它的代码过时)......但是......

由于调用 CodeBuild 的 CodePipeline 给它一个随机名称,我应该如何在 serverless.template 中引用 ACTUAL BuildArtifact?

4

2 回答 2

0

I know this is very weird, I was stuck with this behavior of CodePipeline and then had to rewrite the buildspec to make CodePipeline work. CodePipeline makes it's own zip file even if you create your own zip through CodeBuild as well and that too with a unique name.

But there is one way out, Codepipeline will create one zip file but it will unzip it while giving the artifact to CodeDeploy. So you need not worry about its name. CodeDeploy will get the unzipped version of your code. CodePipeline keeps track of the name and it will always point to the newest one.

Suppose :

CodePipeline creates artifact : some-random-name.zip

some-random-name
       |- deploy/lib/lambda-code
       |- some-file.yaml

Whenever CodePipeline gives artifact to CodeDeploy, it will unzip it so you can anytime refer the code under some-random-name.zip

So in your case when you give CodeUri in the SAM template just give the folder name which is deploy where your lambda code is present.

Resources:
  Hello:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: example.MyHandler
      Runtime: java8
      CodeUri: deploy
      Description: ''
      MemorySize: 512
      Timeout: 15

Hope this helps.

于 2019-09-14T05:30:45.437 回答
0

我遇到了同样的错误,我设法通过执行以下操作来解决它:

1- 在构建规范 ( buildspec.yml ) 上添加一个 sam package 命令(这会生成一个package.yml,cloudformation 将使用它来部署 lambda)。

build:
  commands:
    - sam package
      --template-file ../template.yaml
      --output-template-file ../package.yml
      --s3-bucket onnera-ci-cd-bucketcode here

2-将package.yml添加到输出工件

artifacts:
  files:
    - DeviceProvisioning/package.yml

3- 在将部署的template.yaml上直接引用 CodeUri(在内部,这将通过 codebuild 的输出工件解析到存储桶)。

Resources:
  DeviceProvisioningFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: DeviceProvisioningFunction/target/DeviceProvisioningFunction-1.0.jar

4-在管道上使构建阶段的输出在部署阶段可用:

const buildOutput = [new codepipeline.Artifact()];
const buildAction = new codepipeline_actions.CodeBuildAction({
  actionName: 'CodeBuild',
  project: deviceProvisioning,
  input: sourceOutput,
  outputs: buildOutput, 
});

5- 使用构建输出在部署操作中指定模板路径:

const deployAction = new codepipeline_actions.CloudFormationCreateUpdateStackAction({
  extraInputs: buildAction.actionProperties.outputs,
  actionName: "UpdateLambda",
  stackName: "DeviceProvisioningStack",
  adminPermissions: true,
  templatePath: buildOutput[0].atPath("package.yml"),
  cfnCapabilities: [CfnCapabilities.AUTO_EXPAND, CfnCapabilities.NAMED_IAM]
});

确保构建阶段的输出工件在部署阶段可用。

在此处输入图像描述

于 2021-12-13T08:17:58.887 回答