<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sandeep Nandey</title>
    <description>The latest articles on DEV Community by Sandeep Nandey (@itssandeepnandey).</description>
    <link>https://dev.to/itssandeepnandey</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1059455%2Fd25446cd-9e75-4cc3-88f1-f3b3e8d2c486.png</url>
      <title>DEV Community: Sandeep Nandey</title>
      <link>https://dev.to/itssandeepnandey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/itssandeepnandey"/>
    <language>en</language>
    <item>
      <title>Deploy .Net Core AWS Lambda Function using AWS CodeBuild Pipeline</title>
      <dc:creator>Sandeep Nandey</dc:creator>
      <pubDate>Tue, 11 Apr 2023 05:21:48 +0000</pubDate>
      <link>https://dev.to/itssandeepnandey/deploy-net-core-aws-lambda-function-using-aws-codebuild-pipeline-4ch9</link>
      <guid>https://dev.to/itssandeepnandey/deploy-net-core-aws-lambda-function-using-aws-codebuild-pipeline-4ch9</guid>
      <description>&lt;p&gt;I have divided the deployment process into 5 steps&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Understand and update &lt;code&gt;aws-lambda-tools-defaults.json&lt;/code&gt; of our .Net project&lt;/li&gt;
&lt;li&gt;Add a &lt;code&gt;buildspec.yml&lt;/code&gt; file to our .Net project&lt;/li&gt;
&lt;li&gt;Create an AWS CodeBuild pipeline&lt;/li&gt;
&lt;li&gt;Permissions required to deploy&lt;/li&gt;
&lt;li&gt;Run CodeBuild pipeline and deploy our Lambda Function&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Let's begin&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Understand and update aws-lambda-tools-defaults.json of our .Net project
&lt;/h2&gt;

&lt;p&gt;When we create a new .Net Core Lambda Function Project in Visual Studio, a file name &lt;code&gt;aws-lambda-tools-defaults.json&lt;/code&gt; gets added to our project. You can set build options by using the fields in this file, which the Lambda tooling reads by default.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rnB-eLTr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onbpu552afora5wsu9bc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rnB-eLTr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onbpu552afora5wsu9bc.png" alt="Image description" width="298" height="221"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a configuration file to deploy our lambda function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8QZBXZIz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngzyhzs0ezz0kv1emm0l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8QZBXZIz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngzyhzs0ezz0kv1emm0l.png" alt="Image description" width="800" height="195"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;let's update this file to add a few more configurations that we will require for our deployment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "Information": ["All the command line options for the Lambda command can be specified in this file."],
  "profile": "default",
  "region": "eu-west-1",
  "configuration": "Release",
  "function-architecture": "x86_64",
  "function-runtime": "dotnet6",
  "function-memory-size": 256,
  "function-timeout": 30,
  "function-handler": "MyLambdaFunction::MyLambdaFunction.Function::FunctionHandler",
  "function-description": "This is a test lambda function for demo", 
  "s3-bucket": "my-build-source-bucket", 
  "s3-prefix": "MyLambdaFunction-Builds/"
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7227fg6---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xeeww0obx5detlovp4eo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7227fg6---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xeeww0obx5detlovp4eo.png" alt="Image description" width="800" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; We have added the below 3 configurations:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;function-description: Description of our lambda function&lt;/li&gt;
&lt;li&gt;s3-bucket: S3 bucket name to upload the build output&lt;/li&gt;
&lt;li&gt;s3-prefix: Folder name inside the above mentioned bucket where build output will be uploaded&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 2: Add a buildspec.yml file to our .Net project
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A &lt;strong&gt;buildspec&lt;/strong&gt; (build specification) is a collection of build commands and related settings, in YAML format, that &lt;strong&gt;CodeBuild&lt;/strong&gt; uses to run a build. If you include a buildspec as part of the source code, by default, the buildspec file must be named buildspec.yml and placed in the root of your source directory.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Right-click on project &amp;gt; Add new item &amp;gt; select a text file &amp;gt; rename to buildspec.yml&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6IcFUjc4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gsxnafhfq0i6mghcjz10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6IcFUjc4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gsxnafhfq0i6mghcjz10.png" alt="Image description" width="297" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the below content to buildspec.yml file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: 0.2
env:
  variables:
  # Set/Override value for below variables using Enviroment Variables of codebuild pipeline 
    LAMBDA_FUNCTION_NAME: "My-AWS-Lambda-Function"
  # Variables specific to this file
    PROJECT_FILE_DIR_PATH: "MyLambdaFunction"
