patternMinor
is it possible to connect an AWS autoscaling group with an application load balancer?
Viewed 0 times
connectgroupapplicationwithpossibleawsloadbalancerautoscaling
Problem
I'm working with AWS cloudformation I have an autoscaling group for a couple of machines running an API server, and I would like to set up an application load balancer as a single entry point, so that client application only sees one URL. I was able to create the load balancer and the autoscaling group but I don't know how to connect them.
```
Resources:
NodeLaunchConfig:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
AssociatePublicIpAddress: false
# IamInstanceProfile: !Ref NodeInstanceProfile
ImageId: !Ref NodeImageId
InstanceType: !Ref NodeInstanceType
KeyName: !Ref KeyName
# SecurityGroups is like this on purpuse, this work, IGNORE the error message if any
SecurityGroups: !Ref NodeSecurityGroup
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeSize: 200
VolumeType: gp2
DeleteOnTermination: true
NodeGroup:
Type: AWS::AutoScaling::AutoScalingGroup
DependsOn: LoadBalancer
Properties:
LaunchConfigurationName: !Ref NodeLaunchConfig
DesiredCapacity: "1"
MinSize: "0"
MaxSize: "10"
# TargetGroupARNs:
# - !Ref LoadBalancer
TerminationPolicies:
- NewestInstance
# VPCZoneIdentifier is like this on purpuse, this work, IGNORE the error message if any
VPCZoneIdentifier: !Ref Subnets
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}-node
PropagateAtLaunch: true
- Key: Owner
Value: !Ref Owner
PropagateAtLaunch: true
- Key: Department
Value: AI
PropagateAtLaunch: true
- Key: Env
Value: !Ref Environment
PropagateAtLaunch: true
- Key: Job
Value: KubernetesGroupNode
PropagateAtLaunch: true
UpdatePolicy:
AutoScalingRollingUpdate:
MaxBatchSize: 1
MinInstancesInService: 1
PauseTime: PT5M
LoadBalancer:
```
Resources:
NodeLaunchConfig:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
AssociatePublicIpAddress: false
# IamInstanceProfile: !Ref NodeInstanceProfile
ImageId: !Ref NodeImageId
InstanceType: !Ref NodeInstanceType
KeyName: !Ref KeyName
# SecurityGroups is like this on purpuse, this work, IGNORE the error message if any
SecurityGroups: !Ref NodeSecurityGroup
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeSize: 200
VolumeType: gp2
DeleteOnTermination: true
NodeGroup:
Type: AWS::AutoScaling::AutoScalingGroup
DependsOn: LoadBalancer
Properties:
LaunchConfigurationName: !Ref NodeLaunchConfig
DesiredCapacity: "1"
MinSize: "0"
MaxSize: "10"
# TargetGroupARNs:
# - !Ref LoadBalancer
TerminationPolicies:
- NewestInstance
# VPCZoneIdentifier is like this on purpuse, this work, IGNORE the error message if any
VPCZoneIdentifier: !Ref Subnets
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}-node
PropagateAtLaunch: true
- Key: Owner
Value: !Ref Owner
PropagateAtLaunch: true
- Key: Department
Value: AI
PropagateAtLaunch: true
- Key: Env
Value: !Ref Environment
PropagateAtLaunch: true
- Key: Job
Value: KubernetesGroupNode
PropagateAtLaunch: true
UpdatePolicy:
AutoScalingRollingUpdate:
MaxBatchSize: 1
MinInstancesInService: 1
PauseTime: PT5M
LoadBalancer:
Solution
Your CloudFormation template currently has the following resources:
Follow these steps in sequence to attach the auto scaling group to the load balancer:
-
Add a listener resource to your CloudFormation template, as described here. Set its
-
Add a target group resource to your CloudFormation template, as described here.
-
Add a "listener rule" resource to your CloudFormation template, as described here. Set its
If you prefer to visualize a chain of CloudFormation resources linking the load balancer to the auto scaling group, it'll look like this:
Here's a complete example: LB to ASG Example.
- A Launch Configuration
- An Auto Scaling Group
- A Load Balancer
Follow these steps in sequence to attach the auto scaling group to the load balancer:
-
Add a listener resource to your CloudFormation template, as described here. Set its
LoadBalancerArn property to the load balancer's ARN.-
Add a target group resource to your CloudFormation template, as described here.
-
Add a "listener rule" resource to your CloudFormation template, as described here. Set its
ListenerArn property to the listener's ARN. In its Actions property, add a forward action & provide the target group's ARN like this:Actions:
- Type: forward
TargetGroupArn: !Ref MyTargetGroup- Add the target group's ARN to the
TargetGroupARNsproperty of the auto scaling group, as described here.
If you prefer to visualize a chain of CloudFormation resources linking the load balancer to the auto scaling group, it'll look like this:
Here's a complete example: LB to ASG Example.
Code Snippets
Actions:
- Type: forward
TargetGroupArn: !Ref MyTargetGroupContext
StackExchange DevOps Q#11653, answer score: 5
Revisions (0)
No revisions yet.