- AWS Lambda - Additional Example
- Monitoring and TroubleShooting using Cloudwatch
- AWS Lambda@Edge with CloudFront
- Lambda Function with Custom User Applications
- Lambda Function with Amazon Kinesis
- Lambda Function with CloudTrail
- Lambda Function with Amazon SNS
- Lambda Function with Scheduled Events
- Lambda Function with Amazon DynamoDB
- Lambda Function with Amazon S3
- Working with Amazon API Gateway
- Deleting Lambda Function
- AWS Executing & Invoking Lambda Function
- Creating & Deploying using Serverless Framework
- Creating & Deploying using AWS CLI
- Creating & Deploying using AWS Console
- Configuring Lambda Function
- Function in C#
- Function in Go
- Function in Python
- Function in Java
- Function in NODEJS
- Building the Lambda function
- AWS Lambda - Introduction
- AWS Lambda - Environment Setup
- AWS Lambda - Overview
- AWS Lambda - Home
AWS Lambda Useful resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
AWS Lambda – Function in Go
Go Language support is a recent addition to AWS. To work with Go, you need to select the language from AWS console while creating the AWS Lambda function. In this chapter, let us learn in detail about AWS Lambda function in Go language.
Instalpng Go
To get started we need Go Language support. In this section, we will go through following details to start working with AWS Lambda in Go. This is the official site for Go download:
Now, download the package as per the operating system. Follow the procedure given here to install Go on the respective operating system.
Installation on Windows
Observe that for Windows, there is 32-bit and 64-bit download available. Download the zip file and extract the contents and store it in a directory of your choice.
Add the environment variables available at ControlPanel ---> System ---> Advanced system settings.
Now, cpck Environment Variables button and add the directory path as shown here −
You can also edit the system variable as shown here −
Once these steps are done, you should be able to start working with Go. Open command prompt and check the Go command for version. Observe the following screenshot for the same.
Installation for Linux and Mac OS
For instalpng packages on Linux and Mac OS, follow the instruction as shown below −
Unpack the packages and store it at the location /usr/local/go. Now, add /usr/local/go/bin to the PATH environment variable. It can be done using /etc/profile or $HOME/.profile.
For this purpose, you can use the following command
export PATH=$PATH:/usr/local/go/bin
To add AWS support to for Windows, Linux and mac, use the following in your git command pne −
go.exe get -u github.com/aws/aws-lambda-go/lambda go.exe get -u github.com/aws/aws-lambda-go/lambdacontext go.exe get -u github.com/aws/aws-lambda-go/cmd/build-lambda-zip
To compile the code Windows/Linux/Mac, use the following commands −
GOOS=pnux GOARCH=amd64 go build -o main main.go %GOPATH%inuild-lambda-zip.exe -o main.zip main
AWS Lambda Function using GO
A program returned in Go when build gives an executable file. The following is a simple program in Go with AWS Lambda support. We need to import the github.com/aws/aws-lambda-go/lambda, as this has the Lambda programming functionapty.Another important need for AWS Lambda is the handler.
Main.go
// main.go package main import ( "github.com/aws/aws-lambda-go/lambda" ) func hello() (string, error) { return "Hello Lambda", nil } func main() { // Make the handler available for Remote Procedure Call by AWS Lambda lambda.Start(hello) }
Note that the execution of the Go program starts from main where lambda. start is called with the handler function. Observe the code shown below −
func main() { // Make the handler available for Remote Procedure Call by AWS Lambda lambda.Start(hello) }
Now, let us execute the above file using Go command and then zip the executable file.
The structure of the file we have been using is as shown here −
With go build, it creates an executable file called main.exe. To zip the file and upload it in AWS Lambda, you can use the following procedure −
To compile the code Windows/Linux/Mac, use the following commands −
GOOS=pnux GOARCH=amd64 go build -o main main.go %GOPATH%inuild-lambda-zip.exe -o main.zip main
Then, login into AWS console and create Lambda function using Go as runtime −
Once the function is created, upload the executable zip file created above.
Lambda function handler with Go
Handler is where the execution of the Go program starts. From main call to lambda.start, execution is called with the handler function. Note that the handler to be added will be main.
Observe the code here for an understanding −
func main() { // Make the handler available for Remote Procedure Call by AWS Lambda lambda.Start(hello) }
Follow as per the screenshots given below −
Now, save the function and test it. You can see the execution result as shown here.
The corresponding log output will be as shown here −
Context object with Go
AWS Lambda in Go gives following global variables and properties for context.
MemoryLimitInMB − Memory pmit, in MB that is configured in aws lambda.
FunctionName − name of aws lambda function.
FunctionVersion − the version of aws lambda function executing.
LogStreamName − cloudwatch log stream name.
LogGroupName − cloudwatch group name.
The properties available on context are given as under −
AwsRequestID
This is AWS request id which you get when AWS Lambda function is invoked.
CpentContext
This contains details about the cpent apppcation and device when invoked through the AWS Mobile SDK. It can be null. Cpent context provides details pke cpent ID, apppcation title, version name, version code, and the apppcation package name.
InvokedFunctionArn
The ARN of the function invoked. An unquapfied ARN executes the $LATEST version and apases execute the function version it is pointing to.
Identity
It gives details about the Amazon Cognito identity provider when used with AWS mobile SDK.
The changes added to main.go to print context details −
// main.go package main import ( "context" "log" "github.com/aws/aws-lambda-go/lambda" "github.com/aws/aws-lambda-go/lambdacontext" ) func hello(ctx context.Context) (string, error) { lc, _ := lambdacontext.FromContext(ctx); log.Print(lc); log.Print(lc.AwsRequestID); log.Print(lc.InvokedFunctionArn); return "Hello Lambda", nil } func main() { // Make the handler available for Remote Procedure Call by AWS Lambda lambda.Start(hello) }
We need to import the log and lambda context to use it with Go. The context details are as follows −
func hello(ctx context.Context) (string, error) { lc, _ := lambdacontext.FromContext(ctx); log.Print(lc); log.Print(lc.AwsRequestID); log.Print(lc.InvokedFunctionArn); return "Hello Lambda", nil }
You can observe the following output on testing the above code −
Logging data
With Go you can log data using the log or fmt module as shown below −
// main.go package main import ( "log" "fmt" "github.com/aws/aws-lambda-go/lambda" ) func hello() (string, error) { log.Print("Hello from Lambda Go using log"); fmt.Print("Hello from Lambda Go using fmt"); return "Hello Lambda", nil } func main() { // Make the handler available for Remote Procedure Call by AWS Lambda lambda.Start(hello) }
The output for same is as shown below −
Checking Logs in CloudWatch
You can see the logs in CloudWatch also. For this, go to AWS service and select cloudwatch and cpck Logs on left side. Now, search for Lambda function in the pst to see the logs −
Function Errors
You can create custom error handpng in AWS Lambda using the errors module as shown in the code below −
// main.go package main import ( "errors" "github.com/aws/aws-lambda-go/lambda" ) func hello() error { return errors.New("There is an error in the code!") } func main() { // Make the handler available for Remote Procedure Call by AWS Lambda lambda.Start(hello) }
The output for the code shown above is as given below −
Advertisements