UNPKG

serverless

Version:

Serverless Framework - Build web, mobile and IoT applications with serverless architectures using AWS Lambda, Azure Functions, Google CloudFunctions & more

74 lines (62 loc) 2.02 kB
'use strict'; const { addPermission, removePermission } = require('./lib/permissions'); const { updateConfiguration, removeConfiguration } = require('./lib/bucket'); const { getEnvironment, getLambdaArn, handlerWrapper } = require('../utils'); function handler(event, context) { if (event.RequestType === 'Create') { return create(event, context); } else if (event.RequestType === 'Update') { return update(event, context); } else if (event.RequestType === 'Delete') { return remove(event, context); } throw new Error(`Unhandled RequestType ${event.RequestType}`); } function create(event, context) { const { Region, AccountId } = getEnvironment(context); const { FunctionName, BucketName, BucketConfig } = event.ResourceProperties; const lambdaArn = getLambdaArn(Region, AccountId, FunctionName); return addPermission({ functionName: FunctionName, bucketName: BucketName, region: Region, }).then(() => updateConfiguration({ lambdaArn, region: Region, functionName: FunctionName, bucketName: BucketName, bucketConfig: BucketConfig, }) ); } function update(event, context) { const { Region, AccountId } = getEnvironment(context); const { FunctionName, BucketName, BucketConfig } = event.ResourceProperties; const lambdaArn = getLambdaArn(Region, AccountId, FunctionName); return updateConfiguration({ lambdaArn, region: Region, functionName: FunctionName, bucketName: BucketName, bucketConfig: BucketConfig, }); } function remove(event, context) { const { Region } = getEnvironment(context); const { FunctionName, BucketName } = event.ResourceProperties; return removePermission({ functionName: FunctionName, bucketName: BucketName, region: Region, }).then(() => removeConfiguration({ region: Region, functionName: FunctionName, bucketName: BucketName, }) ); } module.exports = { handler: handlerWrapper(handler, 'CustomResouceExistingS3'), };