How to Add a Rule to an Existing Application Load Balancer Listener using AWS CDK

When I inherited a project, I discovered an application load balancer with a HTTPS Listener that was set up before I began using CDK.

This listener currently has 13 rules in place that route requests based on hostname to different fargate instances, with the last rule acting as a fallback option.

Now, I need to add new rules using CDK before the fallback rules. Since new rules will be added for new fargate services in the future, this process needs to be dynamic.

How can I insert new rules into an existing listener and determine the priority of a specific rule (in this case, the fallback rule) to use as a starting point for priority calculation?

Alternatively, is it feasible to simply add one rule after another with each having a priority of 1, causing the priorities of the existing rules to increment by 1 each time?

I managed to retrieve the existing listener using the following code snippet, but I am unsure how to access its rules:

const httpsListener = elbv2.ApplicationListener.fromLookup(
  this,
  "ALBListenerHTTPS", {
    listenerArn: "my:aws:alb:listener:arn",
  }
);

Answer №1

In the world of software development, every ApplicationListener corresponds to a

 AWS::ElasticLoadBalancingV2::ListenerRule
CloudFormation resource.

When you have an ApplicationListener, you can create new ApplicationListenerRules like this:

Create a new ApplicationListenerRule instance, naming it 'Rule1', with parameters such as the listener and priority.
    ...

However, if you want `aws-cdk` to be responsible for managing load balancer rules, they must all be generated by it. There are two potential solutions to achieve this.

Solution A

The first solution involves importing all existing listener rules into your CloudFormation stack. This can be done by utilizing the option to import load balancer rule resources into CloudFormation stack as described here.

Solution B

An alternative approach is to adjust the priorities of existing listener rules using the command line interface. The goal is to ensure that the priorities of existing rules start at N + 1, where N represents the current count of rules. By following this method, you create room for any new rules defined in `aws-cdk`. Once you have sufficient space available, you can define duplicates of existing rules using `aws-cdk` and subsequently deploy them. After successful deployment, manually created rules can be safely removed.

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

Create a single declaration in which you can assign values to multiple const variables

As a newcomer to react/JS, I have a question that may seem basic: I need multiple variables that are determined by a system variable. These variables should remain constant for each instance. Currently, my approach is functional but it feels incorrect to ...

Exploring the functionalities of React Native with react-hook-form and implementing them with TypeScript

I've been working on creating a custom Input component in react native using typescript for the react-hook-form library. type CustomInputProps = { name: any, control: any } const CustomInput: FC<CustomInputProps> = ({name, control, ...p ...

Typescript: Dynamic return type determined by argument properties

I have a function that should return two different types based on its argument props. interface IPaginateParams { perPage: number; currentPage: number; isFromStart?: boolean; } interface IWithPagination<Data, TParams extends IPaginateParams = IPa ...

Referencing other styled-components in Typescript and React code bases

I'm attempting to replicate this code snippet found on https://styled-components.com/docs/advanced using TypeScript. const Link = styled.a` display: flex; align-items: center; padding: 5px 10px; background: papayawhip; color: palevioletred; ...

What is the best way to present sorted items on a user interface?

I have a unique Med interface containing properties like drugClass, dosage, and name. Using this interface, I created an array of drugs with different attributes. How can I filter this array by drugClass and then display the filtered data on a web page? ...

Issue: The element p-button (and p-password) is unrecognized

After installing primeng using the command npm install primeng --save, I have version ^12.0.1 of primeng listed in both dependencies and devDependencies in my package.json file. In my angular.json file, I have included the necessary styles: "styles& ...

Contrasting the utilization of `typeof` with a constant and `enum` in TypeScript

Can you explain the distinction between using typeof with a constant and an enum in TypeScript? For example: const TYPE_A = 'a' const TYPE_B = 'b' type MyType = | typeof TYPE_A | typeof TYPE_B type Result = { name: string type ...

Error Message: Angular NullInjectorException - The provider for "[Object]" is not found

While developing a simple Flashcard application that performs CRUD operations using Angular, Node.js, Express, and PostgreSQL, I encountered the following error: flashcards-area.component.ts:24 ERROR NullInjectorError: R3InjectorError(AppModule)[Flashcard ...

Comparison of Amazon MWS Performance: Choosing Between XML and Flat File Uploads

Does using the MWS XML API result in better performance compared to uploading flat files when adding items to FBA inventory? My client requires immediate printing of an FNSKU once an item goes through our listing procedure. This procedure collects all nec ...

I'm looking for a sample of RadPieChart for nativescript + angular. Can anyone help me out?

I'm currently working on a multi-platform application that requires a PieChart to be displayed on the main screen. Can someone point me to a comprehensive example of how to implement this? I have tried following this guide and modifying it according ...

Concealing the sidebar in React with the help of Ant Design

I want to create a sidebar that can be hidden by clicking an icon in the navigation bar without using classes. Although I may be approaching this incorrectly, I prefer to keep it simple. The error message I encountered is: (property) collapsed: boolean ...

Dynamically generating an Angular component and populating it with data

I am currently working with Angular 7/8 and I have some code that adds a new component dynamically. In the parent component, my .ts file includes the following: PARENT COMPONENT Within the .ts file: @ViewChild(InjectDirective) injectComp: InjectDirect ...

A step-by-step guide to customizing the Material UI Chips delete SVG icon to appear in white color through

Using a Material UI component, I added a custom class to my chip. Attached is a screenshot showing what I mean. Currently, I am attempting to change the color of the cross button to white. After inspecting the element, I discovered that it is an SVG ico ...

"Can you share a method to extract the value from a TextField component in a React hook-based Material-

Currently, I am using Material-UI within a React project and have a component set up like this: const UserDetail = (props: ListDetailProps) => { const oldpassword = useRef<TextFieldProps>(null); const newpassword = useRef<TextFieldProps ...

How to retrieve a component's property within an Angular2 provider?

As a beginner in OOP, I am currently experimenting with Ionic 2, Angular 2, and TypeScript. In my home.html file, I have an input field connected to the username property in home.ts as shown below: export class HomePage { public username: string; public n ...

Getting blocked by CORS when trying to link an AWS React application to a Node.js API hosted on AWS

Seeking assistance with resolving CORS (Cross-Origin Resource Sharing) errors in my web application. Here are the crucial details of my setup: Front-End: React version 16.14. Back-End: Node.js version 16.19 with Express.js version 4.18.1. Server Instance: ...

Employing async await for postponing the execution of a code block

I have been working on an Angular component where I need to perform some actions after a data service method returns some data asynchronously. Although I attempted to use async/await in my code, I feel that I may not have fully understood how it works. Her ...

Utilize Tailwind CSS in React to dynamically highlight the active navigation item on click

Check out my navigation bar code: <nav className="bg-white shadow dark:bg-gray-800"> <div className="container flex items-center justify-center p-6 mx-auto text-gray-600 capitalize dark:text-gray-300"> <Link ...

Utilizing TypeScript to mandate properties in a React component

When trying to apply TypeScript type enforcement on React components, I encountered some unexpected behavior. Here are simplified examples: function FunctionalComponent(props: { color: string }) { return <></>; } type ComponentWithName<I ...

How to retrieve the value of a variable accessible to all users in Angular?

In my code, I am working with a service variable called Users. Service: Users:BehaviorSubject<Array<any>> = new BehaviorSubject([]); I am updating the values in the component using this code: this.Service.Users.next([...this.Service.User ...