<?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: NMK</title>
    <description>The latest articles on Forem by NMK (@nmk).</description>
    <link>https://forem.com/nmk</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%2F455739%2F6ca276ef-300d-4ade-8c59-f39d5c1182b0.png</url>
      <title>Forem: NMK</title>
      <link>https://forem.com/nmk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nmk"/>
    <language>en</language>
    <item>
      <title>serverless offline golang setup</title>
      <dc:creator>NMK</dc:creator>
      <pubDate>Thu, 20 Aug 2020 10:21:56 +0000</pubDate>
      <link>https://forem.com/nmk/serverless-offline-golang-setup-11lp</link>
      <guid>https://forem.com/nmk/serverless-offline-golang-setup-11lp</guid>
      <description>&lt;p&gt;Recently i have faced an issue in running the serverless application built using GoLang in offline(locally).&lt;/p&gt;

&lt;p&gt;So, i just want to share the detailed steps &lt;/p&gt;

&lt;h1&gt;
  
  
  Initial Setup
&lt;/h1&gt;

&lt;p&gt;Pre requisites&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Serverless package should be installed globally&lt;/li&gt;
&lt;li&gt;Golang&lt;/li&gt;
&lt;li&gt;Make sure you're in your ${GOPATH}/src directory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;take a sample application setup from aws examples&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ serverless create -t aws-go-dep -p &amp;lt;your project name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Add a Makefile in root directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.PHONY: build clean deploy
build:
    env GOOS=linux go build -ldflags="-s -w" -o &amp;lt;your bunary path&amp;gt;
clean:
    rm -rf ./bin
deploy: clean build
    sls deploy --verbose
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Add package.json file in the project root directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "name": "lambda-golang-offline",
    "description": "To run the golanf lambda service locally",
    "version": "1.0.0",
    "scripts": {
        "start": "node ./node_modules/.bin/serverless offline start --useDocker",
        "watch": "nodemon --watch ./ -e go,js --exec make build"
    },
    "devDependencies": {
        "nodemon": "^2.0.3",
        "serverless": "^1.70.0",
        "serverless-offline": "^6.1.5"
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now install the npm dependencies using the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm i
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now build the binary&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ make build
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now start the server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;for the first time it will take time. wait and then hit the api in postman&lt;/p&gt;

&lt;p&gt;while development use &lt;code&gt;watch&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm run watch
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;serverless.yaml looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;service: returns
frameworkVersion: '&amp;gt;=1.28.0 &amp;lt;2.0.0'
provider:
  name: aws
plugins:
  - serverless-offline
package:
  exclude:
    - ./**
  include:
    - ./bin/**
functions:
  return:
    runtime: go1.x
    handler: &amp;lt;your handler&amp;gt;
    environment:
      env: ${opt:stage, self:provider.stage}
      IS_OFFLINE: false
    events:
      - http: 
          path: /&amp;lt;desires path&amp;gt;
          method: post
          cors:
            origins:
            - "*"
            headers:
            - Content-Type
            - X-Amz-Date
            - Authorization
            - X-Api-Key
            - X-Amz-Security-Token
            - X-Amz-User-Agent

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



&lt;p&gt;if you are planning to use api gateway endpoints in front end then you should add the cors policy in serverless.yaml file&lt;br&gt;
instaed of &lt;code&gt;*&lt;/code&gt; use the relevant origin names&lt;/p&gt;

&lt;p&gt;you can find all the files in &lt;a href="https://github.com/manikanta4a9/serverless-golang-offline"&gt;git&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next Post will be on Serverless lambda setup with typescript and mysql&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>go</category>
    </item>
  </channel>
</rss>
