Get Started
Deploy Golang with LaunchFlow
Deploy a Go application to AWS Fargate with LaunchFlow.
View the source code for this guide in our examples repo.
0. Set up your Go Project
If you already have a Go Project you can skip to step #1.
Initialize your Go module:
1mkdir launchflow-go
2cd launchflow-go
3go mod init launchflow-go
Create a main.go
file with a simple HTTP server:
1package main
2
3import (
4 "fmt"
5 "log"
6 "net/http"
7 "os"
8)
9
10func main() {
11 lfEnv := os.Getenv("LAUNCHFLOW_ENVIRONMENT")
12 if lfEnv == "" {
13 log.Fatal("LaunchFlow environment not set")
14 }
15 port := os.Getenv("PORT")
16 if port == "" {
17 port = "3000"
18 }
19 host := os.Getenv("HOST")
20 if host == "" {
21 host = "127.0.0.1"
22 }
23 address := fmt.Sprintf("%s:%s", host, port)
24
25 greeting := fmt.Sprintf("Hello from %s!", lfEnv)
26 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
27 fmt.Fprint(w, greeting)
28 })
29
30 log.Printf("Server listening on %s", address)
31 log.Fatal(http.ListenAndServe(address, nil))
32}
Create a Dockerfile
in the root directory of your project:
1FROM public.ecr.aws/docker/library/golang:1.20 as builder
2
3WORKDIR /app
4COPY . .
5
6RUN CGO_ENABLED=0 GOOS=linux go build -o /go/bin/app
7
8FROM public.ecr.aws/docker/library/alpine:3.14
9
10COPY /go/bin/app /go/bin/app
11
12ENV HOST=0.0.0.0
13ENV PORT=80
14
15ENTRYPOINT ["/go/bin/app"]
1. Initialize Launch Flow
Install the LaunchFlow Python SDK and CLI using pip
.
1pip install launchflow[aws]
Initialize LaunchFlow in your project
1lf init --backend=local
- Name your project
- Select
Yes
for creating an exampleinfra.py
- Select
AWS
for your cloud provider - Select
ECS Fargate
for your service
Once finished you will get an infra.py
that looks like:
1import launchflow as lf
2
3# ECSFargateService Docs: https://docs.launchflow.com/reference/aws-services/ecs-fargate
4api = lf.aws.ECSFargateService(
5 "my-ecs-api",
6 dockerfile="Dockerfile", # Path to your Dockerfile
7)
ECSFargateService will build your Dockerfile and deploy to ECS Fargate. You can provide additional fields to ECSFargateService
to configure things like machine type, num instances, or even a custom domain.
2. Deploy your Service
Make sure you have local AWS credentials set up before deploying.
1lf deploy
- Name your environment (
dev
is a good first name) - Select your cloud provider
AWS
- Confirm the resources to be created
- Select the service to deploy
Once complete you will see a link to your deployed service on ECS Fargate.
3. Cleanup your Resources
Optionally you can delete all your resources, service, and environments with:
1lf destroy
2lf environments delete
4. Visualize, Share, and Automate
LaunchFlow Cloud usage is optional and free for individuals.
Using the local backend like we did above works fine for starting a project, but doesn't offer a way to share state between multiple users. LaunchFlow Cloud is a web-based service for managing, sharing, and automating your infrastructure. It's free small teams and provides a simple, secure way to collaborate with your team and automate your release pipelines.
Sign up for LaunchFlow Cloud and connect your local environment by running:
1lf init --backend=lf
This will create a project in your LaunchFlow Cloud account and migrate your local state to the LaunchFlow Cloud backend.
What's next?
- Add resources to your application
- Promote your application to a production enviroment
- Learn more about Environments, Services, and Resources
- Join theLaunchFlow Slack community
- View your application in theLaunchFlow console