AWS GoLang Lambda Build and Deployment on Windows Tutorial
Here's a simple and straight-forward way to deploy GoLang Lambdas from your Windows PC. In this article we'll cover how to do a simple deployment of a lambda, although there many more ways to deploy a lambda, but we'll leave those for another day. In this article we will cover:
- Building our code
- Compressing our code
- Uploading our code to our AWS Lambda
Step 1: Build your code
GoLang code must be compiled in order to be executed. First, locate your GoLang installation:
Navigate to your GoLang installation
If you do not know where this is then do the following.
Press your Windows key to open the start menu. Search for "go.exe" but don't press enter. On the right you should see the option "Open file location". Select this option. You should see the file go.exe in the File Explorer. This is your installation.
Once you have located your GoLang installation, hold down the Shift Key and Right Click on the go.exe file, then select the Copy as path option to copy the path to your clipboard.
Next, we will build our code using our GoLang installation
Using File Explorer, navigate to where your GoLang code is stored on your PC. Typically you want to be in the same directory as your go.mod and go.sum files as well as any source code files e.g. main.go.
In the Address Bar, enter cmd and press enter. This will open up the command prompt in the current directory.
Enter the following command:
1
set GOOS=linux
Then paste the path to your GoLang installation that we copied into your clipboard, then remove the ".exe" leaving something like this:
1
C:\Users\james\sdk\go1.15.3\bin\go
Now type build -o bin\main followed by any source files. For example:
1
C:\Users\james\sdk\go1.15.3\bin\go build -o bin\main main.go
Heres an example which builds with multiple source files:
1
C:\Users\james\sdk\go1.15.3\bin\go build -o bin\main main.go other.go
You've now built your code! You can find the built code in the bin folder.
Step 2: Compress your files
In order to upload your code and files to your lambda, you need to compress and package all that you want to upload.
It is recommended you use AWS's build-lambda-zip tool. To download this tool run this command:
1
C:\Users\james\sdk\go1.15.3\bin\go get -u github.com/aws/aws-lambda-go/cmd/build-lambda-zip
Note: You must have git installed to download this tool. You can download git from the Git SCM website.
Now locate where the tool has been installed
You can find the tool installation location the same way as finding your GoLang installation.
Press the Windows key and search for build-lambda-zip.exe but don't press enter (Note: be sure to include the .exe in your search).
On the right, select "Open file location".
Hold down the Shift key and right click on build-lambda-zip.exe, then select Copy as path.
Now we'll run the tool
From the same command prompt (assuming you're still in the same directory as your go source code), paste the tool's location and type -output main.zip bin\main as shown in the example bellow:
1
C:\Users\james\go\bin\build-lambda-zip.exe -output main.zip bin\main
Run this command. This will compress and package your code into the main.zip file that should now be seen in your directory.
This main.zip file is what you upload to your AWS Lambda.
Step 3: Upload your packaged code to your AWS Lambda
In the AWS Console, navigate to your GoLang Lambda.
Change Lambda Runtime Settings
Under the Runtime settings section, make sure that your handler is set to be the main function you defined in your source code. This will be the entry point to your code. Usually, the handler will be main.
Upload your packaged code
Under the Function code section, click on the Actions drop down and select the "Upload .zip file" option and upload your main.zip file.
Completed
Congratulations! You've now uploaded your code to your lambda. You can now test it, link it up to an API Gateway endpoint, and many other things.