<?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>Forem: Muhammad Hamza Hameed</title>
    <description>The latest articles on Forem by Muhammad Hamza Hameed (@hamzahameed).</description>
    <link>https://forem.com/hamzahameed</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%2F976245%2F694fb65a-873f-4ff4-8dd0-aa05bf5697fd.png</url>
      <title>Forem: Muhammad Hamza Hameed</title>
      <link>https://forem.com/hamzahameed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hamzahameed"/>
    <language>en</language>
    <item>
      <title>Go Workspaces (multi-module)</title>
      <dc:creator>Muhammad Hamza Hameed</dc:creator>
      <pubDate>Mon, 21 Nov 2022 07:48:39 +0000</pubDate>
      <link>https://forem.com/hamzahameed/go-workspaces-multi-module-5f8o</link>
      <guid>https://forem.com/hamzahameed/go-workspaces-multi-module-5f8o</guid>
      <description>&lt;h2&gt;
  
  
  Workspace Deserves a Spotlight
&lt;/h2&gt;

&lt;p&gt;A new feature in Go 1.18 &lt;code&gt;go workspace mode&lt;/code&gt; which makes it simple to work with multiple modules. With multi-module workspaces, you can tell the Go command that you’re writing code in multiple modules at the same time and easily build and run code in those modules.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Workspace?
&lt;/h2&gt;

&lt;p&gt;In short, the workspace makes the multiple-module dependency and switch easier in local development.&lt;/p&gt;

&lt;p&gt;You must have encountered such an &lt;strong&gt;issue&lt;/strong&gt;: The multiple modules in the repo reference each other directly. But your local changes to a module &lt;code&gt;A&lt;/code&gt; fail to reflect in the module &lt;code&gt;B&lt;/code&gt; that depends on &lt;code&gt;A&lt;/code&gt;. What we take to overcome this is&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Push the modified code of module &lt;code&gt;A&lt;/code&gt; to GitHub, and pull the update in module &lt;code&gt;B&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Replace the module &lt;code&gt;B&lt;/code&gt; go.mod file’s dependency on &lt;code&gt;A&lt;/code&gt; to a local absolute path.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   replace github.com/hamza/a v0.1 =&amp;gt; /Users/hamza/workspace/repo/module-a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both solutions have their own cons, which bring us pain in developing in multi-module mode somehow.&lt;/p&gt;

&lt;p&gt;The first method is not designed in the way you like it if you try to avoid releasing new versions before more tests. Besides, after you publish it on Github, more releases and more versions are required for fixing the bug once they occur.&lt;/p&gt;

&lt;p&gt;For the second method, you should always remind yourself to revert the changes in the local &lt;code&gt;go.mod&lt;/code&gt; file before submitting; otherwise, you might drag your co-developers into trouble.&lt;/p&gt;

&lt;p&gt;In light of this, Go released the &lt;code&gt;go worspace mode&lt;/code&gt; in its version &lt;code&gt;1.18&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set Up Workspace
&lt;/h2&gt;

&lt;p&gt;Let’s test whether the workspace can ease the above-mentioned issues. Make sure that you’ve installed &lt;code&gt;Go&lt;/code&gt; at &lt;code&gt;Go 1.18&lt;/code&gt; or later using the links at &lt;a href="https://go.dev/dl/" rel="noopener noreferrer"&gt;go.dev/dl.&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a module for your code
&lt;/h3&gt;

&lt;p&gt;To begin, create a module for the code you’ll write.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open a command prompt and create a directory for your code called workspace.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;   $&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;workspace
&lt;span class="gp"&gt;   $&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;workspace
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Initialize the module&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this example, I will create a new module &lt;code&gt;hello&lt;/code&gt; that will depend on the &lt;code&gt;golang.org/x/example&lt;/code&gt; module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;   $&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;hello
&lt;span class="gp"&gt;   $&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;hello
&lt;span class="gp"&gt;   $&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;go mod init example.com/hello
&lt;span class="go"&gt;   go: creating new go.mod: module example.com/hello
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add a dependency on the golang.org/x/example module by using go get.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;   $&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;go get golang.org/x/example
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create hello.go in the hello directory with the following contents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;   &lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

   &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"golang.org/x/example/stringutil"&lt;/span&gt;
   &lt;span class="p"&gt;)&lt;/span&gt;

   &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stringutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, run the hello program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;   &lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;hello&lt;/span&gt;
   &lt;span class="n"&gt;olleH&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create the workspace
&lt;/h3&gt;

&lt;p&gt;In this step, we’ll create a &lt;code&gt;go.work&lt;/code&gt; file to specify a workspace with the module.&lt;/p&gt;

&lt;h4&gt;
  
  
  Initialize the workspace
&lt;/h4&gt;

&lt;p&gt;In the workspace directory, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;go work init ./hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The go work init command tells go to create a &lt;code&gt;go.work&lt;/code&gt; file for a workspace containing the modules in the &lt;code&gt;./hello&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;The go command produces a go.work file that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go 1.18

use ./hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;go.work&lt;/code&gt; file has similar syntax to &lt;code&gt;go.mod&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;go&lt;/code&gt; directive tells &lt;code&gt;Go&lt;/code&gt; which version of &lt;code&gt;Go&lt;/code&gt; the file should be interpreted with. It’s similar to the &lt;code&gt;go&lt;/code&gt; directive in the &lt;code&gt;go.mod&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;use&lt;/code&gt; directive tells &lt;code&gt;Go&lt;/code&gt; that the module in the &lt;code&gt;hello&lt;/code&gt; directory should be main modules when doing a build.&lt;/p&gt;

&lt;p&gt;So in any subdirectory of workspace the module will be active.&lt;/p&gt;

&lt;h4&gt;
  
  
  Run the program in the workspace directory
&lt;/h4&gt;

&lt;p&gt;In the &lt;code&gt;workspace&lt;/code&gt; directory, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;go run example.com/hello
&lt;span class="go"&gt;olleH
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Go command includes all the modules in the workspace as main modules. This allows us to refer to a package in the module, even outside the module. Running the &lt;code&gt;go run&lt;/code&gt; command outside the module or the workspace would result in an error because the go command wouldn’t know which modules to use.&lt;/p&gt;

&lt;p&gt;Next, we’ll add a local copy of the &lt;code&gt;golang.org/x/example&lt;/code&gt; module to the workspace. We’ll then add a new function to the &lt;code&gt;stringutil&lt;/code&gt; package that we can use instead of Reverse.&lt;/p&gt;

