How to build, test and deploy lambda function to AWS?
Tags: serverless, aws, node
Published: 2019-10-28 10:55:32

<!-- wp:paragraph -->
<p>In this <a href="https://innomizetech.com/blog/serverless-fundamentals-and-how-to-build-test-and-deploy-lambda-function-to-aws-part-1">article</a>, I have introduced the basic fundamentals of <strong>Serverless</strong> and its uses cases. This article will help you understand how to build, test, and deploy Serverless functions to the public clouds. Currently, we have a couple of public cloud vendors that provide service to run serverless functions such as <strong>Amazon Web Service</strong>, <strong>Azure</strong>, <strong>IBM Bluemix</strong>. But in this article, we only focus on <strong><a href="https://aws.amazon.com/">AWS</a></strong> one of the largest public cloud providers on the market.</p>
<!-- /wp:paragraph -->

<!-- wp:more -->
<!--more-->
<!-- /wp:more -->

<!-- wp:heading {"level":3} -->
<h3 id="45e2">How to build, run and deploy?</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>When starting designing, and building serverless functions, you might wonder how to deploy your function into the cloud. With AWS, there are some ways we can deploy, test and invoke your function:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><strong>Using the AWS Console Management:&nbsp;</strong>we can create Lambda function, upload code, add triggers, and test your Lambda function manually. You might use this way when first.</li><li><strong>AWS CLI</strong>: you also can use&nbsp;<a rel="noreferrer noopener" href="https://docs.aws.amazon.com/cli/latest/reference/lambda/index.html" target="_blank">AWS Lambda CLI</a>&nbsp;to create, deploy, invoke, manage, monitor your Lambda function. You can use existing commands to deploy and test your Lambda function automatically without a manual process. But this isn’t good for production and large project.</li><li><strong>Using frameworks:&nbsp;</strong>deploy can be easier with S<strong>erverless</strong>,&nbsp;<strong>AWS SAM, AWS Amplify, Zappa, Bref (for PHP function), Claudia, etc.</strong></li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Time to Code</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Before getting started, you need an AWS account. If you do not already have an AWS account, you can utilize their&nbsp;<a href="https://portal.aws.amazon.com/billing/signup">free-tier accounts</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Sign in with an AWS IAM user. You should not use the root account unless access to the billing or operations requires root privileges. This user should have IAM permission to create and manage IAM users.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Step 1: Prepare credentials</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Create a&nbsp;<strong>Programmatic access</strong>&nbsp;IAM user and assign&nbsp;<strong>Administrator</strong>&nbsp;policy.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong><em>For your real project, you should restrict permissions using AWS managed or custom policy instead of Administrator privileges.</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Go to&nbsp;<strong>IAM</strong>&nbsp;and select the above user you just created and view details; then select the&nbsp;<strong>Security credentials</strong>&nbsp;tab and create an Access Key. Store and copy your access key and secret key for the next step.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong><em>DO NOT share your credentials with anyone you don’t trust.</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Step 2: Configure credentials</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Install the latest&nbsp;<strong>AWS CLI</strong>&nbsp;on your machine, you can follow&nbsp;<a href="https://docs.aws.amazon.com/cli/latest/userguide/install-linux-al2017.html">these instructions</a>.</li><li>Configure an AWS named profile using this command:&nbsp;<code>aws configure --profile slsDev</code>. Next, enter the Access Key ID and Secret Key (copied in step #1) and additional properties, then finish configuring the AWS credentials command.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Once you have finished configuring the AWS credentials, you can build, run locally, test and deploy your Lambda function using your desired Framework. For this article, I will detail two frameworks:&nbsp;<strong>Serverless</strong>&nbsp;and&nbsp;<strong>AWS SAM</strong>. We will use Node.js runtime, so be sure to have Node.js ≥ 8 installed on your machine.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Build, test and deploy a hello world Lambda function using Serverless framework</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Step 1: Install Serverless</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>npm install serverless -g
sls --version // print the installed verion i.e. 1.45.1</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>At this point, you can configure the serverless credentials, or if you have already done this from our previous steps, you can continue.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you experience issues installing serverless, you may refer to&nbsp;<a href="https://serverless.com/framework/docs/providers/aws/guide/installation">this guide</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Step 2: Create a hello world project</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The command below will create a Serverless project that contains a Hello World function with Node.js runtime.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sls create --template hello-world
code . // open VSCode editor</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>The following files will be created once the command has been successfully executed.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><strong>serverless.yml:</strong>&nbsp;all service configurations are managed in this file. For more details, see&nbsp;<a href="https://serverless.com/framework/docs/providers/aws/guide/services/">https://serverless.com/framework/docs/providers/aws/guide/services/</a></li><li><strong>handle.js:</strong>&nbsp;Lambda function handle to write your custom code</li></ul>
<!-- /wp:list -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://www.thecoraledge.com/storage/5/4.png" alt="Image Description Here"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Step 3: Test your function locally</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are several ways to test your function locally before deploying into AWS.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>a) Test Lambda function locally using Serverless CLI</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sls invoke local -f helloWorld</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>The results of this command are:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>{
   "statusCode": 200,
   "headers": {
       "Access-Control-Allow-Origin": "*"
   },
   "body": "{\"message\":\"Go Serverless v1.0! Your function executed successfully!\",\"input\":\"\"}"
}</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Refer to&nbsp;<a href="https://serverless.com/framework/docs/providers/google/cli-reference/invoke-local/">this guide</a>&nbsp;for more available options of the <strong>invoke</strong> command.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>b) Run API locally using serverless-offline</strong><br>The&nbsp;<a href="https://github.com/dherault/serverless-offline">serverless-offline</a>&nbsp;plugin emulates your AWS Lambda function and AWS API Gateway on your machine. It starts an HTTP server that handles the request’s lifecycle, like APIG, and invokes your handlers.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Install the&nbsp;<code>serverless-offline</code>&nbsp;plugin</li></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>npm install serverless-offline -D</code></pre>
<!-- /wp:code -->

