-
Notifications
You must be signed in to change notification settings - Fork 42
/
bucket.go
136 lines (115 loc) · 4.37 KB
/
bucket.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
// Copyright Nitric Pty Ltd.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package deploy
import (
"fmt"
"github.com/nitrictech/nitric/cloud/common/deploy/resources"
common "github.com/nitrictech/nitric/cloud/common/deploy/tags"
deploymentspb "github.com/nitrictech/nitric/core/pkg/proto/deployments/v1"
storagepb "github.com/nitrictech/nitric/core/pkg/proto/storage/v1"
"github.com/pulumi/pulumi-aws/sdk/v5/go/aws/lambda"
"github.com/pulumi/pulumi-aws/sdk/v5/go/aws/s3"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/samber/lo"
)
type S3NotificationArgs struct {
Location string
StackID string
Bucket *s3.Bucket
Listeners []*deploymentspb.BucketListener
Lambdas map[string]*lambda.Function
}
func eventTypeToStorageEventType(eventType *storagepb.BlobEventType) []string {
switch *eventType {
case storagepb.BlobEventType_Created:
return []string{"s3:ObjectCreated:*"}
case storagepb.BlobEventType_Deleted:
return []string{"s3:ObjectRemoved:*"}
default:
return []string{}
}
}
// createNotification creates an AWS S3 bucket notification, containing all target lambda functions and their filters
func createNotification(ctx *pulumi.Context, name string, args *S3NotificationArgs, opts ...pulumi.ResourceOption) (*s3.BucketNotification, error) {
invokePerms := map[string]pulumi.Resource{}
notificationTargetLambdas := s3.BucketNotificationLambdaFunctionArray{}
for _, listener := range args.Listeners {
// Get the deployed service
funcName := listener.GetService()
lambdaFunc, ok := args.Lambdas[funcName]
if !ok {
return nil, fmt.Errorf("invalid service %s given for bucket subscription", funcName)
}
// Don't create duplicate permissions
if invokePerms[funcName] == nil {
perm, err := lambda.NewPermission(ctx, name+"-"+funcName, &lambda.PermissionArgs{
Action: pulumi.String("lambda:InvokeFunction"),
Function: lambdaFunc.Arn,
Principal: pulumi.String("s3.amazonaws.com"),
SourceArn: args.Bucket.Arn,
}, opts...)
if err != nil {
return nil, fmt.Errorf("unable to create lambda invoke permission: %w", err)
}
invokePerms[funcName] = perm
}
if listener.Config.KeyPrefixFilter == "*" {
listener.Config.KeyPrefixFilter = ""
}
notificationTargetLambdas = append(notificationTargetLambdas, s3.BucketNotificationLambdaFunctionArgs{
LambdaFunctionArn: lambdaFunc.Arn,
Events: pulumi.ToStringArray(
eventTypeToStorageEventType(&listener.Config.BlobEventType),
),
FilterPrefix: pulumi.String(listener.Config.KeyPrefixFilter),
}.ToBucketNotificationLambdaFunctionOutput())
}
notificationOptions := append([]pulumi.ResourceOption{pulumi.DependsOn(lo.Values(invokePerms))}, opts...)
notification, err := s3.NewBucketNotification(ctx, name, &s3.BucketNotificationArgs{
Bucket: args.Bucket.ID(),
LambdaFunctions: notificationTargetLambdas,
}, notificationOptions...)
if err != nil {
return nil, fmt.Errorf("unable to create bucket notification: %w", err)
}
return notification, nil
}
// Bucket - Implements deployments of Nitric Buckets using AWS S3
func (a *NitricAwsPulumiProvider) Bucket(ctx *pulumi.Context, parent pulumi.Resource, name string, config *deploymentspb.Bucket) error {
opts := []pulumi.ResourceOption{pulumi.Parent(parent)}
bucket, err := s3.NewBucket(ctx, name, &s3.BucketArgs{
Tags: pulumi.ToStringMap(common.Tags(a.StackId, name, resources.Bucket)),
}, opts...)
if err != nil {
return err
}
a.Buckets[name] = bucket
if len(config.Listeners) > 0 {
notificationName := fmt.Sprintf("notification-%s", name)
notification, err := createNotification(ctx, notificationName, &S3NotificationArgs{
StackID: a.StackId,
Location: a.Region,
Bucket: bucket,
Lambdas: a.Lambdas,
Listeners: config.Listeners,
}, opts...)
if err != nil {
return err
}
a.BucketNotifications[name] = notification
}
return nil
}