Skip to content

Commit

Permalink
Example of deploying a Go project to fargate.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zemnmez committed Dec 26, 2024
1 parent 5e31d73 commit b97e34d
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 15 deletions.
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ go_deps.from_file(go_mod = "//:go.mod")
use_repo(
go_deps,
"co_honnef_go_tools",
"com_github_aws_aws_lambda_go",
"com_github_bazelbuild_bazel_watcher",
"com_github_bazelbuild_buildtools",
"com_github_go_delve_delve",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go 1.22.7
toolchain go1.23.4

require (
github.com/aws/aws-lambda-go v1.47.0
github.com/bazelbuild/bazel-gazelle v0.40.0
github.com/bazelbuild/bazel-watcher v0.25.3
github.com/bazelbuild/buildtools v0.0.0-20240918101019-be1c24cc9a44
Expand Down
16 changes: 2 additions & 14 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ts/pulumi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ts_project(
"//ts/pulumi/lulu.computer:pulumi_ts",
"//ts/pulumi/pleaseintroducemetoyour.dog:ts",
"//ts/pulumi/shadwell.im",
"//ts/pulumi/zemn.me/hello_world:ts",
],
)

Expand Down
2 changes: 1 addition & 1 deletion ts/pulumi/lib/tags.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { all, Input } from '@pulumi/pulumi';

type TagSet = Input<Record<string, Input<string>>>;
export type TagSet = Input<Record<string, Input<string>>>;

export const mergeTags = (a?: TagSet, b?: TagSet): TagSet =>
all([a, b]).apply(([a, b]) => ({ ...a, ...b }));
Expand Down
60 changes: 60 additions & 0 deletions ts/pulumi/zemn.me/hello_world/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
load("@rules_oci//oci:defs.bzl", "oci_image")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
load("//bzl:rules.bzl", "bazel_lint")
load("//go:rules.bzl", "go_binary", "go_library")
load("//ts:rules.bzl", "ts_project")
load("//ts/pulumi/lib/oci:rules.bzl", "pulumi_image")

go_library(
name = "hello_world_lib",
srcs = ["hello.go"],
importpath = "github.com/zemn-me/monorepo/ts/pulumi/zemn.me/hello_world",
visibility = ["//visibility:private"],
deps = ["@com_github_aws_aws_lambda_go//lambda"],
)

go_binary(
name = "hello_world",
embed = [":hello_world_lib"],
visibility = ["//visibility:public"],
)

bazel_lint(
name = "bazel_lint",
srcs = ["BUILD.bazel"],
)

pkg_tar(
name = "tar",
srcs = [":hello_world"],
)

oci_image(
name = "image",
base = "@distroless_base",
entrypoint = ["/hello_world"],
tars = [":tar"],
)

pulumi_image(
name = "oci_image",
src = ":image",
component_name = "HelloWorldImage",
)

ts_project(
name = "ts",
srcs = [
"hello_world.ts",
],
visibility = [
"//ts/pulumi:__subpackages__",
],
deps = [
":oci_image",
"//:node_modules/@pulumi/aws",
"//:node_modules/@pulumi/awsx",
"//:node_modules/@pulumi/pulumi",
"//ts/pulumi/lib",
],
)
17 changes: 17 additions & 0 deletions ts/pulumi/zemn.me/hello_world/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"context"

"github.com/aws/aws-lambda-go/lambda"
)

// Handler is the Lambda function entry point
func Handler(ctx context.Context) (string, error) {
return "Hello, World!", nil
}

func main() {
// Start the Lambda function
lambda.Start(Handler)
}
70 changes: 70 additions & 0 deletions ts/pulumi/zemn.me/hello_world/hello_world.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { ecs } from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";
import { ComponentResource, ComponentResourceOptions, interpolate } from "@pulumi/pulumi";

import { mergeTags, TagSet, tagTrue } from "#root/ts/pulumi/lib/tags.js";
import { HelloWorldImage } from '#root/ts/pulumi/zemn.me/hello_world/HelloWorldImage.js';



export interface Args {
tags?: TagSet
}

export class FargateHelloWorld extends ComponentResource {
constructor(
name: string,
args: Args,
opts?: ComponentResourceOptions
) {
super('ts:pulumi:Component', name, args, opts);
const tag = name;
const tags = mergeTags(args.tags, tagTrue(tag));

const cluster = new ecs.Cluster("cluster", {
tags: tags
});

// Create the ECR repository to store our container image
const repo = new awsx.ecr.Repository("repo", {
forceDelete: true,
tags: tags
});

// Create a load balancer to listen for requests and route them to the container.
const loadbalancer = new awsx.lb.ApplicationLoadBalancer("loadbalancer", {});

const img = new HelloWorldImage(
`${name}_img`,
{
repository: repo.url,
}
)

// Define the service and configure it to use our image and load balancer.
new awsx.ecs.FargateService("service", {
cluster: cluster.arn,
assignPublicIp: true,
taskDefinitionArgs: {
container: {
name: "awsx-ecs",
image: img.url,
cpu: 128,
memory: 512,
essential: true,
portMappings: [{
containerPort: 80,
targetGroup: loadbalancer.defaultTargetGroup,
}],
},
},
});

// Export the URL so we can easily access it.
const frontendURL = interpolate`http://${loadbalancer.loadBalancer.dnsName}`;

this.registerOutputs({
frontendURL: frontendURL,
});
}
}
5 changes: 5 additions & 0 deletions ts/pulumi/zemn.me/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { bskyDid } from '#root/project/zemn.me/bio/bio.js';
import { BlueskyDisplayNameClaim } from '#root/ts/pulumi/lib/bluesky_username_claim.js';
import { mergeTags, tagTrue } from '#root/ts/pulumi/lib/tags.js';
import Website from '#root/ts/pulumi/lib/website.js';
import { FargateHelloWorld } from '#root/ts/pulumi/zemn.me/hello_world/hello_world.js';

export interface Args {
zoneId: Pulumi.Input<string>;
Expand Down Expand Up @@ -33,6 +34,10 @@ export class Component extends Pulumi.ComponentResource {
{ parent: this }
);

new FargateHelloWorld(`${name}_fargate`, {
tags: args.tags
});

this.site = new Website(
`${name}_zemn_me`,
{
Expand Down

0 comments on commit b97e34d

Please sign in to comment.