/
main.go
170 lines (150 loc) · 4.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"encoding/base64"
"errors"
"github.com/pulumi/pulumi-aws/sdk/v4/go/aws/cloudwatch"
"github.com/pulumi/pulumi-aws/sdk/v4/go/aws/ecr"
"github.com/pulumi/pulumi-aws/sdk/v4/go/aws/iam"
"github.com/pulumi/pulumi-aws/sdk/v4/go/aws/lambda"
"github.com/pulumi/pulumi-docker/sdk/v3/go/docker"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"strings"
)
const name = "helloworld"
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
repo, err := createPrivateEcrRepository(ctx)
if err != nil {
return err
}
err = createEcrRepositoryLifecycle(ctx, err, repo)
if err != nil {
return err
}
registryInfo := getRegistryInfo(ctx, repo)
// Build and publish the container image.
image, err := docker.NewImage(ctx, name+"-image", &docker.ImageArgs{
Build: &docker.DockerBuildArgs{Context: pulumi.String("../lambda")},
ImageName: repo.RepositoryUrl,
Registry: registryInfo,
})
if err != nil {
return err
}
role, err := createIamRoleForLambda(ctx)
if err != nil {
return err
}
function, err := createLambda(ctx, image, role)
if err != nil {
return err
}
// Export the lambda ARN.
ctx.Export("lambda", function.Arn)
return nil
})
}
func createPrivateEcrRepository(ctx *pulumi.Context) (*ecr.Repository, error) {
repo, err := ecr.NewRepository(ctx, name+"-repo", nil)
if err != nil {
return nil, err
}
return repo, nil
}
func createEcrRepositoryLifecycle(ctx *pulumi.Context, err error, repo *ecr.Repository) error {
_, err = ecr.NewLifecyclePolicy(ctx, name+"-policy", &ecr.LifecyclePolicyArgs{
Repository: repo.Name,
Policy: pulumi.String(`{
"rules": [{
"rulePriority": 1,
"description": "Expire untagged images after 7 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 7
},
"action": {
"type": "expire"
}
}]
}`),
})
return err
}
func getRegistryInfo(ctx *pulumi.Context, repo *ecr.Repository) docker.ImageRegistryOutput {
registryInfo := repo.RegistryId.ApplyT(func(id string) (docker.ImageRegistry, error) {
creds, err := ecr.GetCredentials(ctx, &ecr.GetCredentialsArgs{RegistryId: id})
if err != nil {
return docker.ImageRegistry{}, err
}
decoded, err := base64.StdEncoding.DecodeString(creds.AuthorizationToken)
if err != nil {
return docker.ImageRegistry{}, err
}
parts := strings.Split(string(decoded), ":")
if len(parts) != 2 {
return docker.ImageRegistry{}, errors.New("invalid credentials")
}
return docker.ImageRegistry{
Server: creds.ProxyEndpoint,
Username: parts[0],
Password: parts[1],
}, nil
}).(docker.ImageRegistryOutput)
return registryInfo
}
func createIamRoleForLambda(ctx *pulumi.Context) (*iam.Role, error) {
role, err := iam.NewRole(ctx, name+"-lambda-exec-role", &iam.RoleArgs{
AssumeRolePolicy: pulumi.String(`{
"Version": "2012-10-17",
"Statement": [{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}]
}`),
})
if err != nil {
return nil, err
}
_, err = iam.NewRolePolicyAttachment(ctx, name+"-lambda-exec", &iam.RolePolicyAttachmentArgs{
Role: role.Name,
PolicyArn: pulumi.String("arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"),
})
if err != nil {
return nil, err
}
return role, nil
}
func createLambda(ctx *pulumi.Context, image *docker.Image, role *iam.Role) (*lambda.Function, error) {
logGroup, err := cloudwatch.NewLogGroup(ctx, name, &cloudwatch.LogGroupArgs{
Name: pulumi.String("/aws/lambda/" + name),
RetentionInDays: pulumi.Int(30),
})
if err != nil {
return nil, err
}
args := &lambda.FunctionArgs{
ImageUri: image.ImageName,
MemorySize: pulumi.Int(128),
Name: pulumi.String(name),
PackageType: pulumi.String("Image"),
Publish: pulumi.Bool(false),
Role: role.Arn,
Timeout: pulumi.Int(3),
}
function, err := lambda.NewFunction(
ctx,
name+"-lambda",
args,
pulumi.DependsOn([]pulumi.Resource{role, logGroup}),
)
if err != nil {
return nil, err
}
return function, nil
}