Hot Zip the .net Core Project and Upload to Lambda
Building and deploying AWS Lambda (.Net core 3.i) Web APIs using Gitlab CI/CD
GitLab includes utilities that allows us to create pipelines that tin can be used for CI (Continuous Integration) and CD (Continuous Commitment)
Today we will be edifice a uncomplicated .Net API and automatically deploying information technology as soon as whatsoever commit is pushed on to our source control (git)
First we will get-go by creating our function
Next up, I have created a sample ASP.net project which includes Spider web API functionalities and I have included the Lamda entry point. You lot can clone information technology here.
The project basically exposes "weatherforcast" endpoint which returns an array of atmospheric condition forecasts
[
{
"date": "2019-07-16T19:04:05.7257911-06:00",
"temperatureC": 52,
"temperatureF": 125,
"summary": "Mild"
},
{
"appointment": "2019-07-17T19:04:05.7258461-06:00",
"temperatureC": 36,
"temperatureF": 96,
"summary": "Warm"
},
{
"date": "2019-07-18T19:04:05.7258467-06:00",
"temperatureC": 39,
"temperatureF": 102,
"summary": "Cool"
},
{
"date": "2019-07-19T19:04:05.7258471-06:00",
"temperatureC": 10,
"temperatureF": 49,
"summary": "Bracing"
},
{
"date": "2019-07-20T19:04:05.7258474-06:00",
"temperatureC": -1,
"temperatureF": 31,
"summary": "Dank"
} ]
This is automatically generated using Visual Studio 2019 Web API project.
Next nosotros will need to empathize how manual deployment of .Internet core projects work. To deploy lambda .Net projects, nosotros will be using the AWS lambda .Net tools. To install the lamba tools, using the command line run the following
dotnet tool install -chiliad Amazon.Lambda.Tools
Next, we but run the following command in our projection directory
dotnet lambda package
As seen in the screenshot in a higher place, 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 demand to create an API gateway to route the traffic to the lambda.I have created an API gateway with the following resources and configuration
So i accept deployed the API and tested it
Now it is time to automate it using GitLab CI/CD
Starting time we will need to create an AWS user to deploy the project with. So we will become to the IAM section in the AWS dashboard and and then create a user. Make sure to enable "Programmatic access" to become the key and secret.
And then click next, for the permissions. I'm going to give it full permission over Lambda only i would recommend tightening upward the security and giving it only permissions to update the lambda.
Then create the user. Later creating the user you should get this screen, copy the Access key ID and Secret access primal and salve them somewhere as we will need them later on.
Next, you volition need to have a repository fix upward on GitLab. yous tin can fork the ane i linked above. create a yaml file chosen ".gitlab-ci.yml" in the root directory of the repository.
First we will start past shaping the skeleton of our deployment process
stages:
- build
- deploy build_job:
stage: build
script:
- echo "build"
deploy_job:
stage: deploy
script:
- echo "deploy"
This defines ii main stages in our pipeline. The build phase and the deployment phase. In the build stage we are going to build our .Net project and in the deployment phase we are going to update our lambda lawmaking. Currently the script only prints "build" in the building stage and "deploy" in the deployment stage.
Next lets piece of work on the build stage. Equally mentioned before, building and packaging the project is done using one command. then let's add together that command to our pipeline.
build_job:
stage: build
script:
- dotnet lambda packet
Nonetheless, that will not piece of work so hands as this volition run on a linux machine with no .Internet SDK or whatever dependencies which will throw an exception as it does not know that dotnet is. And so lets fix this by telling the pipeline to use a docker container to run our build procedure. For the container we will utilize a container prototype that i have built that has the following:
- .Net SDK 3.1
- .Net Lambda Tools
- Zip utility
So to add this to our pipeline we specify a central called image.
build_job:
phase: build
prototype: ahmedghanima/dotnet-lambda
script:
- export PATH="$PATH:/root/.dotnet/tools" # add lambda tools to path
- dotnet lambda package
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 phone call it straight by only writing dotnet lambda
Next up, we will let gitlab shop our build naught output so we can download it and employ it in our deployment stages ( then that information technology tin can exist shared beyond stages ). This is done by specifying artifacts.
build_job:
stage: build
image: ahmedghanima/dotnet-lambda
script:
- export PATH="$PATH:/root/.dotnet/tools" # add lambda tools to path
- dotnet lambda package
artifacts:
expire_in: i calendar week
paths:
- '$CI_PROJECT_DIR/bin/Release/netcoreapp3.1/dotnet-deploy-example.nada' # saving output bundle
This means that the output of our build process "dotnet-deploy-instance.cypher" can now be downloaded through the Gitlab interface and can be shared across stages.
Now the build procedure should exist finished, lets move on to the deployment.
For the deployment nosotros will do the same also by using containers to run it so that we do not take to install the aws cli ourselves. Nosotros will be using an image provided by amazon.
amazon/lambda-build-node10.x
This image has the aws cli equally of the time of this postal service, aws does not have an official aws cli image. To deploy and update our lambda code we tin can use the update-function-code command in the aws cli.
aws lambda update-office-code --part-name dotNetCore-Example --zip-file fileb://$CI_PROJECT_DIR/bin/Release/netcoreapp3.1/dotnet-deploy-example.zip
Nevertheless, 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 tin can practise that by specifying that the deployment process depends on the build process.
deploy_job:
phase: deploy
image: amazon/lambda-build-node10.x
script:
- aws lambda update-function-code --function-proper noun dotNetCore-Case --nix-file fileb://$OUTPUT_FILE_PATH
dependencies:
- build_job
Finally, we demand to tell the aws cli to use our provided credentials that nosotros created above to exist able to update the office lawmaking otherwise we will receive an unauthorized error.
So lets go back to the Gitlab UI and become to our repository.And then lets caput to Settings → CI/CD. Gitlab provides surroundings variables that can be passed to our pipeline. so we volition use this feature to laissez passer the aws api key and cloak-and-dagger to our pipeline.
At present nosotros volition create iii variables:
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_DEFAULT_REGION
then add together the right value to each variable. In my case the default region is Frankfurt so that'southward european union-central-one.
Subsequently adding the variables the aws cli container should automatically pick them upwardly. So finally the gitlab-ci file should await like this
variables:
OUTPUT_FILE_PATH: '$CI_PROJECT_DIR/bin/Release/netcoreapp3.i/dotnet-deploy-example.aught' stages:
- build
- deploy build_job:
stage: build
image: ahmedghanima/dotnet-lambda
script:
- export PATH="$PATH:/root/.dotnet/tools" # add lambda tools to path
- dotnet lambda package
artifacts:
expire_in: 1 week
paths:
- $OUTPUT_FILE_PATH # saving output bundle
deploy_job:
phase: deploy
paradigm: amazon/lambda-build-node10.ten
script:
- aws lambda update-office-code --function-name dotNetCore-Example --cypher-file fileb://$OUTPUT_FILE_PATH
dependencies:
- build_job
Now any commit that will exist pushed to our repository would burn the pipeline causing information technology to automatically build and deploy our project.
Resources
Gitlab CI/CD Documentation
Gitlab-ci file full references
Feel free to cheque out my other articles and my weblog
If you found this commodity any useful, y'all know what to practice now. Striking that clap button and follow me to get more articles on your feed.
Source: https://ahgh.medium.com/gitlab-includes-utilities-that-allows-us-to-create-pipelines-that-can-be-used-for-ci-continuous-7ec4bbb59f31
0 Response to "Hot Zip the .net Core Project and Upload to Lambda"
Post a Comment