Skip to content

Commit

Permalink
update go fargate example to build docker image and push to ecr
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanBoyle committed Aug 31, 2020
1 parent e9d65b9 commit da6f1b2
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 35 deletions.
32 changes: 6 additions & 26 deletions aws-go-fargate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

This example shows authoring Infrastructure as Code in the [Go programming language](https://golang.org). It
provisions a full [Amazon Elastic Container Service (ECS) "Fargate"](https://aws.amazon.com/ecs) cluster and
related infrastructure, running a load-balanced NGINX web server accessible over the Internet on port 80.
related infrastructure, building a docker image, pushing it to ECR, and using it to run a web server accessible over the Internet on port 80.
This example is inspired by [Docker's Getting Started Tutorial](https://docs.docker.com/get-started/).

## Prerequisites
Expand Down Expand Up @@ -73,31 +73,11 @@ Next, to deploy the application and its infrastructure, follow these steps:

```bash
$ curl http:https://$(pulumi stack output url)
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http:https://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http:https://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
42
$ curl http:https://$(pulumi stack output url)
19
$ curl http:https://$(pulumi stack output url)
88
```

7. Try making some changes, rebuilding, and rerunning `pulumi up`. For example, let's scale up to 5 instances:
Expand Down
18 changes: 18 additions & 0 deletions aws-go-fargate/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Start from a Debian image with the latest version of Go installed
# and a workspace (GOPATH) configured at /go.
FROM golang

# Copy the local package files to the container's workspace.
ADD . /go/src/foo

# Build the outyet command inside the container.
# (You may fetch or manage dependencies here,
# either manually or with a tool like "godep".)
WORKDIR /go/src/foo
RUN go build -o /go/bin/main

# Run the outyet command by default when the container starts.
ENTRYPOINT /go/bin/main

# Document that the service listens on port 80.
EXPOSE 80
5 changes: 5 additions & 0 deletions aws-go-fargate/app/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/pulumi/examples/aws-go-fargate/app

go 1.14

require github.com/gorilla/mux v1.8.0
2 changes: 2 additions & 0 deletions aws-go-fargate/app/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
25 changes: 25 additions & 0 deletions aws-go-fargate/app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"log"
"math/rand"
"net/http"
"time"

"github.com/gorilla/mux"
)

func main() {
r := mux.NewRouter()
handler := func(w http.ResponseWriter, r *http.Request) {
rand.Seed(time.Now().UnixNano())
fmt.Fprintf(w, "%d", rand.Intn(100))
}
r.HandleFunc("/", handler)
s := &http.Server{
Addr: ":80",
Handler: r,
}
log.Fatal(s.ListenAndServe())
}
1 change: 1 addition & 0 deletions aws-go-fargate/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ go 1.13

require (
github.com/pulumi/pulumi-aws/sdk/v3 v3.1.0
github.com/pulumi/pulumi-docker/sdk/v2 v2.2.3
github.com/pulumi/pulumi/sdk/v2 v2.2.1
)
8 changes: 8 additions & 0 deletions aws-go-fargate/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
Expand Down Expand Up @@ -148,6 +150,11 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/pulumi/pulumi-aws/sdk/v3 v3.1.0 h1:huTCq1WTR4f45JA1yJHqRVtS7v7aoKCYs4xs71VOKRo=
github.com/pulumi/pulumi-aws/sdk/v3 v3.1.0/go.mod h1:Tiu0MSxPkufZDbNTXNMb8cKiIRWLt+7spM3DPuzLcBc=
github.com/pulumi/pulumi-docker v1.5.0 h1:Wg94ooWKcOpI2A+rsbuEtD2i+o5IGeZY1e9sdhqeDKk=
github.com/pulumi/pulumi-docker/sdk v0.0.0-20200416220238-c315a35622c8 h1:dy+YUHixjhKx2kY0eT97/cfYp/i+2ODnKzDHvQi1D4A=
github.com/pulumi/pulumi-docker/sdk/v2 v2.2.3 h1:QDMqeWyxMyP725xx9pYgV4EIC/avMqNsThLsiuo64bA=
github.com/pulumi/pulumi-docker/sdk/v2 v2.2.3/go.mod h1:J0I8pec653rvuAZNKpNxLj6ZuIKutcRDP+4iYUuD504=
github.com/pulumi/pulumi/sdk/v2 v2.0.0/go.mod h1:W7k1UDYerc5o97mHnlHHp5iQZKEby+oQrQefWt+2RF4=
github.com/pulumi/pulumi/sdk/v2 v2.2.1 h1:fNp+69Udcsft3ME+5lKt2IFT3imFAN6D98WiuiAIq/Q=
github.com/pulumi/pulumi/sdk/v2 v2.2.1/go.mod h1:QNbWpL4gvf3X0lUFT7TXA2Jo1ff/Ti2l97AyFGYwvW4=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Expand All @@ -162,6 +169,7 @@ github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasO
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.6/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
Expand Down
64 changes: 55 additions & 9 deletions aws-go-fargate/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package main

import (
"encoding/base64"
"fmt"
"strings"

"github.com/pulumi/pulumi-aws/sdk/v3/go/aws/ec2"
"github.com/pulumi/pulumi-aws/sdk/v3/go/aws/ecr"
"github.com/pulumi/pulumi-aws/sdk/v3/go/aws/ecs"
elb "github.com/pulumi/pulumi-aws/sdk/v3/go/aws/elasticloadbalancingv2"
"github.com/pulumi/pulumi-aws/sdk/v3/go/aws/iam"
"github.com/pulumi/pulumi-docker/sdk/v2/go/docker"
"github.com/pulumi/pulumi/sdk/v2/go/pulumi"
)

Expand Down Expand Up @@ -107,6 +113,54 @@ func main() {
return err
}

repo, err := ecr.NewRepository(ctx, "foo", &ecr.RepositoryArgs{})
if err != nil {
return err
}

repoCreds := repo.RegistryId.ApplyStringArray(func(rid string) ([]string, error) {
creds, err := ecr.GetCredentials(ctx, &ecr.GetCredentialsArgs{
RegistryId: rid,
})
if err != nil {
return nil, err
}
data, err := base64.StdEncoding.DecodeString(creds.AuthorizationToken)
if err != nil {
fmt.Println("error:", err)
return nil, err
}

return strings.Split(string(data), ":"), nil
})
repoUser := repoCreds.Index(pulumi.Int(0))
repoPass := repoCreds.Index(pulumi.Int(1))

image, err := docker.NewImage(ctx, "my-image", &docker.ImageArgs{
Build: docker.DockerBuildArgs{
Context: pulumi.String("./app"),
},
ImageName: repo.RepositoryUrl,
Registry: docker.ImageRegistryArgs{
Server: repo.RepositoryUrl,
Username: repoUser,
Password: repoPass,
},
})

containerDef := image.ImageName.ApplyString(func(name string) (string, error) {
fmtstr := `[{
"name": "my-app",
"image": %q,
"portMappings": [{
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp"
}]
}]`
return fmt.Sprintf(fmtstr, name), nil
})

// Spin up a load balanced service running NGINX.
appTask, err := ecs.NewTaskDefinition(ctx, "app-task", &ecs.TaskDefinitionArgs{
Family: pulumi.String("fargate-task-definition"),
Expand All @@ -115,15 +169,7 @@ func main() {
NetworkMode: pulumi.String("awsvpc"),
RequiresCompatibilities: pulumi.StringArray{pulumi.String("FARGATE")},
ExecutionRoleArn: taskExecRole.Arn,
ContainerDefinitions: pulumi.String(`[{
"name": "my-app",
"image": "nginx",
"portMappings": [{
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp"
}]
}]`),
ContainerDefinitions: containerDef,
})
if err != nil {
return err
Expand Down

0 comments on commit da6f1b2

Please sign in to comment.