&lt;h3&gt;
  
  
  Download and modify the golang.org/x/example module
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Clone the repository&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;From the &lt;code&gt;workspace&lt;/code&gt; directory, run the git command to clone the repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;   $&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;git clone https://go.googlesource.com/example
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Add the module to the workspace
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;   $&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;go work use ./example
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;go work use&lt;/code&gt; command adds a new module to the &lt;code&gt;go.work&lt;/code&gt; file. It will now look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   go 1.18

   use (
     ./hello
     ./example
   )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The module now includes both the &lt;code&gt;example.com/hello&lt;/code&gt; module and the &lt;code&gt;golang.org/x/example&lt;/code&gt; module.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the new function.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We’ll add a new function to uppercase a string to the &lt;code&gt;golang.org/x/example/stringutil&lt;/code&gt; package.&lt;/p&gt;

&lt;p&gt;Create a new file named &lt;code&gt;toupper.go&lt;/code&gt; in the &lt;code&gt;workspace/example/stringutil&lt;/code&gt; directory containing the following contents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;   &lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;stringutil&lt;/span&gt;

   &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"strings"&lt;/span&gt;

   &lt;span class="c"&gt;// ToUpper uppercases argument string.&lt;/span&gt;
   &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;ToUpper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ToUpper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Run the code in the workspace
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqbjmx4cdd6dnjlm2h6fm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqbjmx4cdd6dnjlm2h6fm.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://go.dev/doc/tutorial/workspaces" rel="noopener noreferrer"&gt;Go Workspaces&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://go.dev/ref/mod#workspaces" rel="noopener noreferrer"&gt;Go Modules&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Get the full source code from &lt;a href="https://github.com/datumbrain/kss-go-workspace" rel="noopener noreferrer"&gt;this github repository&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>go</category>
      <category>workspace</category>
      <category>multimodule</category>
      <category>programming</category>
    </item>
    <item>
      <title>GoCD - Introduction (A tool that use for continuous Integration and continuous delivery CI/CD)</title>
      <dc:creator>Muhammad Hamza Hameed</dc:creator>
      <pubDate>Mon, 21 Nov 2022 07:40:34 +0000</pubDate>
      <link>https://forem.com/hamzahameed/gocd-introduction-a-tool-that-use-for-continuous-integration-and-continuous-delivery-cicd-7p4</link>
      <guid>https://forem.com/hamzahameed/gocd-introduction-a-tool-that-use-for-continuous-integration-and-continuous-delivery-cicd-7p4</guid>
      <description>&lt;p&gt;&lt;code&gt;GoCD&lt;/code&gt; is an &lt;strong&gt;open-source tool&lt;/strong&gt; which is used in software development to help teams and organizations automate the continuous delivery (CD) of software. It supports automating the entire build-test-release process from code check-in to deployment.&lt;/p&gt;

&lt;h2&gt;
  
  
  GoCD system requirements
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Client (browser) requirements
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;GoCD&lt;/code&gt; supports the latest versions of the following browsers&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Google Chrome&lt;/li&gt;
&lt;li&gt;Mozilla Firefox&lt;/li&gt;
&lt;li&gt;Microsoft Edge&lt;/li&gt;
&lt;li&gt;Apple Safari&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  GoCD server requirements
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Hardware
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RAM&lt;/strong&gt; - minimum 1GB, 2GB recommended&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CPU&lt;/strong&gt; - minimum 2 cores, 2GHz&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Disk&lt;/strong&gt; - minimum 1GB free space&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Supported operating systems
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Windows&lt;/strong&gt; - Windows Server 2012, Windows Server 2016, Windows 8 and Windows 10&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mac OSX&lt;/strong&gt; - 10.7 (Lion) and above with Intel processor&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debian&lt;/strong&gt; - Debian 8.0 and above&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CentOS/RedHat&lt;/strong&gt; - CentOS/RedHat version 6.0 and above&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ubuntu&lt;/strong&gt; - Ubuntu 14 and above&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Alpine Linux&lt;/strong&gt; - Alpine Linux 3.6 and above&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Installation
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;GoCD&lt;/code&gt; consists of two installable components. The &lt;strong&gt;&lt;code&gt;GoCD&lt;/code&gt; Server&lt;/strong&gt; and one or more &lt;strong&gt;&lt;code&gt;GoCD&lt;/code&gt; Agents&lt;/strong&gt;. They have a one-to-many relationship in that many &lt;code&gt;GoCD&lt;/code&gt; agents can connect to one &lt;code&gt;GoCD&lt;/code&gt; Server. To do any real work, you need at least one agent, since agents are the real builders or work executors in the system.&lt;/p&gt;

&lt;p&gt;Make sure your system has installed &lt;strong&gt;Java / JRE version 13&lt;/strong&gt; (for releases 21.1.0 and higher)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Follow &lt;a href="https://docs.gocd.org/current/installation/installing_go_server.html"&gt;&lt;code&gt;GoCD&lt;/code&gt; Server installation instructions&lt;/a&gt; to install server&lt;/li&gt;
&lt;li&gt;Follow &lt;a href="https://docs.gocd.org/current/installation/installing_go_agent.html"&gt;&lt;code&gt;GoCD&lt;/code&gt; Agent installation instructions&lt;/a&gt; to install agent&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Follow the the given links instructions, select your operating system and install &lt;code&gt;GoCD&lt;/code&gt; Server and (at least one) Agent and run server and angent according to the instructions.&lt;/p&gt;

&lt;p&gt;Whatever operating systems you install the &lt;code&gt;GoCD&lt;/code&gt; server and (at least one) &lt;code&gt;GoCD&lt;/code&gt; agent on, the default ports used by the server are &lt;code&gt;8153&lt;/code&gt; (HTTP port) and &lt;code&gt;8154&lt;/code&gt; (HTTPS port). So, after installation you should be able to access either &lt;code&gt;http://localhost:8153&lt;/code&gt; or &lt;code&gt;http://your-server-installation-hostname:8153&lt;/code&gt; and you should see a screen such as,&lt;/p&gt;

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

&lt;p&gt;Once the agent is started, switching to the agents tab by clicking on the &lt;code&gt;Agents&lt;/code&gt; link in the header should take you to a screen where the agent shows up and is idle. Like this:&lt;/p&gt;

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

