You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在TypeScript CDK中获取Python Lambda代码包S3路径并传递至部署阶段

解决方案

步骤1:从Prebuild步骤获取S3存储位置

ShellStep的执行产物会自动上传到CDK管理的S3桶中,你可以通过prebuild.output.s3Location直接获取对应的桶名和对象键,这两个值就是Python代码压缩包的完整S3路径信息。

步骤2:修改PipelineStage接收参数

调整PipelineStage的构造函数,添加参数来接收S3桶名和对象键,再传递给内部的LambdaStack:

// PipelineStage.ts
import { Stage, StageProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { LambdaStack } from './LambdaStack';

export interface PipelineStageProps extends StageProps {
  lambdaCodeS3Bucket: string;
  lambdaCodeS3Key: string;
}

export class PipelineStage extends Stage {
  constructor(scope: Construct, id: string, props: PipelineStageProps) {
    super(scope, id, props);

    new LambdaStack(this, 'LambdaStack', {
      lambdaCodeS3Bucket: props.lambdaCodeS3Bucket,
      lambdaCodeS3Key: props.lambdaCodeS3Key,
    });
  }
}

步骤3:修改LambdaStack使用S3代码包

在LambdaStack中接收S3参数,并用它来创建Lambda函数:

// LambdaStack.ts
import { Stack, StackProps, aws_lambda } from 'aws-cdk-lib';
import { Construct } from 'constructs';

export interface LambdaStackProps extends StackProps {
  lambdaCodeS3Bucket: string;
  lambdaCodeS3Key: string;
}

export class LambdaStack extends Stack {
  constructor(scope: Construct, id: string, props: LambdaStackProps) {
    super(scope, id, props);

    new aws_lambda.Function(this, 'MyLambda', {
      runtime: aws_lambda.Runtime.PYTHON_3_11,
      handler: 'index.handler', // 替换为你的Python代码处理函数路径
      code: aws_lambda.Code.fromBucket(
        aws_lambda.Bucket.fromBucketName(this, 'LambdaCodeBucket', props.lambdaCodeS3Bucket),
        props.lambdaCodeS3Key
      ),
    });
  }
}

步骤4:在主Stack中传递S3参数

回到AwsCdkLearnStack,将Prebuild步骤的S3位置信息传递给PipelineStage:

// AwsCdkLearnStack.ts
import { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { CodePipeline, CodePipelineSource, ShellStep } from 'aws-cdk-lib/pipelines';
import { PipelineStage } from './PipelineStage';

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

    const source = CodePipelineSource.gitHub('bharath-t/aws-cdk-learn', 'main');
    const python_source = CodePipelineSource.gitHub('bharath-t/python-learn', 'main');

    const prebuild = new ShellStep('Prebuild', {
      input: python_source,
      primaryOutputDirectory: './src',
      commands: ['pip install flake8 pytest build', 'flake8 --exit-zero', 'pytest'],
    });

    const pipeline = new CodePipeline(this, 'TestPipeline', {
      pipelineName: 'TestPipeline',
      synth: new ShellStep('Synth', {
        input: source,
        additionalInputs: {
          '../siblingdir': prebuild,
        },
        commands: ['npm ci', 'npx cdk synth'],
      })
    });

    const betaStage = new PipelineStage(this, 'BetaStage', {
      stageName: 'beta',
      lambdaCodeS3Bucket: prebuild.output.s3Location.bucketName,
      lambdaCodeS3Key: prebuild.output.s3Location.objectKey,
    });

    pipeline.addStage(betaStage);
  }
}

额外说明

  • 如果你需要使用整个Python仓库的压缩包,而非primaryOutputDirectory指定的./src目录,直接删除prebuild配置中的primaryOutputDirectory即可。
  • CDK会自动管理S3中代码包的生命周期,无需手动上传或清理文件。

内容的提问来源于stack exchange,提问作者Bharath Thati

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 09:15:13