Serverless Deployment
This guide explains how to deploy the Go REST API using the Serverless Framework with AWS Lambda.
Prerequisites
- Node.js and npm installed
- Serverless Framework CLI installed (
npm install -g serverless) - AWS CLI configured with your credentials
Files
The following files in the /deployments/serverless directory are used for the serverless deployment:
serverless.yml: Main configuration file for the Serverless FrameworkTaskfile.yml: Contains commands to build and deploy the applicationmain.go: The Lambda function entry point
Deployment Steps
-
Navigate to the serverless deployment directory:
cd deployments/serverless -
Build the Go binary:
task build -
Deploy the application:
task sls:deployThis command will package your application and deploy it to AWS Lambda using the Serverless Framework.
-
After successful deployment, you'll see output with details about your Lambda function and API Gateway endpoint.
Configuration
The serverless.yml file contains the configuration for your serverless deployment. Key points:
- The service name is set to
go-rest-api - It's configured to deploy to the
us-east-1region by default - The Lambda function is named
apiand uses thego1.xruntime - An API Gateway is set up to trigger the Lambda function
- The API paths are defined to route requests to the Lambda function
Taskfile Commands
The Taskfile.yml provides the following commands:
task build: Compiles the Go application for AWS Lambdatask sls:deploy: Deploys the application using Serverless Frameworktask sls:remove: Removes the deployed application from AWS (with confirmation prompt)
Customization
To customize the deployment:
- Modify the
serverless.ymlfile to change AWS region, function name, or API Gateway settings. - Update the
main.gofile if you need to change the Lambda function's behavior. - Adjust the
Taskfile.ymlif you need to modify the build or deployment process.
Accessing the API
After deployment, you can access your API using the URL provided in the Serverless Framework output. It will look something like:
https://