&lt;p&gt;An agent or agents can be installed on any node, and not necessarily the node that the &lt;code&gt;GoCD&lt;/code&gt; Server is installed on. The only requirements are that ports &lt;code&gt;8153&lt;/code&gt; and &lt;code&gt;8154&lt;/code&gt; of the &lt;code&gt;GoCD&lt;/code&gt; Server are accessible from the node that the agents are installed on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Concepts in GoCD
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Server and agents
&lt;/h3&gt;

&lt;p&gt;In the &lt;code&gt;GoCD&lt;/code&gt; ecosystem, the server is the one that controls everything. It provides the user interface to users of the system and provides work for the agents to do. The agents are the ones that do any work (run commands, do deployments, etc) that is configured by the users or administrators of the system.&lt;/p&gt;

&lt;p&gt;The server does not do any user-specified &lt;code&gt;work&lt;/code&gt; on its own. It will not run any commands or do deployments. That is the reason you need a &lt;code&gt;GoCD&lt;/code&gt; Server and at least one &lt;code&gt;GoCD&lt;/code&gt; Agent installed before you proceed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pipelines and materials
&lt;/h3&gt;

&lt;p&gt;A pipeline, in &lt;code&gt;GoCD&lt;/code&gt;, is a representation of workflow or a part of a workflow. For instance, if you are trying to automatically run tests, build an installer and then deploy an application to a test environment, then those steps can be modeled as a pipeline. &lt;code&gt;GoCD&lt;/code&gt; provides different modeling constructs within a pipeline, such as stages, jobs and tasks. We will see these in more detail soon. For the purpose of this part of the guide, you need to know that a pipeline can be configured to run a command or set of commands.&lt;/p&gt;

&lt;p&gt;Another equally important concept is that of a material. A material is a cause for a pipeline to &lt;code&gt;trigger&lt;/code&gt; or to start doing what it is configured to do. Typically, a material is a source control repository (like Git, Subversion, etc) and any new commit to the repository is a cause for the pipeline to trigger. A pipeline needs to have at least one material and can have as many materials of different kinds as you want.&lt;/p&gt;

&lt;p&gt;The concept of a pipeline is extremely central to Continuous Delivery. Together with the concepts of stages, jobs and tasks, &lt;code&gt;GoCD&lt;/code&gt; provides important modeling blocks which allow you to build up very complex workflows, and get feedback quicker.&lt;/p&gt;

&lt;p&gt;Now, back at the &lt;code&gt;Add pipeline&lt;/code&gt; screen, you can choose your own material here, you can change &lt;code&gt;Material Type&lt;/code&gt; to &lt;code&gt;Git&lt;/code&gt; and provide the URL for your git repository in the &lt;code&gt;URL&lt;/code&gt; textbox. If you now click on the &lt;code&gt;Test Connection&lt;/code&gt; button, provide github username and github personal access token. it should tell you everything is OK.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hnu139gs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x28moof4luqbv5i9t1lq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hnu139gs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x28moof4luqbv5i9t1lq.png" alt="Image description" width="880" height="522"&gt;&lt;/a&gt;&lt;br&gt;
Scroll down and let's provide the pipeline a name, without spaces, and ignore the &lt;code&gt;Pipeline Group&lt;/code&gt; field for now.&lt;/p&gt;

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

&lt;p&gt;This step assumes that you have git installed on the &lt;code&gt;GoCD&lt;/code&gt; Server and Agent. Like git, any other commands you need for running your scripts need to be installed on the &lt;code&gt;GoCD&lt;/code&gt; Agent nodes.&lt;br&gt;
If everything went well, press &lt;code&gt;Next&lt;/code&gt; to be taken to step 3, which deals with stages, jobs and tasks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage details, jobs, and tasks
&lt;/h3&gt;

&lt;p&gt;let's provide the stage a name, without spaces. Scroll down you will see the job and task field. Provide the job a name, without spaces and define your task bellow in the task field.&lt;/p&gt;

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

&lt;p&gt;If all went well, you just created your first pipeline! Leaving you in a screen similar to the one shown below. If you give it a minute, you'll see your pipeline building (yellow) and then finish successfully (green)&lt;/p&gt;

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

&lt;p&gt;The pipeline is building&lt;/p&gt;

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

&lt;p&gt;The pipeline is finished successfully.&lt;/p&gt;

&lt;p&gt;Clicking on the bright green bar will show you information about the stage&lt;/p&gt;

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

&lt;p&gt;and then clicking on the job (terminal) icon will take you to the actual task and show you what it did&lt;/p&gt;

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

&lt;p&gt;Scrolling up a bit, you can see it print out the environment variables for the task and the details of the agent this task ran on&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Pipeline dependency material
&lt;/h3&gt;

&lt;p&gt;Instead of using a source code repository on GitHub as a material, it is possible to use a stage of any pipeline as a material for another pipeline. This seemingly simple feature provides a lot of power. This allows pipelines to be chained together, allowing very complex workflows to be modeled well and is a basis for the more advanced features in &lt;code&gt;GoCD&lt;/code&gt;, such as Value Stream Map (VSM) and fan-in/fan-out.&lt;/p&gt;

&lt;p&gt;A pipeline dependency material links pipelines together. The pipeline which has the material which points to another pipeline is called the &lt;code&gt;Downstream Pipeline&lt;/code&gt;. The actual pipeline which is the dependency material is called the &lt;code&gt;Upstream Pipeline&lt;/code&gt;. Even though it is called a &lt;code&gt;Pipeline Dependency&lt;/code&gt; the real dependency is to a stage in an upstream pipeline.&lt;/p&gt;

&lt;p&gt;As soon as that stage completes successfully, the first stage of each of the configured downstream pipelines triggers. If the stage does not complete successfully, its configured downstream pipelines do not trigger.&lt;/p&gt;

&lt;p&gt;Let's see how we can get a pipeline dependency to work. Clicking on &lt;code&gt;Admin&lt;/code&gt; and then &lt;code&gt;Pipelines&lt;/code&gt; takes you to the &lt;code&gt;Pipeline Configuration&lt;/code&gt; page which lists all the pipelines in the system. It looks like this&lt;/p&gt;

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

&lt;p&gt;we can mange previous piplines or we can add more pipelines here. Lets make another pipeline.&lt;/p&gt;

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

&lt;p&gt;scroll down and chose some task in &lt;code&gt;job and task field&lt;/code&gt; and click on &lt;code&gt;Run&lt;/code&gt;&lt;/p&gt;

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

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

