CI/CD for Lambda functions with Azure DevOps and .Net Core | by Lucas Bazetto | Towards Data Science

With each passing day, the serverless model is being more present in software development with new resources, tools, and approaches.
However, without control, we can easily create a nightmare. Like the uncle Ben said: “With great power comes great responsibility”.
One of the ways to help us with this “responsibility”, is having a CI/CD pipeline for the Lambda functions and extract the whole benefits of a full ALM.
Motivated by all these points in this post we are going to create a pipeline to deploy our Lambda Function.
So, let’s start! :D
Azure DevOps account
AWS Account
Basic knowledge of C#
We need to create a Programmatic access user that will be responsible for connecting the AWS and the Azure DevOps accounts.
For tests and only for tests you can set this permission to your user:
Never, never, never grant all access in production. In terms of security, less is more :)
With our AWS programmatic user, access key, and secret key, we can create the Azure DevOps project.
One of my favorite features of the Azure DevOps is the Pipeline. I’m a big fan of this resource because it is very easy to use and when necessary it can be used to handle huge and complex scenarios.
Firstly we are going to connect both accounts:
This Toolkit is necessary to create a service connection and enable the LambdaDeployFunction task that we will use in the pipeline job.
You can follow this link to install the tool.
With the toolkit installed on our account, we can move to the next step.
And create the service account:
AWS Service Connection:
Fill it with your AWS Credentials and add a Name:
For this example, a variable group is necessary as we will use it to store the Lambda Function ARN, this link contains all information necessary to create and configure the group.
Awesome! Now we have the Azure DevOps project connected within our AWS account. Let’s finally see a bit of code :D
You can clone the full source code from here:
git clone https://github.com/lbazetto/dotnet-core-aws-lambda-ci-cd-pipeline-azuredevops.git
As the project is based on .Net Lambda Tools, we are keeping the recommended folder structure:
.├── awsLambdaCICDPipeline.sln├── azure-pipelines.yml├── src│   └── awsLambdaCICDPipeline│       ├── Function.cs│       └── awsLambdaCICDPipeline.csproj└── test    └── awsLambdaCICDPipeline.Tests        ├── FunctionTest.cs        └── awsLambdaCICDPipeline.Tests.csproj
The src folder contains the Lambda function and the test folder contains the unit tests.
As the idea is the focus on the process instead of the Lambda function itself, the code is very simple, it just receives a string and does a ToUpper. And in the unit test, it just mocks the context and calls the function:
With everything sorted, its time to check the most important part of this tutorial, the pipeline. Our Pipeline has 2 stages:
This stage is responsible for build, run the unit tests, and publish the Artifacts to Azure DevOps.
That stage deploys the Lambda function that has been created in the first stage.
Splitting our build definition into stages makes it more flexible and help us to create some specific rules for each case. In this scenario, we can run the same definition either when creating a pull request or when the pull request is completed.
The YAML definition:
Now is time to create the Pipeline:
Choose the code location:
You might be required to put some credentials depending on your code location.
And we are able to see the YAML definition now:
Just save it and you can see the pipeline in your repo.Important: if your code is on Github, a pull request validation will be created automatically. If not you can set it via branch policies.
We have now:
The AWS Account linked to AzureDevOps
The source code with the Lambda Function and the YAML definition
When a pull is created, we can see the checks on Github.
And if you click in the pipeline details is possible to notice that the Deploy stage has been skipped.
Once the pipeline is completed and code is merged to master we are able to see that now both stages have been run:
Finally, we can see our Lambda function deployed from the Azure DevOps:
And that is it!
What do you think about it? Is there is something that you are doing in another way or do you think that I can improve? Please let me know :)
Thank you very much for your time!
Originally posted in https://devandops.cloud/ci-cd-for-lambda-functions-with-azure-devops-and-net-core/