AWS CDK 管道:如何将 CodeBuild 输出分配给 Lambda 代码?

AWS CDK pipeline : how to assign a CodeBuild output to a Lambda code?

我有以下有效的 AWS CDK 管道。它基本上从 2 个不同的 GitHub 存储库(一个用于应用程序代码,一个用于 cdk 代码)获取源代码并构建应用程序代码和 cdk 代码:

import * as cdk from 'aws-cdk-lib';
import * as codepipeline_actions from 'aws-cdk-lib/aws-codepipeline-actions';
import * as codepipeline from 'aws-cdk-lib/aws-codepipeline'
import * as codebuild from 'aws-cdk-lib/aws-codebuild'


import { Construct } from 'constructs';
import { CodePipeline, CodePipelineSource, ShellStep, CodeBuildStep } from 'aws-cdk-lib/pipelines';

export class CdkPipelineStack extends cdk.Stack {
    constructor(scope: Construct, id: string, props?: cdk.StackProps) {
        super(scope, id, props);

        // parameters
        const ConnectionArn = 'arn:aws:codestar-connections:region:account:connection/xxx-yyyy'


        // Source
        const cdkCodeStarConnection = CodePipelineSource.connection('owner/cdk-repo', 'master', {
            connectionArn: ConnectionArn
        })

        const lambdaCodeStarConnection = CodePipelineSource.connection('owner/lambda-repo', 'master', {
            connectionArn: ConnectionArn
        })

        // Building Lambda
        const lambdaBuildStep = new CodeBuildStep('BuildLambda', {
            buildEnvironment: {
                buildImage: codebuild.LinuxBuildImage.AMAZON_LINUX_2_3
            },
            input: lambdaCodeStarConnection,
            commands: ['variouscommands'],
            partialBuildSpec: codebuild.BuildSpec.fromObject({
                phases: {
                    install: {
                        "runtime-versions": {
                            python: "3.9"
                        },
                        "commands": ["python --version"]
                    }
                }
            })
        })

        const pipeline = new CodePipeline(this, 'Pipeline', {
            pipelineName: 'Pipeline',
            synth: new ShellStep('Synth', {
                input: cdkCodeStarConnection,
                commands: ['npm ci', 'npm run build', 'npx cdk synth', 'npx cdk ls'],
                additionalInputs: { 'lambda': lambdaBuildStep.addOutputDirectory('lambda') }
            })
        });
    }
}

现在,我想要实现的是能够将构建的代码(因此 lambdaBuildStep 的输出)分配给我在另一个堆栈中的 Lambda 函数,即在下面的代码片段中,我希望 assign_output_CodeBuildStep 成为 lambdaBuildStep 构建我的函数的 s3 位置:

import * as cdk from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';

import { Construct } from 'constructs';
import { Bucket } from 'aws-cdk-lib/aws-s3'

import {backendBuildStep} from 'cdk-pipeline-stack'; 
import {ArtifactMap} from 'aws-cdk-lib/pipelines';


export class LambdaMonolith extends cdk.Stack {
    constructor(scope: Construct, id: string) {
        super(scope, id);

        const s3Code = assign_output_CodeBuildStep

        const LambdaFunction = new lambda.Function(this, 'MyFunction', {
            code: lambdaCode,
            runtime: lambda.Runtime.PYTHON_3_8,
            handler: 'lambda.handler',
          });
    }
};

而且我似乎找不到将该信息从管道堆栈传输到 Lambda 堆栈的方法。

如有任何帮助,我们将不胜感激。

谢谢!

所以我找到了解决办法。也许不是解决方案。确实,这看起来很费解……而且我相信还有更好的方法。

所以解决方案在于 CodePipeline 构造中的 ShellStep 将 additionalInputs 的输出(因此前一个 CodeBuildStep 的结果,即 lambdaBuildStep)附加到一个动态的特定目录中已生成但存储在名为 CODEBUILD_SRC_DIR_BuildLambda_lambda_repo 的环境变量中,因此您可以看到它是 CodeBuildStep 名称和存储库的组合(破折号更改为下划线)。

所以我的解决方案是使用这个环境变量作为我的 Lambda 代码资产。

        const LambdaFunction = new lambda.Function(this, 'MyFunction', {
            code: lambda.Code.fromAsset(process.env.CODEBUILD_SRC_DIR_BuildLambda_lambda_repo || ""),
            runtime: lambda.Runtime.PYTHON_3_8,
            handler: 'lambda.handler',
          });