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 FrameworkMakefile
: 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:
make build
-
Deploy the application:
make deploy
This 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-1
region by default - The Lambda function is named
api
and uses thego1.x
runtime - An API Gateway is set up to trigger the Lambda function
- The API paths are defined to route requests to the Lambda function
Makefile Commands
The Makefile
provides the following commands:
make build
: Compiles the Go application for AWS Lambdamake deploy
: Deploys the application using Serverless Frameworkmake remove
: Removes the deployed application from AWS
Customization
To customize the deployment:
- Modify the
serverless.yml
file to change AWS region, function name, or API Gateway settings. - Update the
main.go
file if you need to change the Lambda function's behavior. - Adjust the
Makefile
if 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://