&lt;p&gt;This is how it will look on the Pipelines list (dashboard) when finished. That doesn't look very different. But, this allows for some powerful features such as fan-in, fan-out and the ValueStream Map (VSM) Click on the label of the &lt;code&gt;Demo_PipeLine&lt;/code&gt; (the part highlightedin the image above) &lt;code&gt;VSM&lt;/code&gt; for a sneak peek at a small Value Stream Map.&lt;/p&gt;

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

&lt;p&gt;You now know how to chain pipelines together. Let's now learn about artifacts, which are very useful when used with pipeline dependencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Artifacts
&lt;/h3&gt;

&lt;p&gt;An artifact in &lt;code&gt;GoCD&lt;/code&gt; is a file or directory which is most often produced during the run of a pipeline. Every job in a pipeline can be configured to publish its own set of artifacts and &lt;code&gt;GoCD&lt;/code&gt; will ensure that these artifacts are moved from the Agent where it is created, to the Server and stored there, so that it can be retrieved at any time.&lt;/p&gt;

&lt;p&gt;Typically, artifacts are created during a job run, by one of the tasks. Some examples of artifacts are: Test reports, coverage reports, installers, documentation, meta information about the build process itself and anything else that needs to be stored after a pipeline has finished.&lt;/p&gt;

&lt;p&gt;These artifacts, published by a job, can be fetched and used by &lt;em&gt;any&lt;/em&gt; downstream pipeline or &lt;em&gt;any&lt;/em&gt; stage after the one that produced the artifact in the same pipeline, using a special task called a &lt;code&gt;Fetch Artifact&lt;/code&gt; task. As jobs are independent of each other. So, a job in the same stage as another job that produced an artifact cannot use that artifact. It needs to be used in a stage after that one.&lt;/p&gt;

&lt;p&gt;Let's see how to publish an artifact. In the upstream pipeline, &lt;code&gt;Demo_Pipeline&lt;/code&gt;, let's first declare an artifact. The build script used throughout this guide creates a file called &lt;code&gt;my-artifact.html&lt;/code&gt; after it finishes. We can use that as the artifact for this example.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;code&gt;Admin&lt;/code&gt; -&amp;gt; &lt;code&gt;Pipelines&lt;/code&gt; and select the stage, &lt;code&gt;demo_stage&lt;/code&gt; and then the job, &lt;code&gt;build&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Click on the &lt;code&gt;Artifacts&lt;/code&gt; tab in the job.&lt;/li&gt;
&lt;li&gt;Enter &lt;code&gt;my-artifact.html&lt;/code&gt; as the artifact source.&lt;/li&gt;
&lt;li&gt;Leave the &lt;code&gt;Destination&lt;/code&gt; box empty for now.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;Since we have chosen to leave the &lt;code&gt;Destination&lt;/code&gt; empty, and that means that the artifact will be accessible at the root, by its own name. We have also chosen the type as &lt;code&gt;Build Artifact&lt;/code&gt;, to signify that it is not a &lt;code&gt;Test Artifact&lt;/code&gt;. Marking it as a &lt;code&gt;Test Artifact&lt;/code&gt; means that &lt;code&gt;GoCD&lt;/code&gt; will try and parse the file as a test report and if it can, it will use it in the test aggregation reporting it does. Typically, you'll want to use &lt;code&gt;Build Artifact&lt;/code&gt; for installers, binaries, documentation, etc.&lt;/p&gt;

&lt;p&gt;After saving this change, retrigger &lt;code&gt;Demo_Pipeline&lt;/code&gt; by going to the pipeline dashboard and clicking on the play button against it. Once it is finished, going to the &lt;code&gt;Artifacts&lt;/code&gt; tab of the pipeline run shows you the artifact for that run. Every run of that pipeline, from now on, will have that artifact.&lt;/p&gt;

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

&lt;p&gt;Clicking on it will show you its contents&lt;/p&gt;

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

&lt;h4&gt;
  
  
  Fetching and using artifacts
&lt;/h4&gt;

&lt;p&gt;We can now use this artifact in any downstream pipeline, or any subsequent stage of the same pipeline. Let's fetch this artifact in the pipeline &lt;code&gt;My2ndPipeline&lt;/code&gt; and display it as a part of the output. To do this, we go to the task configuration section of the &lt;code&gt;echo&lt;/code&gt; job inside the &lt;code&gt;Demo_Stage&lt;/code&gt; stage of &lt;code&gt;My2ndPipeLine&lt;/code&gt; pipeline, and add a &lt;code&gt;Fetch Artifact&lt;/code&gt; task.&lt;/p&gt;

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

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

&lt;p&gt;Once you provide all the details and click save, you can move the &lt;code&gt;Fetch Artifact&lt;/code&gt; task above, so that it is done first. Then, for this demonstration, let us display the fetched file in a &lt;code&gt;Custom Command&lt;/code&gt; task.&lt;/p&gt;

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

&lt;p&gt;After it is moved up, you can edit the &lt;code&gt;Custom Command&lt;/code&gt; task to output the contents of the file (for instance). If you are running this on Windows, use &lt;code&gt;type&lt;/code&gt; instead of &lt;code&gt;cat&lt;/code&gt;&lt;/p&gt;

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

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

&lt;p&gt;Running &lt;code&gt;My2ndPipeLine&lt;/code&gt; now will show you the contents of the file created in &lt;code&gt;Demo_Pipeline&lt;/code&gt; and fetched as an artifact. The importance of fetching an artifact this way is that &lt;code&gt;GoCD&lt;/code&gt; will ensure that the correct version of the artifact will be fetched and provided to the job in &lt;code&gt;My2ndPipeLine&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If more instances of &lt;code&gt;Demo_Pipeline&lt;/code&gt; ran (because it is fast) while &lt;code&gt;My2ndPipeLine&lt;/code&gt; has run fewer times, &lt;code&gt;GoCD&lt;/code&gt; will ensure that every time &lt;code&gt;My2ndPipeLine&lt;/code&gt; runs, the correct (and compatible) version of the artifact is fetched and used. When you now check the output of the pipeline, you should see something like this&lt;/p&gt;

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

