UNPKG

@pulumi/aws-serverless

Version:

Pulumi Amazon Web Services (AWS) Serverless Components.

101 lines 4.78 kB
"use strict"; // Copyright 2016-2018, Pulumi Corporation. // // 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. Object.defineProperty(exports, "__esModule", { value: true }); const aws = require("@pulumi/aws"); const function_1 = require("./function"); const subscription_1 = require("./subscription"); function onPut(name, bucket, handler, args, opts) { args = args || {}; const argsCopy = Object.assign({}, args, { events: ["s3:ObjectCreated:*"] }); return onEvent(name + "-put", bucket, handler, argsCopy, opts); } exports.onPut = onPut; function onDelete(name, bucket, handler, args, opts) { args = args || {}; const argsCopy = Object.assign({}, args, { events: ["s3:ObjectRemoved:*"] }); return onEvent(name + "-delete", bucket, handler, argsCopy, opts); } exports.onDelete = onDelete; const defaultComputePolicies = [ aws.iam.AWSLambdaFullAccess, aws.iam.AmazonEC2ContainerServiceFullAccess, ]; /** * Creates a new subscription to the given bucket using the lambda provided, along with optional * options to control the behavior of the subscription. This function should be used when full * control over the subscription is wanted, and other helpers (like onPut/onDelete) are not * sufficient. */ function onEvent(name, bucket, handler, args, opts) { const func = function_1.createLambdaFunction(name + "-bucket-subscription", handler, opts); return new BucketEventSubscription(name, bucket, func, args, opts); } exports.onEvent = onEvent; let bucketSubscriptionInfos = new Map(); /** * A component corresponding to a single underlying aws.s3.BucketNotification created for a bucket. * Note: due to the AWS requirement that all notifications for a bucket be defined at once, the * actual aws.s3.BucketNotification instances will only be created once the pulumi program runs to * completion and all subscriptions have been heard about. */ class BucketEventSubscription extends subscription_1.EventSubscription { constructor(name, bucket, func, args, opts) { super("aws-serverless:bucket:BucketEventSubscription", name, func, { bucket: bucket }, opts); const permission = new aws.lambda.Permission(name, { function: func, action: "lambda:InvokeFunction", principal: "s3.amazonaws.com", sourceArn: bucket.id.apply(bucketName => `arn:aws:s3:::${bucketName}`), }, { parent: this }); this.permission = permission; // We must create only a single BucketNotification per Bucket per AWS API limitations. See // https://github.com/terraform-providers/terraform-provider-aws/issues/1715. So we push // the subscription information here, and then actually create the BucketNotification if // needed on process `beforeExit`. let subscriptions = bucketSubscriptionInfos.get(bucket); if (!subscriptions) { subscriptions = []; bucketSubscriptionInfos.set(bucket, subscriptions); } subscriptions.push({ name: name, events: args.events, filterPrefix: args.filterPrefix, filterSuffix: args.filterSuffix, lambdaFunctionArn: func.arn, permission: permission, }); } } exports.BucketEventSubscription = BucketEventSubscription; process.on("beforeExit", () => { const copy = bucketSubscriptionInfos; // Since we are generating more work on the event loop, we will casue `beforeExit` to be invoked again. // Make sure to clear out eh pending subscrpitions array so that we don't try to apply them again. bucketSubscriptionInfos = new Map(); for (const [bucket, subscriptions] of copy) { const permissions = subscriptions.map(s => s.permission); const _ = new aws.s3.BucketNotification(subscriptions[0].name, { bucket: bucket.id, lambdaFunctions: subscriptions.map(subscription => ({ events: subscription.events, filterPrefix: subscription.filterPrefix, filterSuffix: subscription.filterSuffix, lambdaFunctionArn: subscription.lambdaFunctionArn, })), }, { parent: bucket, dependsOn: permissions }); } }); //# sourceMappingURL=bucket.js.map