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

如何定义C# .NET Core Lambda以获取AWS CodePipeline输入数据?

解决C# Lambda访问CodePipeline输入数据的问题

我之前也碰到过一模一样的问题,一开始自己瞎写类反序列化总是出问题,后来才发现AWS已经给我们封装好了现成的CodePipeline事件类,直接用就完事了!

正确的做法分这几步:

  1. 安装必要的NuGet包
    首先在你的Lambda项目里安装这几个包:

    • Amazon.Lambda.Core:Lambda的核心依赖包
    • Amazon.Lambda.CodePipelineEvents:专门处理CodePipeline事件的官方类库
    • 可选:Amazon.Lambda.Serialization.SystemTextJson(如果偏好使用System.Text.Json做序列化,默认可能用Newtonsoft,现在更推荐这个)
  2. 编写Lambda函数代码
    直接把函数的参数类型指定为CodePipelineEvent,Lambda运行时会自动帮你完成JSON反序列化,完全不用自己手动处理:

    using Amazon.Lambda.Core;
    using Amazon.Lambda.CodePipelineEvents;
    
    // 指定序列化器,避免反序列化失败
    [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
    
    namespace CodePipelineLambdaHandler
    {
        public class PipelineHandler
        {
            public async Task<string> HandleCodePipelineEvent(CodePipelineEvent pipelineEvent, ILambdaContext context)
            {
                // 访问流水线基础信息
                var pipelineName = pipelineEvent.Pipeline;
                var stageName = pipelineEvent.Stage;
                var actionName = pipelineEvent.Action;
    
                // 读取输入工件(Input Artifacts)的存储信息
                if (pipelineEvent.InputArtifacts != null && pipelineEvent.InputArtifacts.Any())
                {
                    var firstArtifact = pipelineEvent.InputArtifacts.First();
                    var artifactName = firstArtifact.Name;
                    var s3Location = firstArtifact.Location.S3Location;
                    var bucket = s3Location.BucketName;
                    var objectKey = s3Location.ObjectKey;
    
                    context.Logger.LogInformation($"找到输入工件:{artifactName},存储位置:{bucket}/{objectKey}");
                }
    
                // 读取Action配置的自定义参数
                if (pipelineEvent.ActionConfiguration != null)
                {
                    var configParams = pipelineEvent.ActionConfiguration.Configuration;
                    if (configParams.TryGetValue("MyCustomSetting", out var paramValue))
                    {
                        context.Logger.LogInformation($"自定义配置值:{paramValue}");
                    }
                }
    
                // 这里写你的业务逻辑,最后返回处理结果给CodePipeline
                return "Lambda函数处理完成";
            }
        }
    }
    
  3. 如果非要自己写反序列化类(不推荐)
    要是你不想依赖官方包,那得严格匹配CodePipeline输入JSON的结构,核心结构大概是这样的:

    public class CustomCodePipelineEvent
    {
        public string Pipeline { get; set; }
        public string Stage { get; set; }
        public string Action { get; set; }
        public List<InputArtifact> InputArtifacts { get; set; }
        public ActionConfiguration ActionConfiguration { get; set; }
        // 其他字段如AccountId、Region等按需添加
    }
    
    public class InputArtifact
    {
        public string Name { get; set; }
        public ArtifactLocation Location { get; set; }
    }
    
    public class ArtifactLocation
    {
        public string Type { get; set; } // 通常为"S3"
        public S3Location S3Location { get; set; }
    }
    
    public class S3Location
    {
        public string BucketName { get; set; }
        public string ObjectKey { get; set; }
    }
    
    public class ActionConfiguration
    {
        public Dictionary<string, string> Configuration { get; set; }
    }
    

小测试技巧

测试的时候,你可以在Lambda控制台的测试事件模板里选"CodePipeline",直接用官方提供的示例事件来模拟真实输入,不用自己瞎编JSON结构。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:38:28