&lt;p&gt;Hope this helped you.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.gocd.org/current/"&gt;&lt;code&gt;GoCD&lt;/code&gt; - An Introduction&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.gocd.org/current/installation/"&gt;Installation &lt;code&gt;GoCD&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.gocd.org/current/introduction/concepts_in_go.html"&gt;Concepts in &lt;code&gt;GoCD&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.gocd.org/current/navigation/value_stream_map.html"&gt;Value Stream Map&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Advanced Usages :

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.gocd.org/current/advanced_usage/pipelines_as_code.html"&gt;Pipelines as code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.gocd.org/current/advanced_usage/agent_auto_register.html"&gt;Auto Register a Remote Agent&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.gocd.org/current/advanced_usage/admin_install_multiple_agents.html"&gt;Multiple Agents on One Machine&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.gocd.org/current/advanced_usage/compare_pipelines.html"&gt;Compare Builds&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.gocd.org/current/advanced_usage/one_click_backup.html"&gt;Backup &lt;code&gt;GoCD&lt;/code&gt; Server&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.gocd.org/current/advanced_usage/cron_backup.html"&gt;Timer Based &lt;code&gt;GoCD&lt;/code&gt; Server Backup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.gocd.org/current/advanced_usage/business_continuity.html"&gt;&lt;code&gt;GoCD&lt;/code&gt; Business Continuity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.gocd.org/current/integration/"&gt;Integrating &lt;code&gt;GoCD&lt;/code&gt; With Other Tools&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.gocd.org/current/gocd_on_kubernetes/introduction.html"&gt;&lt;code&gt;GoCD&lt;/code&gt; on Kubernetes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.gocd.org/current/extension_points/"&gt;Plugins in &lt;code&gt;GoCD&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>go</category>
      <category>gocd</category>
      <category>devops</category>
    </item>
    <item>
      <title>Serverless application in golang with AWS (Lambda Service)</title>
      <dc:creator>Muhammad Hamza Hameed</dc:creator>
      <pubDate>Mon, 21 Nov 2022 07:30:26 +0000</pubDate>
      <link>https://forem.com/hamzahameed/serverless-application-in-golang-with-aws-lambda-service-5g8m</link>
      <guid>https://forem.com/hamzahameed/serverless-application-in-golang-with-aws-lambda-service-5g8m</guid>
      <description>&lt;h2&gt;
  
  
  What is Serverless Architecture?
&lt;/h2&gt;

&lt;p&gt;Serverless architecture is a way to build and run applications and services without having to manage infrastructure. Your application still runs on servers, but all the server management is done by AWS.&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS Lambda
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;AWS Lambda&lt;/code&gt; is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers. You can trigger Lambda from over 200 AWS services and software as a service (SaaS) applications and only pay for what you use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of Using AWS Lambda
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Run code without provisioning or managing infrastructure. Simply write and upload code as a &lt;code&gt;.zip&lt;/code&gt; file or container image.&lt;/li&gt;
&lt;li&gt;Automatically respond to code execution requests at any scale, from a dozen events per day to hundreds of thousands per second.&lt;/li&gt;
&lt;li&gt;Save costs by paying only for computing time you use — by per millisecond — instead of provisioning infrastructure upfront for peak capacity.&lt;/li&gt;
&lt;li&gt;Optimize code execution time and performance with the right function memory size. Respond to high demand in double-digit milliseconds with Provisioned Concurrency.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Serverless Application in Go with AWS
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7r5o0icie6avcmtngil5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7r5o0icie6avcmtngil5.png" alt="Image description" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You’ll need an AWS account for this. If you don’t yet have one, &lt;a href="https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&amp;amp;all-free-tier.sort-order=asc&amp;amp;awsf.Free%20Tier%20Types=*all&amp;amp;awsf.Free%20Tier%20Categories=*all" rel="noopener noreferrer"&gt;sign up for a free account here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;For building and deploying your functions, you’ll be using the &lt;code&gt;Serverless Framework&lt;/code&gt;, which is the most widely used tool for the job. Assuming you have a recent version of &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;Node.js&lt;/a&gt; installed, you can install the &lt;code&gt;Serverless CLI&lt;/code&gt; with the following npm command:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;  $&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; serverless
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you have the Serverless CLI installed, you must configure it to use the AWS access keys of your account:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;  $&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;serverless config credentials &lt;span class="nt"&gt;--provider&lt;/span&gt; aws &lt;span class="nt"&gt;--key&lt;/span&gt; &amp;lt;access key ID&amp;gt; &lt;span class="nt"&gt;--secret&lt;/span&gt; &amp;lt;secret access key&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can get the access key and secret key from the &lt;code&gt;My Security Credentials&lt;/code&gt; option. Use &lt;code&gt;Create New Access Key&lt;/code&gt; if you do not have one already.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feezfmrfnwe4cx40089iy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feezfmrfnwe4cx40089iy.png" alt="Image description" width="800" height="537"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you don’t have Go installed yet, you can either &lt;a href="https://golang.org/dl/" rel="noopener noreferrer"&gt;download an installer&lt;/a&gt; from the official website or use your favorite package manager to install it&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Lambda-Time Function
&lt;/h3&gt;

&lt;p&gt;Now that you have everything you need, let’s create a new project using Go modules for your function. For example, let’s name the module &lt;code&gt;lambda-time&lt;/code&gt;. In a new, empty directory, initialize the Go module, and install the &lt;a href="https://github.com/aws/aws-lambda-go" rel="noopener noreferrer"&gt;AWS Lambda for Go&lt;/a&gt; library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;go mod init lambda-time
&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;go get github.com/aws/aws-lambda-go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this, you can proceed to create a &lt;code&gt;main.go&lt;/code&gt; file that implements your handler function and starts the process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"context"&lt;/span&gt;
    &lt;span class="s"&gt;"encoding/json"&lt;/span&gt;
    &lt;span class="s"&gt;"log"&lt;/span&gt;
    &lt;span class="s"&gt;"time"&lt;/span&gt;
    &lt;span class="s"&gt;"github.com/aws/aws-lambda-go/events"&lt;/span&gt;
    &lt;span class="s"&gt;"github.com/aws/aws-lambda-go/lambda"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;lambda&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handleRequest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;handleRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;APIGatewayProxyRequest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;APIGatewayProxyResponse&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;UTC&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTC&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;APIGatewayProxyResponse&lt;/span&gt;&lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;APIGatewayProxyResponse&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;StatusCode&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;200&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;UTC&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Time&lt;/span&gt; &lt;span class="s"&gt;`json:"utc"`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This previous code can be broken into a few simple steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define a response struct that supports JSON serialization and defines the HTTP response body of a successful invocation of your AWS Lambda function.&lt;/li&gt;