<!-- wp:list -->
<ul><li>Update&nbsp;<strong>serverless.yml</strong>&nbsp;file: add the following lines to the bottom of the&nbsp;<strong>serverless.yml</strong>&nbsp;file:</li></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>plugins:
- serverless-offline</code></pre>
<!-- /wp:code -->

<!-- wp:list -->
<ul><li>Then, run the&nbsp;<strong>serverless offline</strong>&nbsp;command.</li></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sls offline</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>The results of the command are:</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://www.thecoraledge.com/storage/5/5.png" alt="Image Description Here"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>You can send a request to your API running at&nbsp;<a href="http://localhost:3000/hello-world">http://localhost:3000/hello-world</a>&nbsp;(port 3000 default port of the serverless-offline).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Step 4: Deploy your Lambda function to AWS</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To deploy your Lambda function to AWS, run the command</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sls deploy — state prod</code></pre>
<!-- /wp:code -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://www.thecoraledge.com/storage/5/6.png" alt="Image Description Here"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Using an option&nbsp;<code>state</code>, with the above command and serverless, you can deploy multiple states for the same service and deploy multiple environments of your Lambda function such as Dev/Staging/Prod environments.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The output of the deploy command will contain the list of&nbsp;<strong>endpoints</strong>&nbsp;of your API. Send a request to check the output.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Step 5: Remove your Lambda function</strong><br>If you don’t want to run your Lambda function anymore, you can remove it automatically out of AWS using this command&nbsp;<code>sls remove — state prod</code></p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://www.thecoraledge.com/storage/5/7.png" alt="Image Description Here"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><em>If you have deployed multiple states, you can remove all states by running the above command for each deployed state.</em></p>
<!-- /wp:paragraph -->

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

