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: 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).Building the go function using:
Invoking the function using SAM CLI:
Output:
Thanks for reading the blog. Checkout code at Github