Building and deploying AWS Lambda (.NET core 3.1) Web APIs using Gitlab CI/CD

GitLab includes utilities that allows us to create pipelines that can be used for CI (Continuous Integration) and CD (Continuous Delivery)

Today we will be building a simple .Net API and automatically deploying it as soon as any commit is pushed on to our source control (git)

First we will start by creating our function

Next up, I have created a sample ASP.net project which includes Web API functionalities and I have included the Lamda entry point. You can clone it here.

The project basically exposes “weatherforcast” endpoint which returns an array of weather forecasts

This is automatically generated using Visual Studio 2019 Web API project.

Next we will need to understand how manual deployment of .NET core projects work. To deploy lambda .Net projects, we will be using the AWS lambda .Net tools. To install the lamba tools, using the command line run the following

Next, we just run the following command in our project directory

As seen in the screenshot above, it created a zip file which we can upload to our lambda through the lambda dashboard.

To enable us to test our lambda deployment, we will need to create an API gateway to route the traffic to the lambda.I have created an API gateway with the following resources and configuration

Then i have deployed the API and tested it

Now it is time to automate it using GitLab CI/CD

First we will need to create an AWS user to deploy the project with. So we will go to the IAM section in the AWS dashboard and then create a user. Make sure to enable “Programmatic access” to get the key and secret.

Then click next, for the permissions. I’m going to give it full permission over Lambda but i would recommend tightening up the security and giving it only permissions to update the lambda.

Then create the user. After creating the user you should get this screen, copy the Access key ID and Secret access key and save them somewhere as we will need them later.

Next, you will need to have a repository set up on GitLab. you can fork the one i linked above. create a yaml file called “.gitlab-ci.yml” in the root directory of the repository.

First we will start by shaping the skeleton of our deployment process

This defines two main stages in our pipeline. The build stage and the deployment stage. In the build stage we are going to build our .Net project and in the deployment stage we are going to update our lambda code. Currently the script only prints “build” in the building stage and “deploy” in the deployment stage.

Next lets work on the build stage. As mentioned before, building and packaging the project is done using one command. so let’s add that command to our pipeline.

However, that will not work so easily as this will run on a linux machine with no .Net SDK or any dependencies which will throw an exception as it does not know that dotnet is. So lets fix this by telling the pipeline to use a docker container to run our build process. For the container we will use a container image that i have built that has the following:

  1. .Net SDK 3.1
  2. .Net Lambda Tools
  3. Zip utility

So to add this to our pipeline we specify a key called image.

Also notice that i have added the export command as the dotnet lamda tools are not added to the linux path by default, this is done to be able to call it directly by just writing dotnet lambda

Next up, we will let gitlab store our build zip output so we can download it and use it in our deployment stages ( so that it can be shared across stages ). This is done by specifying artifacts.

This means that the output of our build process “dotnet-deploy-example.zip” can now be downloaded through the Gitlab interface and can be shared across stages.

Now the build process should be finished, lets move on to the deployment.

For the deployment we will do the same as well by using containers to run it so that we do not have to install the aws cli ourselves. We will be using an image provided by amazon.

This image has the aws cli as of the time of this post, aws does not have an official aws cli image. To deploy and update our lambda code we can use the update-function-code command in the aws cli.

However, currently the deployment job does not have the zip file. so we need to tell it to download the artifacts from the build process. we can do that by specifying that the deployment process depends on the build process.

Finally, we need to tell the aws cli to use our provided credentials that we created above to be able to update the function code otherwise we will receive an unauthorized error.

So lets go back to the Gitlab UI and go to our repository.Then lets head to Settings → CI/CD. Gitlab provides environment variables that can be passed to our pipeline. so we will use this feature to pass the aws api key and secret to our pipeline.

Now we will create 3 variables:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_DEFAULT_REGION

then add the correct value to each variable. In my case the default region is Frankfurt so that’s eu-central-1.

After adding the variables the aws cli container should automatically pick them up. So finally the gitlab-ci file should look like this

Now any commit that will be pushed to our repository would fire the pipeline causing it to automatically build and deploy our project.

Resources

Gitlab CI/CD Documentation

Gitlab-ci file full references

Feel free to check out my other articles and my blog

If you found this article any useful, you know what to do now. Hit that clap button and follow me to get more articles on your feed.