&lt;li&gt;Create a request handler function, which creates a response struct containing the current time in UTC and then proceeds to serialize it as JSON. In case the serialization fails, you return the error; if everything goes well, you respond with your serialized JSON as the response body and a status code of 200.&lt;/li&gt;
&lt;li&gt;Register your handler function in the main function using the &lt;code&gt;AWS Lambda for Go library&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Handler Function
&lt;/h3&gt;

&lt;p&gt;It’s worth taking some time to understand how the handler function works. While there are multiple &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/golang-handler.html" rel="noopener noreferrer"&gt;valid handler signatures&lt;/a&gt;, the one you used is the complete one. The context argument provides information on the invoked function, its environment and the deadline of the invocation. Returning an error value from the handler function signals that the invocation failed and automatically logs the value of the error.&lt;/p&gt;

&lt;p&gt;That leaves the request and response structs in your handler function signature. Lambda functions are invoked either by AWS services or by using an &lt;code&gt;AWS SDK&lt;/code&gt; (e.g., from another Lambda function). Data passed in and out of a Lambda function is in JSON format. In your case, the &lt;code&gt;AWS Lambda for Go&lt;/code&gt; library automatically handles the serialization and deserialization between JSON and Go values.&lt;/p&gt;

&lt;p&gt;When calling Lambda functions using the AWS SDK, the structure of the input and output JSON data is up to the developer. For AWS Lambda functions invoked by AWS services, the data structure depends on the invoking service. &lt;code&gt;Amazon API Gateway&lt;/code&gt; is the service that triggers Lambda functions in response to HTTP calls. For API Gateway, this means the request is always of type &lt;code&gt;events.APIGatewayProxyRequest&lt;/code&gt; and the response will always be of type &lt;code&gt;events.APIGatewayProxyResponse&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;AWS Lambda for Go&lt;/code&gt; library contains the data definitions for each AWS service that can invoke Lambda functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Integration with Golang Routing Libraries
&lt;/h3&gt;

&lt;p&gt;If you are using some package for routing, there is a package available that may be used to connect their routing library with lambda.&lt;br&gt;
&lt;code&gt;github.com/awslabs/aws-lambda-go-api-proxy&lt;/code&gt; It currently supports the following routing libraries.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;chi&lt;/li&gt;
&lt;li&gt;gin&lt;/li&gt;
&lt;li&gt;gorillamux&lt;/li&gt;
&lt;li&gt;fiber&lt;/li&gt;
&lt;li&gt;echo&lt;/li&gt;
&lt;li&gt;iris&lt;/li&gt;
&lt;li&gt;negroni&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets see the &lt;code&gt;chi&lt;/code&gt; example for routing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"context"&lt;/span&gt;
    &lt;span class="s"&gt;"encoding/json"&lt;/span&gt;
    &lt;span class="s"&gt;"log"&lt;/span&gt;
    &lt;span class="s"&gt;"time"&lt;/span&gt;

    &lt;span class="n"&gt;chiadapter&lt;/span&gt; &lt;span class="s"&gt;"github.com/awslabs/aws-lambda-go-api-proxy/chi"&lt;/span&gt;
    &lt;span class="s"&gt;"github.com/aws/aws-lambda-go/events"&lt;/span&gt;
    &lt;span class="s"&gt;"github.com/aws/aws-lambda-go/lambda"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;lambda&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handleRequests&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;handleRequests&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;APIGatewayProxyRequest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;APIGatewayProxyResponse&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;router&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;chi&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewRouter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;UTC&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTC&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusInternalServerError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"application/json; charset=utf-8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;chiadapter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ProxyWithContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;UTC&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Time&lt;/span&gt; &lt;span class="s"&gt;`json:"utc"`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Deployment
&lt;/h3&gt;

&lt;p&gt;Your function is now ready and you can proceed by deploying it with the Serverless Framework. Your application deployed using the Serverless framework based on the &lt;code&gt;serverless.yml&lt;/code&gt; configuration file.&lt;/p&gt;

&lt;p&gt;If you are not familiar with the &lt;code&gt;.yml&lt;/code&gt; syntax, you can read this &lt;a href="https://bref.sh/docs/environment/serverless-yml.html" rel="noopener noreferrer"&gt;serverless.yml guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We must first create a &lt;code&gt;serverless.yml&lt;/code&gt; file that defines what we are deploying.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;lambda-time&lt;/span&gt;
&lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;aws&lt;/span&gt;
  &lt;span class="na"&gt;runtime&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;go1.x&lt;/span&gt;
