I use AWS::ELBv2 with set of rules which redirect requests to different services running on several tcp ports on my EC2 instance. My application doesn't support scaling, so I can't use autoscaling group, I need just ELBv2 attached to my ec2-instance. We are using CloudFormation for deployment automation purposes.
With Autoscaling I can use TargetGroupARNs property of AutoscalingCluster:
"AutoScalingCluster": {
"Type": "AWS::AutoScaling::AutoScalingGroup",
"Properties": {
"TargetGroupARNs": [
{
"Ref": "FirstappTargetGroup"
},
{
"Ref": "SecondappTargetGroup"
}
],
...
}
However for AWS::EC2::Instance there is no such property (according to https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html) and also there are no properties related to Target Groups.
It is possible to register nodes in Target Group after stack is created, however that's additional step which might be not convenient.
Found solution here: https://github.com/getcft/aws-elb-to-ec2-target-group-cf-template/blob/master/elb-to-ec2-target-group-cf-template.yml#L338
You can specify ec2 nodes as Targets: in AWS::ElasticLoadBalancingV2::TargetGroup
"MyTargetGroup": {
"Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
"Properties": {
"Port": 80,
"Protocol": "HTTP",
"VpcId": {
"Ref": "VpcId"
},
"Targets": [
{
"Id": {
"Ref": "MyEC2Node"
}
}
]
}
}
So it's not EC2 node property, it's a TargetGroup property in this case.
Also aws api doesn't show that property via aws elbv2 describe-target-groups