<!-- wp:heading {"level":3} -->
<h3>Build and deploy a hello world Lambda function using AWS SAM framework</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now that you understand how to build, deploy, invoke/test and remove your Lambda function using Serverless, we will explore doing the same process with AWS SAM -&nbsp;<strong>Serverless Application Model</strong>; an open-source framework used to build&nbsp;<a href="https://aws.amazon.com/serverless/">serverless applications</a>&nbsp;on AWS.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Step 1: Install AWS SAM CLI</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Follow&nbsp;<a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html">this article</a>&nbsp;to install the latest version of the AWS SAM CLI on your machine.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Step 2: Create a hello world project</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>SAM provides a command to initialize the SAM project from a built-in template or Cookiecutter project template on GitHub or the local system. To create a Hello World SAM project, run</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sam init -r nodejs10.x -d npm -n sam-hello-world</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>This command will create a SAM project with Nodejs.10x runtime, using NPM as dependencies management. The SAM hello world project is structured as seen below.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://www.thecoraledge.com/storage/5/8.png" alt="Image Description Here"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>SAM Hello World project structure</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As with&nbsp;<strong>Serverless</strong>, AWS SAM also uses a .yaml file for the service configurations. You can add or remove Lambda functions, as well as add your custom CloudFormation resources into this file.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The Lambda handler file for AWS SAM is&nbsp;<em>PROJECT_DIR/[function-name]/app.js</em>. The content of the handler file is completely the same as the&nbsp;<strong>Serverless</strong>&nbsp;handler file.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://www.thecoraledge.com/storage/5/9.png" alt="Image Description Here"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Step 3: Test your function locally</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Invoke Locally: You can invoke the AWS SAM Lambda function locally in the same way as the Serverless framework by using this command:</li></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>echo '{"message": "Hey, are you there?" }' | sam local invoke HelloWorldFunction</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>SAM will pull, build, and start a&nbsp;<em>Docker container</em>&nbsp;and then invoke your Lambda function. Below is an example of the results.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://www.thecoraledge.com/storage/5/10.png" alt="Image Description Here"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Start API:</strong>&nbsp;if you are building an API Gateway on AWS that will integrate with your Lambda function, run the command&nbsp;<code>sam local start-api</code>. This will start your API locally and you can send an HTTP request in the same way as the&nbsp;<code>serverless-offline</code>&nbsp;plugin.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://www.thecoraledge.com/storage/5/11.png" alt="Image Description Here"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Start Lambda locally: if you are building a Lambda that supports other triggers than HTTP, you can start and invoke it locally using:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sam local start-lambda</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Then, use AWS SDK to invoke the Lambda function locally as seen below.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://www.thecoraledge.com/storage/5/12.png" alt="Image Description Here"/><figcaption>Original Code from AWS</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Step 4: Deploy your Lambda function to AWS</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To deploy your Lambda function to AWS with SAM CLI, run the following two commands in order.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><strong>Build the SAM package:</strong>&nbsp;before building the package, if you don’t already have an S3 bucket, you will need to create one. To do this, access the AWS Console Management, select S3, and create a bucket in the region you will deploy your Lambda function. Once you have the bucket you can build the SAM package using the command:</li></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sam package --s3-bucket innomizetech-sam-sample-us-east-1 --profile slsDev --region us-east-1 --output-template-file template-export.yml</code></pre>
<!-- /wp:code -->

<!-- wp:list -->
<ul><li><strong>Deploy the SAM package:</strong>&nbsp;after building the package, you can deploy into AWS using the command:</li></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sam deploy --template-file template-export.yml --profile slsDev --region us-east-1 --stack-name sam-hello-world --capabilities
CAPABILITY_IAM</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Once the command is executed successfully, go to your AWS Console Management, select&nbsp;<strong>us-east-1</strong>&nbsp;region, and you will see a CloudFormation stack has been created and deployed successfully.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://www.thecoraledge.com/storage/5/13.png" alt="Image Description Here"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Lambda Function</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You will also see a Lambda function has been created.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://www.thecoraledge.com/storage/5/14.png" alt="Image Description Here"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>CloudFormation Stack</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Go to CloudFormation management, select the newly created stack, then select the Outputs tab. You will see the outputs that contain the URL of your API Gateway endpoint that allows you to send the request.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://www.thecoraledge.com/storage/5/15.png" alt="Image Description Here"/></figure>
<!-- /wp:image -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://www.thecoraledge.com/storage/5/16.png" alt="Image Description Here"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Step 5: Remove your Lambda function</strong><br>Currently, there is no SAM command available to remove your Lambda resource on AWS. However, you can use CloudFormation CLI to delete your stack. The result of the delete stack command is all associated resources created from the stack will be deleted as well.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>aws cloudformation delete-stack --profile slsDev --region us-east-1 --stack-name sam-hello-world</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3>Summary</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>We have covered the basic flow to build your serverless function, and I introduced two frameworks for building <strong>Serverless</strong> function. However, I recommend you also try other frameworks to determine which is appropriate for your project.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Should you run into issues while practicing the steps outlined in this article, I encourage you to reach out to me. You can get in touch with me through my Twitter handle&nbsp;<a href="https://twitter.com/hoangleitvn">@hoangleitvn</a></p>
<!-- /wp:paragraph -->

Visit our [blog](https://innomizetech.com/blog) for more interesting articles. If you have any questions or need help you can contact me via [Twitter](https://twitter.com/hoangleitvn).