AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS). With AWS Lambda, you can run your code without provisioning or managing servers.
Serverless architecture, also known as Function-as-a-Service (FaaS), is a cloud computing execution model where the cloud provider dynamically manages the allocation and provisioning of servers. In a serverless architecture, developers don’t have to worry about provisioning, scaling, or managing servers.
Testing AWS Lambda functions locally in Golang is crucial as it helps identify and resolve potential build or runtime mismatches between the local and cloud environments.
For example, When building a Golang Lambda function binary on a macOS machine with an Apple Silicon (ARM64) architecture, the resulting binary is compatible with the ARM64 instruction set.However, the AWS Lambda execution environment currently runs on Amazon Linux, which uses an x86-64 (AMD64) architecture.
If you attempt to upload and run the ARM64 binary built on your macOS machine directly on AWS Lambda, it will fail due to the architecture mismatch.
To resolve this issue, you would need to cross-compile the Golang code to produce an x86-64 binary compatible with the AWS Lambda execution environment. This can typically be done by setting the appropriate environment variables (e.g., GOOS=linux, GOARCH=amd64) before building the binary on your macOS machine.
AWS Lambda functions require a specific configuration to define how they should run and what resources they need. While you can provide this configuration through the AWS Management Console or AWS CLI, using a template file makes it easier to version control and manage your Lambda function’s configuration alongside your code.
Example:
AWSTemplateFormatVersion: '2010-09-09'Transform: AWS::Serverless-2016-10-31Description: Hello World FunctionResources: lambdasftp: Type: AWS::Serverless::Function Properties: CodeUri: . Description: '' MemorySize: 1024 Timeout: 3 Handler: main Runtime: provided.al2023 CodeUri: . Architectures: - arm64
AWSTemplateFormatVersion: Specifies the version of the CloudFormation template language.
Transform: Indicates the use of AWS SAM capabilities to simplify serverless resource definitions.
Description: Provides a brief description of the template’s purpose.
Resources: Defines the AWS resources to be deployed.
MyLambdaFunction: A serverless function named MyLambdaFunction
in this case it is lambdasftp
.
AWS::Serverless::Function
for Lambda function)..
indicates current directory).main
function).provided.al2023
based on Amazon Linux 2).package main
import ( "context" "fmt"
"github.com/aws/aws-lambda-go/lambda")
func handler(ctx context.Context) { fmt.Println("Hello World!")}
func main() { lambda.Start(handler)}
Building the go function using:
GOOS=linux CGO_ENABLED=0 go build -o bootstrap main.go
Invoking the function using SAM CLI:
sam local invoke
Output:
Invoking main (provided.al2023)Local image is up-to-dateUsing local image: public.ecr.aws/lambda/provided:al2023-rapid-arm64.
Mounting /Users/randomsumit/dev/aws-lambda-go-exampleas /var/task:ro,delegated, inside runtime containerSTART RequestId: 8e49ebaf-98b4-4ccd-be32-a4effc1b56dbVersion: $LATEST
Hello World!
END RequestId: 56a83226-b92b-4a74-9a8e-6fa410b1afd1REPORT RequestId: 56a83226-b92b-4a74-9a8e-6fa410b1afd1Init Duration: 0.14 ms Duration: 13.67 msBilled Duration: 14 ms Memory Size: 1024 MBMax Memory Used: 1024 MB
null
Thanks for reading the blog. Checkout code at Github