Serverless Golang apps in 60 seconds

By TJ Holowaychuk

In this brief post I’ll illustrate how quick it is production-ready serverless environment for any Golang application or API using Apex Up. This post assumes you have AWS credentials configured on your machine, as Up deploys to AWS Lambda and API Gateway.

Up works great with any Go app, there’s no need to rewrite your application to be purpose-built for serverless, but if you’d like to follow along you can copy paste the following into a main.go file in your project’s directory:

You may have noticed the use of the PORT environment variable in the previous snippet, this is a port number passed by Up to your application so it knows where to listen for requests, aside from that it’s a completely normal Golang net/http app.

Let’s take a look at how Up actually works!

When you see the term “serverless” you may think of FaaS—or functions-as-a-service—but that is not what you’ll find with Up, it deploys your entire application into a single AWS Lambda function, letting you focus on building your API or application as you normally would locally, with go run main.go .

When your app receives a request it is handled by API Gateway, a serverless load balancer provided by AWS. The request is passed to your Lambda function, which typically looks something like the following (in Node.js), where you’re interacting with HTTP using event as the request and a return object as the response. This can be fine in simple scenarios, but it locks you in to FaaS and can be more difficult to develop locally.

exports.handle = async function(event, context) {
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(event)
}
}

In order to allow “vanilla” HTTP applications to work, Up handles theAPI Gateway event, transforms it into a real HTTP request. Up sends the request to your app process, then transforms the response back into the return value expected by API Gateway.

Because Up acts as a reverse proxy for your application it can provide middleware features such as CORS, response compression, logging, error pages, and more.

To get started with deploying the app, first install up:

$ curl -sf https://up.apex.sh/install | sh

Running up in your project’s directory will prompt you to create an up.json file:

Up will walk you through choosing the application’s name, the AWS profile credentials to use, and the AWS region.

Roughly 60 seconds later you have a fully production-ready serverless environment ready to go! Subsequent deploys will be even faster, as the initial deployment is complete.

The endpoint listed is the staging environment, there is also a production environment by default, but you may define custom stages as well.

Try out your new API with curl :

$ curl https://jb8mxj0cda.execute-api.eu-west-2.amazonaws.com/staging/pets
["Tobi","Loki","Jane"]

Alternatively you may use up url which expands to the same endpoint:

$ curl `up url`/pets
["Tobi","Loki","Jane"]

That’s it! If you’re interested in learning more about what Up has to offer check out the features below, or the documentation. After you’re done run the following command to delete the application and its resources:

$ up stack delete
  • HTTP middleware such as error pages, CORS, redirects, static file serving and more
  • Infrastructure as code—use industry best practices for managing your configuration
  • Infinite scalability—never worry about scaling machines, Up is on-demand
  • Low cost—only pay for what you use, and utilize AWS’ 1,000,000 free requests per month
  • Stage based deployment, with support for custom stages
  • Isolated—don’t worry about downtime from a multi-tenant PaaS, deploy to your own AWS
  • Cost effective structured logging for only $0.5/gb
  • Request isolation—every request is isolated to its own Lambda, a crash will never bring your entire application down
  • Expressive structured log querying (ex: up logs ‘error message = "login failed” region="us-west-2"’`)
  • Free SSL & custom domain name support
  • Slack chat for community support
  • Single monthly flat fee of $10/mo, no additional charges for team members
  • Deployment log history with Git integration
  • Instant rollbacks to a previous deployment or Git tag/commit
  • Encrypted environment variables, centralized and defined per-stage
  • Alerting of errors and performance issues via SMA, Slack, and Email
  • Support for regional endpoints
  • Support for Lambda layers
  • Priority email support
  • Help keep the project alive!