4

J'ai donc un modèle AWS CloudFormation avec 3 'types' d'instance différents (Serveur, Agent, Relais)Comment obtenir tous les ID d'instance EC2 de AutoScaling?

J'utilise AutoScaling pour lancer dynamiquement X nombre d'instances d'un type.

Mon problème est que j'ai besoin de toutes les adresses IP de ces serveurs à partir des sorties du modèle, de préférence triées en sections.

à savoir

Serveurs: x.x.x.x y.y.y.y

Relais: z.z.z.z

Agents: a.a.a.a

Comment puis-je obtenir que l'instance Ids des sorties? (Je peux obtenir les adresses IP à partir de l'ID)

modèle ci-joint:

{ 
"AWSTemplateFormatVersion" : "2010-09-09", 

"Description" : "uDeploy Agent-Relay-Server", 

"Parameters" : { 
    "keyName" : { 
     "Description" : "SSH key to enable access on the servers", 
     "Type" : "String", 
     "Default" : "nick-portal" 
    }, 

    "ServerInstanceCount" : { 
     "Description" : "Number of Servers to start", 
     "Type" : "Number", 
     "Default" : "1" 
    }, 
    "RelayInstanceCount" : { 
     "Description" : "Number of Agent Relays to start", 
     "Type" : "Number", 
     "Default" : "2" 
    }, 
    "AgentInstanceCount" : { 
     "Description" : "Number of Agents to start", 
     "Type" : "Number", 
     "Default" : "4" 
    }, 

    "ServerAMI" : { 
     "Description" : "", 
     "Type" : "String", 
     "Default" : "ami-7539b41c" 
    }, 
    "RelayAMI" : { 
     "Description" : "", 
     "Type" : "String", 
     "Default" : "ami-7539b41c" 
    }, 
    "AgentAMI" : { 
     "Description" : "", 
     "Type" : "String", 
     "Default" : "ami-7539b41c" 
    }, 

    "ServerUserData" : { 
     "Description" : "", 
     "Type" : "String", 
     "Default" : "#!/bin/bash" 
    }, 
    "RelayUserData" : { 
     "Description" : "", 
     "Type" : "String", 
     "Default" : "#!/bin/bash" 
    }, 
    "AgentUserData" : { 
     "Description" : "", 
     "Type" : "String", 
     "Default" : "#!/bin/bash" 
    }, 
    "Zone" : { 
     "Description" : "", 
     "Type" : "String", 
     "Default" : "us-east-1d" 
    } 
}, 

"Resources" : { 
    "ServerLaunchConfig" : { 
     "Type" : "AWS::AutoScaling::LaunchConfiguration", 
     "Properties" : { 
      "KeyName" : { "Ref" : "keyName" }, 
      "ImageId" : { "Ref" : "ServerAMI" }, 
      "UserData" : { "Fn::Base64" : { "Ref" : "ServerUserData" } }, 
      "SecurityGroups" : [ { "Ref" : "ServerSecurityGroup" }, { "Ref" : "SshSecurityGroup" } ], 
      "InstanceType" : "t1.micro" 
     } 
    }, 
    "RelayLaunchConfig" : { 
     "Type" : "AWS::AutoScaling::LaunchConfiguration", 
     "Properties" : { 
      "KeyName" : { "Ref" : "keyName" }, 
      "ImageId" : { "Ref" : "RelayAMI" }, 
      "UserData" : { "Fn::Base64" : { "Ref" : "RelayUserData" } }, 
      "SecurityGroups" : [ { "Ref" : "RelaySecurityGroup" }, { "Ref" : "SshSecurityGroup" } ], 
      "InstanceType" : "t1.micro" 
     } 
    }, 
    "AgentLaunchConfig" : { 
     "Type" : "AWS::AutoScaling::LaunchConfiguration", 
     "Properties" : { 
      "KeyName" : { "Ref" : "keyName" }, 
      "ImageId" : { "Ref" : "AgentAMI" }, 
      "UserData" : { "Fn::Base64" : { "Ref" : "AgentUserData" } }, 
      "SecurityGroups" : [ { "Ref" : "AgentSecurityGroup" }, { "Ref" : "SshSecurityGroup" } ], 
      "InstanceType" : "t1.micro" 
     } 
    }, 


    "ServerAutoScalingGroup" : { 
     "Type" : "AWS::AutoScaling::AutoScalingGroup", 
     "Properties" : { 
      "AvailabilityZones" : [ { "Ref" : "Zone" } ], 
      "LaunchConfigurationName" : { "Ref" : "ServerLaunchConfig" }, 
      "MinSize" : { "Ref" : "ServerInstanceCount" }, 
      "MaxSize" : { "Ref" : "ServerInstanceCount" } 
     } 
    }, 
    "RelayAutoScalingGroup" : { 
     "Type" : "AWS::AutoScaling::AutoScalingGroup", 
     "Properties" : { 
      "AvailabilityZones" : [ { "Ref" : "Zone" } ], 
      "LaunchConfigurationName" : { "Ref" : "RelayLaunchConfig" }, 
      "MinSize" : { "Ref" : "RelayInstanceCount" }, 
      "MaxSize" : { "Ref" : "RelayInstanceCount" } 
     } 
    }, 
    "AgentAutoScalingGroup" : { 
     "Type" : "AWS::AutoScaling::AutoScalingGroup", 
     "Properties" : { 
      "AvailabilityZones" : [ { "Ref" : "Zone" } ], 
      "LaunchConfigurationName" : { "Ref" : "AgentLaunchConfig" }, 
      "MinSize" : { "Ref" : "AgentInstanceCount" }, 
      "MaxSize" : { "Ref" : "AgentInstanceCount" } 
     } 
    }, 

    "RelaySecurityGroup" : { 
     "Type" : "AWS::EC2::SecurityGroup", 
     "Properties" : { 
      "GroupDescription" : "Enable inbound 20080 and 7916 from Agents", 
      "SecurityGroupIngress" : 
      [ 
       { 
        "IpProtocol" : "tcp", 
        "FromPort" : "20080", 
        "ToPort" : "20080", 
        "SourceSecurityGroupName" : { "Ref" : "AgentSecurityGroup" } 
       }, 
       { 
        "IpProtocol" : "tcp", 
        "FromPort" : "7916", 
        "ToPort" : "7916", 
        "SourceSecurityGroupName" : { "Ref" : "AgentSecurityGroup" } 
       } 
      ] 
     } 
    }, 
    "ServerSecurityGroup" : { 
     "Type" : "AWS::EC2::SecurityGroup", 
     "Properties" : { 
      "GroupDescription" : "Enable inbound 8080 all and 7918 from Relays", 
      "SecurityGroupIngress" : [ 
       { 
        "IpProtocol" : "tcp", 
        "FromPort" : "7918", 
        "ToPort" : "7918", 
        "SourceSecurityGroupName" : { "Ref" : "RelaySecurityGroup" } 
       }, 
       { 
        "IpProtocol" : "tcp", 
        "FromPort" : "8080", 
        "ToPort" : "8080", 
        "CidrIp" : "0.0.0.0/0" 
       } 
      ] 
     } 
    }, 
    "AgentSecurityGroup" : { 
     "Type" : "AWS::EC2::SecurityGroup", 
     "Properties" : { 
      "GroupDescription" : "Enable no inbound", 
      "SecurityGroupIngress" : [] 
     } 
    }, 
    "SshSecurityGroup" : { 
     "Type" : "AWS::EC2::SecurityGroup", 
     "Properties" : { 
      "GroupDescription" : "Enable SSH from all", 
      "SecurityGroupIngress" : [ 
       { 
        "IpProtocol" : "tcp", 
        "FromPort" : "22", 
        "ToPort" : "22", 
        "CidrIp" : "0.0.0.0/0" 
       } 
      ] 
     } 
    } 


}, 

"Outputs" : { 
    "Ip" 
} 
} 

Répondre

3

Non, vous ne pouvez pas définir les sorties comme les ips. La formation de cloud est responsable des groupes de mise à l'échelle automatique et de la configuration de lancement à mise à l'échelle automatique, mais elle ne contrôle pas les instances EC2 individuelles, vous ne pouvez donc pas obtenir d'informations sur ces sorties.

Vous pouvez écrire quelque chose qui s'exécute sur les instances EC2 au démarrage pour définir une étiquette sur la pile avec les valeurs ip. Mais cela pourrait être difficile à maintenir lorsque les instances se terminent.

3

Sur bash et en utilisant l'utilitaire AWS CLI vous pouvez effectuer les opérations suivantes:

#!/bin/bash 
AUTOSCALING_GROUP="mygroup" 
aws ec2 describe-instances --filters "Name=tag:aws:autoscaling:groupName,Values=$AUTOSCALING_GROUP" "Name=instance-state-name,Values=running" | grep -o '\"i-[0-9a-f]\\+\"' | grep -o '[^\"]\\+' 

Cela affichera les ID d'instance de toutes les machines du groupe de mise à l'échelle automatique « mygroup », un par ligne.

Questions connexes