Skip to content

Commit da6f1b2

Browse files
committed
update go fargate example to build docker image and push to ecr
1 parent e9d65b9 commit da6f1b2

File tree

8 files changed

+120
-35
lines changed

8 files changed

+120
-35
lines changed

aws-go-fargate/README.md

+6-26
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
This example shows authoring Infrastructure as Code in the [Go programming language](https://golang.org). It
66
provisions a full [Amazon Elastic Container Service (ECS) "Fargate"](https://aws.amazon.com/ecs) cluster and
7-
related infrastructure, running a load-balanced NGINX web server accessible over the Internet on port 80.
7+
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.
88
This example is inspired by [Docker's Getting Started Tutorial](https://docs.docker.com/get-started/).
99

1010
## Prerequisites
@@ -73,31 +73,11 @@ Next, to deploy the application and its infrastructure, follow these steps:
7373
7474
```bash
7575
$ curl http://$(pulumi stack output url)
76-
<!DOCTYPE html>
77-
<html>
78-
<head>
79-
<title>Welcome to nginx!</title>
80-
<style>
81-
body {
82-
width: 35em;
83-
margin: 0 auto;
84-
font-family: Tahoma, Verdana, Arial, sans-serif;
85-
}
86-
</style>
87-
</head>
88-
<body>
89-
<h1>Welcome to nginx!</h1>
90-
<p>If you see this page, the nginx web server is successfully installed and
91-
working. Further configuration is required.</p>
92-
93-
<p>For online documentation and support please refer to
94-
<a href="http://nginx.org/">nginx.org</a>.<br/>
95-
Commercial support is available at
96-
<a href="http://nginx.com/">nginx.com</a>.</p>
97-
98-
<p><em>Thank you for using nginx.</em></p>
99-
</body>
100-
</html>
76+
42
77+
$ curl http://$(pulumi stack output url)
78+
19
79+
$ curl http://$(pulumi stack output url)
80+
88
10181
```
10282
10383
7. Try making some changes, rebuilding, and rerunning `pulumi up`. For example, let's scale up to 5 instances:

aws-go-fargate/app/Dockerfile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Start from a Debian image with the latest version of Go installed
2+
# and a workspace (GOPATH) configured at /go.
3+
FROM golang
4+
5+
# Copy the local package files to the container's workspace.
6+
ADD . /go/src/foo
7+
8+
# Build the outyet command inside the container.
9+
# (You may fetch or manage dependencies here,
10+
# either manually or with a tool like "godep".)
11+
WORKDIR /go/src/foo
12+
RUN go build -o /go/bin/main
13+
14+
# Run the outyet command by default when the container starts.
15+
ENTRYPOINT /go/bin/main
16+
17+
# Document that the service listens on port 80.
18+
EXPOSE 80

aws-go-fargate/app/go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/pulumi/examples/aws-go-fargate/app
2+
3+
go 1.14
4+
5+
require github.com/gorilla/mux v1.8.0

aws-go-fargate/app/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
2+
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=

aws-go-fargate/app/main.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"math/rand"
7+
"net/http"
8+
"time"
9+
10+
"github.com/gorilla/mux"
11+
)
12+
13+
func main() {
14+
r := mux.NewRouter()
15+
handler := func(w http.ResponseWriter, r *http.Request) {
16+
rand.Seed(time.Now().UnixNano())
17+
fmt.Fprintf(w, "%d", rand.Intn(100))
18+
}
19+
r.HandleFunc("/", handler)
20+
s := &http.Server{
21+
Addr: ":80",
22+
Handler: r,
23+
}
24+
log.Fatal(s.ListenAndServe())
25+
}

aws-go-fargate/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ go 1.13
44

55
require (
66
github.com/pulumi/pulumi-aws/sdk/v3 v3.1.0
7+
github.com/pulumi/pulumi-docker/sdk/v2 v2.2.3
78
github.com/pulumi/pulumi/sdk/v2 v2.2.1
89
)

aws-go-fargate/go.sum

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3+
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
4+
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
35
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
46
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
57
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
@@ -148,6 +150,11 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z
148150
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
149151
github.com/pulumi/pulumi-aws/sdk/v3 v3.1.0 h1:huTCq1WTR4f45JA1yJHqRVtS7v7aoKCYs4xs71VOKRo=
150152
github.com/pulumi/pulumi-aws/sdk/v3 v3.1.0/go.mod h1:Tiu0MSxPkufZDbNTXNMb8cKiIRWLt+7spM3DPuzLcBc=
153+
github.com/pulumi/pulumi-docker v1.5.0 h1:Wg94ooWKcOpI2A+rsbuEtD2i+o5IGeZY1e9sdhqeDKk=
154+
github.com/pulumi/pulumi-docker/sdk v0.0.0-20200416220238-c315a35622c8 h1:dy+YUHixjhKx2kY0eT97/cfYp/i+2ODnKzDHvQi1D4A=
155+
github.com/pulumi/pulumi-docker/sdk/v2 v2.2.3 h1:QDMqeWyxMyP725xx9pYgV4EIC/avMqNsThLsiuo64bA=
156+
github.com/pulumi/pulumi-docker/sdk/v2 v2.2.3/go.mod h1:J0I8pec653rvuAZNKpNxLj6ZuIKutcRDP+4iYUuD504=
157+
github.com/pulumi/pulumi/sdk/v2 v2.0.0/go.mod h1:W7k1UDYerc5o97mHnlHHp5iQZKEby+oQrQefWt+2RF4=
151158
github.com/pulumi/pulumi/sdk/v2 v2.2.1 h1:fNp+69Udcsft3ME+5lKt2IFT3imFAN6D98WiuiAIq/Q=
152159
github.com/pulumi/pulumi/sdk/v2 v2.2.1/go.mod h1:QNbWpL4gvf3X0lUFT7TXA2Jo1ff/Ti2l97AyFGYwvW4=
153160
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
@@ -162,6 +169,7 @@ github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasO
162169
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
163170
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
164171
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
172+
github.com/spf13/cobra v0.0.6/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
165173
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
166174
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
167175
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=

aws-go-fargate/main.go

+55-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package main
22

33
import (
4+
"encoding/base64"
5+
"fmt"
6+
"strings"
7+
48
"github.com/pulumi/pulumi-aws/sdk/v3/go/aws/ec2"
9+
"github.com/pulumi/pulumi-aws/sdk/v3/go/aws/ecr"
510
"github.com/pulumi/pulumi-aws/sdk/v3/go/aws/ecs"
611
elb "github.com/pulumi/pulumi-aws/sdk/v3/go/aws/elasticloadbalancingv2"
712
"github.com/pulumi/pulumi-aws/sdk/v3/go/aws/iam"
13+
"github.com/pulumi/pulumi-docker/sdk/v2/go/docker"
814
"github.com/pulumi/pulumi/sdk/v2/go/pulumi"
915
)
1016

@@ -107,6 +113,54 @@ func main() {
107113
return err
108114
}
109115

116+
repo, err := ecr.NewRepository(ctx, "foo", &ecr.RepositoryArgs{})
117+
if err != nil {
118+
return err
119+
}
120+
121+
repoCreds := repo.RegistryId.ApplyStringArray(func(rid string) ([]string, error) {
122+
creds, err := ecr.GetCredentials(ctx, &ecr.GetCredentialsArgs{
123+
RegistryId: rid,
124+
})
125+
if err != nil {
126+
return nil, err
127+
}
128+
data, err := base64.StdEncoding.DecodeString(creds.AuthorizationToken)
129+
if err != nil {
130+
fmt.Println("error:", err)
131+
return nil, err
132+
}
133+
134+
return strings.Split(string(data), ":"), nil
135+
})
136+
repoUser := repoCreds.Index(pulumi.Int(0))
137+
repoPass := repoCreds.Index(pulumi.Int(1))
138+
139+
image, err := docker.NewImage(ctx, "my-image", &docker.ImageArgs{
140+
Build: docker.DockerBuildArgs{
141+
Context: pulumi.String("./app"),
142+
},
143+
ImageName: repo.RepositoryUrl,
144+
Registry: docker.ImageRegistryArgs{
145+
Server: repo.RepositoryUrl,
146+
Username: repoUser,
147+
Password: repoPass,
148+
},
149+
})
150+
151+
containerDef := image.ImageName.ApplyString(func(name string) (string, error) {
152+
fmtstr := `[{
153+
"name": "my-app",
154+
"image": %q,
155+
"portMappings": [{
156+
"containerPort": 80,
157+
"hostPort": 80,
158+
"protocol": "tcp"
159+
}]
160+
}]`
161+
return fmt.Sprintf(fmtstr, name), nil
162+
})
163+
110164
// Spin up a load balanced service running NGINX.
111165
appTask, err := ecs.NewTaskDefinition(ctx, "app-task", &ecs.TaskDefinitionArgs{
112166
Family: pulumi.String("fargate-task-definition"),
@@ -115,15 +169,7 @@ func main() {
115169
NetworkMode: pulumi.String("awsvpc"),
116170
RequiresCompatibilities: pulumi.StringArray{pulumi.String("FARGATE")},
117171
ExecutionRoleArn: taskExecRole.Arn,
118-
ContainerDefinitions: pulumi.String(`[{
119-
"name": "my-app",
120-
"image": "nginx",
121-
"portMappings": [{
122-
"containerPort": 80,
123-
"hostPort": 80,
124-
"protocol": "tcp"
125-
}]
126-
}]`),
172+
ContainerDefinitions: containerDef,
127173
})
128174
if err != nil {
129175
return err

0 commit comments

Comments
 (0)