Steps for setting up an event notification for an imported Bucket

My current project involves writing the script in cdk using typescript.

I'm trying to use an existing bucket as a trigger for my lambda function.

const _bucket = s3.Bucket.fromBucketName(this,"frombucket",`vr-${targetEnv}-resource-up-bk`);

lambdaFromContainer_.addEventSource(new S3EventSource(_bucket, { 
  events: [ s3.EventType.OBJECT_CREATED],

}));

The issue arises when s3.Bucket.fromBucketName returns IBucket, causing an error with S3EventSource(_bucket

Argument of type 'IBucket' is not assignable to parameter of type 'Bucket'.
  Type 'IBucket' is missing the following properties from type 'Bucket': autoCreatePolicy, lifecycleRules, metrics, cors, and 31 more.ts(2345)

Any ideas on how to resolve this problem?

Interestingly, creating a new Bucket does not result in any errors.

const _bucket = new s3.Bucket(this, 'cdk-out-bk', {
      bucketName: `vr-${targetEnv}-resource-up-bk`,
}
lambdaFromContainer_.addEventSource(new S3EventSource(_bucket, { 
  events: [ s3.EventType.OBJECT_CREATED],

}));

Answer №1

The reason for this limitation is that CDK lacks the ability to alter imported resources, hence the restricted properties available on IBucket. To set up notifications for a bucket, it's necessary to create the bucket and configure the notification within the same CDK application.

To delve deeper into this matter, please refer to the inquiry at: Implementing managed policies with CDK

Despite these constraints, custom resources offer flexibility in making modifications. Fortunately, there exists a custom resource specifically designed for adding event notifications to imported buckets. Here’s how you can utilize it:

bucket.addEventNotification(s3.EventType.OBJECT_REMOVED, new s3n.SqsDestination(mySqsQueue));

An illustrative example sourced from the documentation:

https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_s3-readme.html#importing-existing-buckets

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transforming an array into an object using TypeScript

I am attempting to create a generic type for a function that transforms an array into an object, like so: type ObjectType = { id: number; name: string; status: string }; const xyz: ObjectType[] = [ { id: 1, name: "X", status: " ...

Capturing a webpage through Record RTC integration with Angular

I am looking to record a specific section of the page as a video with audio. For example, when a user enters the page, I want it to automatically start recording the activities in that particular area (similar to how iframe videos work). The recording sh ...

Combine iron-page and bind them together

Recently, I've started learning about Polymer and I want to bind together paper-tabs and iron-pages so that when a tab is clicked, the content loads dynamically. After going through the documentation, this is what I have tried: <app-toolbar> ...

Checking for null properties in Typescript objectsorHow to verify if a

What is a simple way to determine if the properties of an object in TypeScript are nullable? For example export default interface UserDto{ ID?:int; USER_NAME?:string; FIRST_NAME?:string; LAST_NAME?:string; USER_ROLE?: ...

How to use TypeScript to filter arrays with multiple dimensions

I've been attempting to filter an array with multiple filters, but I can't seem to achieve the desired outcome so far. This is my Angular component: list = [ {type: type1, code: code1}, {type: type2, code: code2}] searchElement(code?: string, ...

Having troubles with *ngFor in Angular 8? Learn how to use ng-template effectively

I need assistance creating a table with dynamically generated columns and using the PrimeNg library for the grid. Despite asking several questions, I have not received any responses. Can someone please help me achieve this? To generate table column heade ...

Using Typescript to pass a property as one of the keys in an object's list of values

In my React Native project, I need to pass a string value from one component to another. The different options for the value can be found in the ScannerAction object: export const ScannerAction = { move: 'move', inventory: 'inventory&apo ...

What is the best way to include type within a nested object?

How should I properly define types for a nested object structured like the example below? const theme: DefaultTheme = { color: { primary: '#5039E7', secondary: '#372E4B', heading: '#4D5062', }, ...

angular 2 checkbox for selecting multiple items at once

Issue I have been searching for solutions to my problem with no luck. I have a table containing multiple rows, each row having a checkbox. I am trying to implement a "select all" and "deselect all" functionality for these checkboxes. Below is an example o ...

What is causing the delay in processing the async await function?

While developing my express application, I decided to use async+await for handling asynchronous operations. However, I encountered a significant delay in completing a particular task. async.parallel( { cars: function (callback) { Car.findById( ...

This property cannot be found on the specified type

So, I have this TypeScript function that will return one of two different objects based on the input value: function myfunc(isError:boolean): {response:string}|{error:string} { if(isError) return {error:''} return {response:''} } N ...

Tips for executing a function when nearing the bottom of a scroll:

I have incorporated the angular2-infinite-scroll plugin, specifically version 0.1.4. You can view my plunker here. Currently, the function onScrollDown() only runs once at the beginning when scrolling. I attempted to adjust the values for infiniteScroll ...

Encountering the error message "express.default is not a function" while attempting to start the node server within a container

Whenever I try to start my node server in a remote container, I keep encountering an error stating "express.default is not a function." Can anyone help me figure this out? Here's the content of my main.ts file: import * as express from 'express& ...

Can I assign a value from the tagModel to ngx-chips in an Angular project?

HTML code: <tag-input class="martop20 tag-adder width100 heightauto" [onAdding]="onAdding" (onAdd)="addInternalDomain($event)" type="text" Ts code: addInternalDomain(tagTex ...

ReactTS: Setting a default generic for dynamic components

Just a follow-up to my previous question: Is it possible to extend type by dynamic Component Props in React with TypeScript? Consider the following code snippet: type Base = { baseProp: ... } type Extend = { extendProp: ... } // use this if "as ...

Experimenting with a module reliant on two distinct services

I am facing an issue with a component that relies on a service to fetch data. The service also retrieves configurations from a static variable in the Configuration Service, but during Karma tests, the const variable is showing up as undefined. Although I ...

What is the best method to transfer data between two div elements?

I'm currently working on a shopping app and trying to implement a feature where clicking on a product adds it to the checkoutList. However, I'm facing an issue where when a product is clicked, no data is being sent and I am getting 'undefine ...

Step-by-step guide on launching a node.js application on AWS Elastic Beanstalk

Attempting to launch a simple Nodejs application on AWS Elastic Beanstalk via the AWS Console. The Nodejs app consists of a basic HelloWorld message. I'm encountering an issue where I don't see the Hello World message when clicking on the Elastic ...

Connect values with formBuilder in Angular

I have an array of objects structured like this const premises = [ { question: '1', value: '1' }, { question: '2', value: '2' }, { question: '3', value: '3' } ] ...

What is the best way to retrieve a value from an array of objects containing both objects and strings in TypeScript?

Consider this scenario with an array: const testData = [ { properties: { number: 1, name: 'haha' } , second: 'this type'}, ['one', 'two', 'three'], ]; The goal is to access the value of 'second&ap ...