&lt;span class="na"&gt;package&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;exclude&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./**&lt;/span&gt;
  &lt;span class="na"&gt;include&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./bin/**&lt;/span&gt;
&lt;span class="na"&gt;functions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;lambda-time&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;bin/lambda-time&lt;/span&gt;
    &lt;span class="na"&gt;events&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;http&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/&lt;/span&gt;
          &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;get&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you name both your service and the function &lt;code&gt;lambda-time&lt;/code&gt;, but the service could instead contain multiple functions with different names. You also need to configure your API Gateway by specifying that the function responds to HTTP events of a particular HTTP method and at a given request path.&lt;/p&gt;

&lt;p&gt;Next up, build the code as an x86-64 Linux executable, and deploy it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;GOOS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;linux &lt;span class="nv"&gt;GOARCH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;amd64  go build &lt;span class="nt"&gt;-o&lt;/span&gt; bin/lambda-time &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;serverless deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once finished, the command prints the URL for the endpoint.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F40ofk3pt2fxaf7u4gpvz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F40ofk3pt2fxaf7u4gpvz.png" alt="Image description" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open it, and make sure it responds with the current time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft1gwu1rrikg49d0gnzhz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft1gwu1rrikg49d0gnzhz.png" alt="Image description" width="800" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you can visit your aws account. You can see your application at &lt;code&gt;AWS services&lt;/code&gt; &amp;gt; &lt;code&gt;Lambda&lt;/code&gt; &amp;gt; &lt;code&gt;Applications&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feta60jthvwq7j53mpx4r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feta60jthvwq7j53mpx4r.png" alt="Image description" width="800" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcuxu9qfqic95d5qjcyto.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcuxu9qfqic95d5qjcyto.png" alt="Image description" width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In your application you can see your &lt;code&gt;ApiGatewayRestApi&lt;/code&gt; , &lt;code&gt;Lambda Function&lt;/code&gt; and &lt;code&gt;ServerlessDeploymentBucket&lt;/code&gt; where your api is deployed.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;ApiGatewayRestApi&lt;/code&gt; there are api paths &lt;code&gt;Method Execution&lt;/code&gt; where you can run Test.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcsjsaheb40rsc7ifbx2p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcsjsaheb40rsc7ifbx2p.png" alt="Image description" width="800" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Stage&lt;/code&gt; where you can create multiple stages, By Default we have a dev stage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzazldh6moer02kk1nvar.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzazldh6moer02kk1nvar.png" alt="Image description" width="800" height="491"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;Lambda Functions&lt;/code&gt; you can see your lambda functions and in your function there is &lt;code&gt;runtime settings&lt;/code&gt; where your handler file and runtime language already selected.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhs66upyuu712egq1sqbo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhs66upyuu712egq1sqbo.png" alt="Image description" width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F799b9e7mpp1tl3qaoy4h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F799b9e7mpp1tl3qaoy4h.png" alt="Image description" width="800" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffdgnry1vakf1g3z1v6zb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffdgnry1vakf1g3z1v6zb.png" alt="Image description" width="800" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's visit &lt;code&gt;Cloud Watch&lt;/code&gt; where you can see your logs:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8458jz1vb8byfa1odcn8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8458jz1vb8byfa1odcn8.png" alt="Image description" width="800" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/blogs/compute/announcing-go-support-for-aws-lambda/" rel="noopener noreferrer"&gt;Go Support for AWS Lambda&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://epsagon.com/development/getting-started-with-aws-lambda-and-go/" rel="noopener noreferrer"&gt;AWS Lambda and Golang&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/golang-handler.html" rel="noopener noreferrer"&gt;AWS Lambda function handler in Go&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/lambda/serverless-architectures-learn-more/" rel="noopener noreferrer"&gt;Serverless Architecture&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See full source-code at &lt;a href="https://github.com/datumbrain/go-serverless-aws-lambda-example" rel="noopener noreferrer"&gt;this github repository&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Background process in Lambda using SQS</title>
      <dc:creator>Muhammad Hamza Hameed</dc:creator>
      <pubDate>Mon, 21 Nov 2022 07:19:34 +0000</pubDate>
      <link>https://forem.com/hamzahameed/background-process-in-lambda-using-sqs-pc5</link>
      <guid>https://forem.com/hamzahameed/background-process-in-lambda-using-sqs-pc5</guid>
      <description>&lt;h2&gt;
  
  
  AWS Lambda
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;AWS Lambda&lt;/code&gt; is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers. You can trigger Lambda from over 200 AWS services and software as a service (SaaS) applications and only pay for what you use.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;We want some work to process at background (asynchronously) in Lambda even if the Lambda function returns. But when the lambda function returns, all the background services automatically stop.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;So here we are going to use Amazon SQS for this purpose. We will send a message to a queue, and it will trigger another lambda function that will run in the background.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  What is Amazon SQS?
&lt;/h3&gt;

&lt;p&gt;Amazon &lt;code&gt;Simple Queue Service (SQS)&lt;/code&gt; is a managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications. Using SQS, you can send, store, and receive messages asynchronously between software components at any volume, without losing messages or requiring other services to be available.&lt;/p&gt;

&lt;p&gt;SQS offers two types of message queues. &lt;code&gt;Standard queues&lt;/code&gt; offer maximum throughput, best-effort ordering, and at-least-once delivery. &lt;code&gt;SQS FIFO queues&lt;/code&gt; are designed to guarantee that messages are processed exactly once, in the exact order that they are sent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Golang Example to Run Background Processes in Lambda Using SQS
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;For building and deploying your functions, you’ll be using the &lt;code&gt;Serverless Framework&lt;/code&gt;, which is the most widely used tool for the job. Assuming you have a recent version of &lt;a href="https://nodejs.org/en/"&gt;Node.js&lt;/a&gt; installed, you can install the &lt;code&gt;Serverless CLI&lt;/code&gt; with the following npm command
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;  $&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; serverless
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you have the Serverless CLI installed, you must configure it to use the AWS access keys of your account&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;  $&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;serverless config credentials &lt;span class="nt"&gt;--provider&lt;/span&gt; aws &lt;span class="nt"&gt;--key&lt;/span&gt; &amp;lt;access key ID&amp;gt; &lt;span class="nt"&gt;--secret&lt;/span&gt; &amp;lt;secret access key&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can get the access key and secret key from the &lt;code&gt;My Security Credentials&lt;/code&gt; option. Use &lt;code&gt;Create New Access Key&lt;/code&gt; if you do not have one already&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5ua7Giu2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/91vrhnamx37zffwrg32c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5ua7Giu2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/91vrhnamx37zffwrg32c.png" alt="Image description" width="880" height="592"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you don’t have Go installed yet, you can either &lt;a href="https://golang.org/dl/"&gt;download an installer&lt;/a&gt; from the official website or use your favorite package manager to install it&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Create a Messaging Queue
&lt;/h3&gt;

&lt;p&gt;Let's create a queue. Go to Simple Queue Service (SQS) in your AWS account. You will see the following interface there&lt;/p&gt;

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

&lt;p&gt;Create queue from here and select type &lt;code&gt;FIFO&lt;/code&gt; and let's name it &lt;code&gt;test.fifo&lt;/code&gt; as we want to process exactly once message, in the exact order that they are sent. you can also use the &lt;code&gt;standard queue&lt;/code&gt; according to your need. And we'll keep all other settings to default for now.&lt;/p&gt;

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

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

&lt;p&gt;And now let's code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sending Message on the SQS Queue
&lt;/h3&gt;

&lt;p&gt;Now that you have everything you need, let’s install the &lt;a href="https://github.com/aws/aws-sdk-go"&gt;AWS SDK for Go&lt;/a&gt; library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;go get github.com/aws/aws-sdk-go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this, we can proceed to write the code to send a message on our SQS queue to trigger the lambda function that will perform the background task.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;handlers&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="s"&gt;"github.com/aws/aws-sdk-go/aws"&lt;/span&gt;
  &lt;span class="s"&gt;"github.com/aws/aws-sdk-go/aws/credentials"&lt;/span&gt;
  &lt;span class="s"&gt;"github.com/aws/aws-sdk-go/aws/session"&lt;/span&gt;
  &lt;span class="s"&gt;"github.com/aws/aws-sdk-go/service/sqs"&lt;/span&gt;
  &lt;span class="s"&gt;"os"&lt;/span&gt;

  &lt;span class="s"&gt;"encoding/json"&lt;/span&gt;
  &lt;span class="s"&gt;"log"&lt;/span&gt;
  &lt;span class="s"&gt;"net/http"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;yourHttpHandlerFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWrite&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;// tasks you need to perform before the background task&lt;/span&gt;
  &lt;span class="o"&gt;...&lt;/span&gt;

  &lt;span class="c"&gt;// sending details to other lambda function to perform the background task&lt;/span&gt;
  &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;SqsTriggerMessage&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Message&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"You can add different fields here according to your data requirements in the background task."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusInternalServerError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c"&gt;// other tasks you need to perform&lt;/span&gt;
  &lt;span class="o"&gt;...&lt;/span&gt;

  &lt;span class="c"&gt;// sending user an immediate response&lt;/span&gt;
  &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;SqsTriggerMessage&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;Message&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;svc&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sqs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Must&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewSession&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;aws&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;Credentials&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;credentials&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewStaticCredentials&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"AWS_ACCESS_KEY_ID"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"AWS_SECRET_ACCESS_KEY"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
          &lt;span class="n"&gt;Region&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;      &lt;span class="n"&gt;aws&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"AWS_REGION"&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;svc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetQueueUrl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;sqs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetQueueUrlInput&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;QueueName&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;aws&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"queue-name"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;svc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;sqs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SendMessageInput&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;MessageBody&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;            &lt;span class="n"&gt;aws&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="n"&gt;QueueUrl&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;               &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;QueueUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;MessageGroupId&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;         &lt;span class="n"&gt;aws&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"group-id"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;MessageDeduplicationId&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;aws&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"deduplication-id"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This previous code can be broken into a few simple steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define a message struct that contains the data you need in the background task&lt;/li&gt;
&lt;li&gt;Marshal your struct into JSON and send that JSON as message to the SQS queue
We can also send this through the message attributes, but I found this method more convenient to use&lt;/li&gt;
&lt;li&gt;Give the user a &lt;code&gt;200&lt;/code&gt; response and return. Your lambda function will finish after this, but the message you sent on the SQS queue will trigger the other lambda function to perform the background task&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Lambda Function to Handle SQS Event
&lt;/h3&gt;

&lt;p&gt;Let's create another program to handle sqs events.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="s"&gt;"github.com/aws/aws-lambda-go/events"&lt;/span&gt;
  &lt;span class="s"&gt;"github.com/aws/aws-lambda-go/lambda"&lt;/span&gt;

  &lt;span class="s"&gt;"encoding/json"&lt;/span&gt;
  &lt;span class="s"&gt;"log"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;lambda&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handleSqsRequest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;handleSqsRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sqsEvent&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SQSEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;sqsEvent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Records&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="n"&gt;SqsTriggerMessage&lt;/span&gt;
    &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unmarshal&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;continue&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c"&gt;// task you need to perform based on the data in the message&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;SqsTriggerMessage&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;Message&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This previous code can be broken into a few simple steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We wrote a &lt;code&gt;handleSqsRequest&lt;/code&gt; function that will receive the messages from queue and registered it in the main function using the &lt;code&gt;AWS Lambda for Go library&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In the event handler function, we are unmarshalling the message body into our message struct, and printing the message on the console. You can send the details of the task you need to perform and use those to call appropriate function to do that task&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we have 2 Lambda functions the first one sends the message to SQS queue, that needs to trigger our second lambda function to perform the background task&lt;/p&gt;

&lt;h3&gt;
  
  
  Deployment
&lt;/h3&gt;

&lt;p&gt;Our lambda functions are now ready, and we can proceed by deploying it with the Serverless Framework. Our application is deployed by the Serverless framework based on the &lt;code&gt;serverless.yml&lt;/code&gt; configuration file.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you are not familiar with the &lt;code&gt;.yml&lt;/code&gt; syntax, you can read this &lt;a href="https://bref.sh/docs/environment/serverless-yml.html"&gt;serverless.yml guide&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We must first create a &lt;code&gt;serverless.yml&lt;/code&gt; file that defines what we are deploying.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;your-service-name&lt;/span&gt;
&lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;aws&lt;/span&gt;
  &lt;span class="na"&gt;runtime&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;go1.x&lt;/span&gt;
&lt;span class="na"&gt;package&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;exclude&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./**&lt;/span&gt;
  &lt;span class="na"&gt;include&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./bin/**&lt;/span&gt;
&lt;span class="na"&gt;functions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;main-program&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;bin/main-program&lt;/span&gt;
    &lt;span class="na"&gt;events&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;http&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/&lt;/span&gt;
          &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;get&lt;/span&gt;
      &lt;span class="s"&gt;...&lt;/span&gt;
  &lt;span class="na"&gt;sqs-handler&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;bin/sqs-handler&lt;/span&gt;
    &lt;span class="na"&gt;events&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;sqs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;replace_this_with_your_sqs_queue_arn&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above file, we defined both our lambda functions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first one runs the main program on the HTTP requests through &lt;code&gt;Amazon API Gateway&lt;/code&gt;. In this program we need to perform the background task, so we will send a message on the SQS queue&lt;/li&gt;
&lt;li&gt;The second one runs our sqs handler on an SQS event in the queue which &lt;code&gt;arn&lt;/code&gt; we provide here. You can get the &lt;code&gt;arn&lt;/code&gt; from your aws account where we created it. &lt;code&gt;Amazon SQS &amp;gt; Queues &amp;gt; test.fifo&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Next up, we will build our code, and deploy it using the &lt;code&gt;serverless deploy&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;GOOS=linux GOARCH=amd64 go build -o bin/main-program .
GOOS=linux GOARCH=amd64 go build -o bin/sqs-handler ./sqs

serverless deploy
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see the logs of all your lambda functions through the &lt;code&gt;CloudWatch&lt;/code&gt;. To access the logs, go to &lt;code&gt;CloudWatch &amp;gt; Log groups&lt;/code&gt; in your amazon account.&lt;/p&gt;

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

&lt;p&gt;You can see the logs from here. Let's check the &lt;code&gt;sqs-handler&lt;/code&gt; logs. It must have printed the received message.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/sqs/"&gt;Amazon SQS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html"&gt;AWS Lambda and SQS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/golang-handler.html"&gt;AWS Lambda Function Handler in Go&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/lambda/serverless-architectures-learn-more/"&gt;Serverless Framework and SQS&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Get the full source code from &lt;a href="https://github.com/datumbrain/background-process-using-sqs"&gt;this github repository&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>lamda</category>
      <category>sqs</category>
      <category>go</category>
    </item>
  </channel>
</rss>
