Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I attach an EC2 instance to an existing Load Balancer using CloudFormation

The way we usually attach instances to a Load Balancer is by using the instances key while defining the load balancer in my aws template. Is there anyway that I can attach an EC2 instance to a Load Balancer that already exists?

like image 769
masterforker Avatar asked Feb 06 '17 02:02

masterforker


People also ask

Can we attach running EC2 instance to ASG?

Amazon EC2 Auto Scaling provides you with the option of attaching one or more EC2 instances to your existing Auto Scaling group. After an instance is attached, it is considered part of the Auto Scaling group.

Does Aw CloudFormation support Amazon EC2 tagging?

Q: Does AWS CloudFormation support Amazon EC2 tagging? Yes. Amazon EC2 resources that support the tagging feature can also be tagged in an AWS template.

What are the 2 ways to connect to EC2 instance?

To connect using the Amazon EC2 console, the instance must have a public IPv4 address. If the instance does not have a public IP address, you can connect to the instance over a private network using an SSH client or the EC2 Instance Connect CLI.


1 Answers

If you don't mind creating the EC2 instance indirectly through an AWS::AutoScaling::AutoScalingGroup (e.g., with a DesiredCapacity of 1), you could automatically attach the EC2 instance to an existing ELB using the LoadBalancerNames (or TargetGroupARNs if using an Application Load Balancer) property.

Otherwise, you can call the RegisterInstancesWithLoadBalancer / DeregisterInstancesFromLoadBalancer APIs manually, either directly from the EC2 instance (e.g., from a UserData script using the AWS CLI), or using a Custom Resource (e.g., using the AWS SDK for JavaScript from a Lambda function).

like image 90
wjordan Avatar answered Oct 13 '22 18:10

wjordan