1
1
package main
2
2
3
3
import (
4
+ "encoding/base64"
5
+ "fmt"
6
+ "strings"
7
+
4
8
"github.com/pulumi/pulumi-aws/sdk/v3/go/aws/ec2"
9
+ "github.com/pulumi/pulumi-aws/sdk/v3/go/aws/ecr"
5
10
"github.com/pulumi/pulumi-aws/sdk/v3/go/aws/ecs"
6
11
elb "github.com/pulumi/pulumi-aws/sdk/v3/go/aws/elasticloadbalancingv2"
7
12
"github.com/pulumi/pulumi-aws/sdk/v3/go/aws/iam"
13
+ "github.com/pulumi/pulumi-docker/sdk/v2/go/docker"
8
14
"github.com/pulumi/pulumi/sdk/v2/go/pulumi"
9
15
)
10
16
@@ -107,6 +113,54 @@ func main() {
107
113
return err
108
114
}
109
115
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
+
110
164
// Spin up a load balanced service running NGINX.
111
165
appTask , err := ecs .NewTaskDefinition (ctx , "app-task" , & ecs.TaskDefinitionArgs {
112
166
Family : pulumi .String ("fargate-task-definition" ),
@@ -115,15 +169,7 @@ func main() {
115
169
NetworkMode : pulumi .String ("awsvpc" ),
116
170
RequiresCompatibilities : pulumi.StringArray {pulumi .String ("FARGATE" )},
117
171
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 ,
127
173
})
128
174
if err != nil {
129
175
return err
0 commit comments