phases:
  install:
    runtime-versions:
      dotnet: 6.0
  build:
    commands:
      - echo Deployment started on `date`for function $LAMBDA_FUNCTION_NAME
      - export PATH="$PATH:/root/.dotnet/tools"
      - dotnet new --install Amazon.Lambda.Templates::*
      - dotnet tool install -g Amazon.Lambda.Tools
      # Change directory to Project(.csproj) file folder
      - cd $PROJECT_FILE_DIR_PATH
      # Build and deploy to lambda function
      - dotnet lambda deploy-function --function-name $LAMBDA_FUNCTION_NAME

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;In this build spec declaration:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;version&lt;/strong&gt; represents the version of the build spec standard being used. This build spec declaration uses the latest version, 0.2.&lt;br&gt;
&lt;strong&gt;env&lt;/strong&gt; Optional sequence. Represents information for one or more custom environment variables.&lt;br&gt;
&lt;strong&gt;phases&lt;/strong&gt; represents the build phases during which you can instruct CodeBuild to run commands. These build phases are listed here as install and build You cannot change the spelling of these build phase names, and you cannot create more build phase names.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After updating &lt;code&gt;aws-lambda-tools-defaults.json&lt;/code&gt; and adding buildspec.yml file, commit your changes to your source control (GitHub, Bitbucket etc).&lt;/p&gt;

&lt;p&gt;That's it, we are now done with the code changes. For the next steps, we will work on AWS console.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Create an AWS CodeBuild pipeline and deploy
&lt;/h2&gt;

&lt;p&gt;Go to AWS console &amp;gt; CodeBuild &amp;gt; click on Build Project&lt;/p&gt;

&lt;p&gt;Provide &lt;strong&gt;Project configuration&lt;/strong&gt; and &lt;strong&gt;source&lt;/strong&gt; repository details (in this example we using &lt;strong&gt;GitHub&lt;/strong&gt; as our source provider) :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yPvZa9g6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ux3qebpttjsnfpmqryi5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yPvZa9g6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ux3qebpttjsnfpmqryi5.png" alt="Image description" width="737" height="1007"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Provide Environment details:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Role name will be important, CodeBuild pipeline will use this role to deploy our lambda function&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3pg9ooCJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d19jpscp92aogt8qqyar.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3pg9ooCJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d19jpscp92aogt8qqyar.png" alt="Image description" width="743" height="920"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Provide the path of &lt;strong&gt;buildspec&lt;/strong&gt; file&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lLYzw9Ux--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mova4siehm8npq3jct5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lLYzw9Ux--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mova4siehm8npq3jct5.png" alt="Image description" width="726" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Leave &lt;strong&gt;Batch configuration&lt;/strong&gt;, &lt;strong&gt;Artifacts&lt;/strong&gt; &amp;amp; &lt;strong&gt;Logs&lt;/strong&gt; section with default settings and click on '&lt;strong&gt;Create build project&lt;/strong&gt;'.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Permissions required to deploy:
&lt;/h2&gt;

&lt;p&gt;Make sure your lambda function role should have the below permissions.&lt;/p&gt;

&lt;p&gt;For this demo, I have added the below permissions to codebuild role &lt;code&gt;codebuild-Build-MyLambdaFunction-service-role&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For Resource, I have kept it for all resources, which you can update as per your requirement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:ListBucket",
                "s3:GetBucketAcl",
                "s3:GetBucketLocation",
                "s3:GetObjectVersion",
                "lambda:UpdateFunctionCode",
                "lambda:UpdateFunctionConfiguration",
                "lambda:GetFunctionConfiguration"
            ],
            "Resource": "*"
        }
    ]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5: Run CodeBuild pipeline and deploy our Lambda Function
&lt;/h2&gt;

&lt;p&gt;So now, our CodeBuild pipeline is ready and we have also added required permissions, let's run the pipeline and test the code build.&lt;/p&gt;

&lt;p&gt;Click on our build project and start the build.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h6LTqB5B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8l5hl0jes7f1l7ek3w7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h6LTqB5B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8l5hl0jes7f1l7ek3w7.png" alt="Image description" width="800" height="130"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What you will see on successful deployment?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NQEC78BH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g03k6gbtn87osun5pb9r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NQEC78BH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g03k6gbtn87osun5pb9r.png" alt="Image description" width="750" height="770"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Build output logs:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lFWkSamY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vb9wvyvq0q01gswxtney.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lFWkSamY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vb9wvyvq0q01gswxtney.png" alt="Image description" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Build files uploaded to our S3 bucket:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JogdAJrS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9nkka5cmc3ozdclhed0k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JogdAJrS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9nkka5cmc3ozdclhed0k.png" alt="Image description" width="800" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lambda function is deployed and function code is updated successfully.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NfXagtE5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b042wapmngu91sdo6ncb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NfXagtE5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b042wapmngu91sdo6ncb.png" alt="Image description" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope this article will help you.&lt;/p&gt;

&lt;p&gt;Happy learning :)&lt;/p&gt;

</description>
      <category>lambda</category>
      <category>dotnet</category>
      <category>devops</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
