Skip to content

Commit

Permalink
feat: make server run on AWS lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenschneider committed Oct 4, 2023
1 parent d38491f commit bfe821f
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
/.idea
/keypair.json
dist/
/bootstrap
/dyndns-server-lambda.zip
26 changes: 16 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ clean:
rm -rf ./$(BUILD_DIR)

build: version-info
CGO_ENABLED=0 go build -ldflags="-X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BINARY_NAME_SERVER) -tags server cmd/server/server.go
CGO_ENABLED=0 go build -ldflags="-X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BINARY_NAME_CLIENT) -tags client cmd/client/client.go
CGO_ENABLED=0 go build -race -ldflags="-w -X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BINARY_NAME_SERVER) -tags server cmd/server/server.go
CGO_ENABLED=0 go build -race -ldflags="-w -X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BINARY_NAME_CLIENT) -tags client cmd/client/client.go

release: clean version-info cross-build-client cross-build-server
sha256sum $(BUILD_DIR)/dyndns-* > $(CHECKSUM_FILE)
Expand All @@ -23,17 +23,23 @@ signed-release: release
pass keys/signify/github | signify -S -s $(SIGNATURE_KEYFILE) -m $(CHECKSUM_FILE)
gh-upload-assets -o soerenschneider -r dyndns -f ~/.gh-token builds

lambda-server:
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -tags server,aws -ldflags="-w -X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o bootstrap cmd/server/server_lambda.go
rm -f dyndns-server-lambda.zip
zip dyndns-server-lambda.zip bootstrap
rm -f bootstrap

cross-build-server:
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_SERVER)-linux-x86_64 -tags server cmd/server/server.go
GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=0 go build -ldflags="-X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_SERVER)-linux-armv5 -tags server cmd/server/server.go
GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=0 go build -ldflags="-X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_SERVER)-linux-armv6 -tags server cmd/server/server.go
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -ldflags="-X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_SERVER)-linux-aarch64 -tags server cmd/server/server.go
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_SERVER)-linux-x86_64 -tags server cmd/server/server.go
GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=0 go build -ldflags="-w -X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_SERVER)-linux-armv5 -tags server cmd/server/server.go
GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=0 go build -ldflags="-w -X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_SERVER)-linux-armv6 -tags server cmd/server/server.go
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -ldflags="-w -X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_SERVER)-linux-aarch64 -tags server cmd/server/server.go

cross-build-client:
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_CLIENT)-linux-x86_64 -tags client cmd/client/client.go
GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=0 go build -ldflags="-X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_CLIENT)-linux-armv5 -tags client cmd/client/client.go
GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=0 go build -ldflags="-X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_CLIENT)-linux-armv6 -tags client cmd/client/client.go
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -ldflags="-X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_CLIENT)-linux-aarch64 -tags client cmd/client/client.go
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_CLIENT)-linux-x86_64 -tags client cmd/client/client.go
GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=0 go build -ldflags="-w -X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_CLIENT)-linux-armv5 -tags client cmd/client/client.go
GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=0 go build -ldflags="-w -X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_CLIENT)-linux-armv6 -tags client cmd/client/client.go
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -ldflags="-w -X '$(MODULE)/internal.BuildVersion=${VERSION}' -X '$(MODULE)/internal.CommitHash=${COMMIT_HASH}'" -o $(BUILD_DIR)/$(BINARY_NAME_CLIENT)-linux-aarch64 -tags client cmd/client/client.go

docker-build-server:
docker build -t "$(DOCKER_PREFIX)/$(BINARY_NAME_SERVER)" --build-arg MODE=server .
Expand Down
69 changes: 69 additions & 0 deletions cmd/server/server_lambda.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//go:build aws

package main

import (
"encoding/json"
"os"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/rs/zerolog/log"
conf2 "github.com/soerenschneider/dyndns/conf"
"github.com/soerenschneider/dyndns/internal/common"
server2 "github.com/soerenschneider/dyndns/server"
"github.com/soerenschneider/dyndns/server/dns"
)

const defaultRegion = "us-east-1"

var propagator dns.Propagator
var server *server2.Server

func init() {
region := os.Getenv("AWS_REGION")
if region == "" {
region = defaultRegion
}

conf := conf2.GetDefaultServerConfig()
if err := conf2.ParseEnvVariables(conf); err != nil {
log.Fatal().Err(err).Msg("could not parse config")
}

var err error
propagator, err = dns.NewRoute53Propagator(conf.HostedZoneId, nil)
if err != nil {
log.Fatal().Err(err).Msg("could not build propagator")
}

c := make(chan common.UpdateRecordRequest)
server, err = server2.NewServer(*conf, propagator, c, nil)
if err != nil {
log.Fatal().Err(err).Msg("could not build server")
}
}

func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
payload := common.UpdateRecordRequest{}
if err := json.Unmarshal([]byte(request.Body), &payload); err != nil {
return events.APIGatewayProxyResponse{
Body: "could not parse json",
StatusCode: 400,
}, err
}

if err := server.HandlePropagateRequest(payload); err != nil {
return events.APIGatewayProxyResponse{
StatusCode: 500,
}, err
}

return events.APIGatewayProxyResponse{
StatusCode: 200,
}, nil
}

func main() {
lambda.Start(handler)
}
11 changes: 6 additions & 5 deletions conf/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/rs/zerolog/log"
"os"

"github.com/rs/zerolog/log"
)

type MqttConfig struct {
Brokers []string `json:"brokers" env:"DYNDNS_BROKERS" envSeparator:";" validate:"required,broker"`
ClientId string `json:"client_id" env:"DYNDNS_CLIENT_ID" validate:"required"`
Brokers []string `json:"brokers" env:"DYNDNS_BROKERS" envSeparator:";" validate:"broker"`
ClientId string `json:"client_id" env:"DYNDNS_CLIENT_ID" validate:"required_with=Brokers ''"`
CaCertFile string `json:"tls_ca_cert" env:"DYNDNS_TLS_CA" validate:"omitempty,file"`
ClientCertFile string `json:"tls_client_cert" env:"DYNDNS_TLS_CERT" validate:"omitempty,file"`
ClientKeyFile string `json:"tls_client_key" env:"DYNDNS_TLS_KEY" validate:"omitempty,file"`
ClientCertFile string `json:"tls_client_cert" env:"DYNDNS_TLS_CERT" validate:"omitempty,required_unless=ClientKeyFile '',file"`
ClientKeyFile string `json:"tls_client_key" env:"DYNDNS_TLS_KEY" validate:"omitempty,required_unless=ClientCertFile '',file"`
TlsInsecure bool `json:"tls_insecure" env:"DYNDNS_TLS_INSECURE"`
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
)

require (
github.com/aws/aws-lambda-go v1.41.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v3 v3.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aws/aws-lambda-go v1.41.0 h1:l/5fyVb6Ud9uYd411xdHZzSf2n86TakxzpvIoz7l+3Y=
github.com/aws/aws-lambda-go v1.41.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM=
github.com/aws/aws-sdk-go v1.45.16 h1:spca2z7UJgoQ5V2fX6XiHDCj2E65kOJAfbUPozSkE24=
github.com/aws/aws-sdk-go v1.45.16/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down

0 comments on commit bfe821f

Please sign in to comment.