Ron
Ron

Reputation: 988

Invoke C# .NET Core Lambda from AWS CodePipeline Action

AWS CodePipeline allows you to invoke a custom Lambda from an action as described here, https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.htmltion

I am having trouble determining how my C# Lambda function should be defined in order to access the input data from the pipeline.

I tried numerous attempts, was thinking it would be something similar to below. I have also tried to create my own C# classes that the input JSON data would be deserialized to.

public void FunctionHandler( Amazon.CodePipeline.Model.Job CodePipeline, ILambdaContext context)

Upvotes: 3

Views: 612

Answers (1)

Ron
Ron

Reputation: 988

I was able to find out a solution. Initially the first step that helped was to change the input parameter for my lambda function to a Stream. I was then able to convert the stream to a string and determine exactly what was being sent to me, e.g

    public void FunctionHandler(Stream input, ILambdaContext context)
    {
 ....
    }

Then, based on the input data I was able to map it to a C# class that wrapped the AWS SDK Amazon.CodePipeline.Model.Job class. It had to be mapped to the json property "CodePipeline.job". The below code worked, I was able to retrieve all input values.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.CodePipeline;
using Newtonsoft.Json;
using System.IO;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace lambdaEmptyFunction
{
    public class Function
    {
        public class CodePipelineInput
        {
            [JsonProperty("CodePipeline.job")]
            public Amazon.CodePipeline.Model.Job job { get; set; }
        }

        public void FunctionHandler(CodePipelineInput input, ILambdaContext context)
        {
            context.Logger.LogLine(string.Format("data {0} {1} {2}", input.job.AccountId, input.job.Data.InputArtifacts[0].Location.S3Location.BucketName, input.job.Id));
        }
    }
}

Upvotes: 7

Related Questions