0

Dose jemand irgendwelche Beispiele haben (Fenster bevorzugt) der folgendenCloudformation Wartezustand basierend auf In-Service hostet von der ELB

Zur Zeit habe ich eine CF-Vorlage, das ist meine AutoScaling Gruppen erstellt, LoadBalancer und einige Alarme auf gesund Hosts unter der ELB und arbeitet Rost,

Was ich tun möchte, ist eine Wartebedingung vor der Erstellung von Alarmen haben, so dass die Alarme erst erstellt werden, wenn die Hosts als "in-Service" markiert sind, aber die ELB.

Ich weiß, wie man einen Wartezustand erhalten jedoch arbeitet ich nicht herausfinden kann, wie man es auf „in Betrieb“ Status nach dem ELB zur Arbeit kommen.

Dose jemand irgendwelche Ratschläge oder Beispiele haben? Ich habe einige Beispiele, die die Bahn wurde Trolling aber abgesehen von den üblichen AWS Seiten der Dokumentation und ein paar anderen, ich habe nicht in der Lage gewesen zu finden, was ich brauche.

Ich habe einen Ausschnitt finden, wie die Gesundheit von einer Instanz bestätigen - aber ich kann nicht herausfinden, ob oder wie diese Bedingung in eine Wartezeit zu arbeiten.

verify_instance_health: 
commands: 
ELBHealthCheck: 
command: !Sub 
'until ; do state=$(aws --region ${AWS::Region} elb describe-instance-health 
--load-balancer-name ${ElasticLoadBalancer} 
--instances $(curl -s http://169.254.169.254/latest/meta-data/instance-id) 
--query InstanceStates[0].State); sleep 10; done' 

Prost

Antwort

0

Sie dies mit einem Custom Resource erreichen können, den die Instanz mit den DescribeInstanceHealth API überwacht, abgeschlossen, wenn die EC2-Instanz den InService Zustand (zum Beispiel unter Verwendung der instanceInService waiter vom AWS SDK for JavaScript) erreicht.

Hier ist ein vollständiges Beispiel, das cfn-init verwendet nginx auf einer EC2-Instanz zu konfigurieren, fügt einen ELB Gesundheits-Check auf TCP-Port 80 und eine InService benutzerdefinierte Ressource, die die EC2-Instanz ist InService in der ELB wartet, bis vor dem Stapel durchführen:

Launch Stack

Description: Wait until instance enters the InService state. 
Parameters: 
    ImageId: 
    Description: Image ID to launch EC2 instances. 
    Type: AWS::EC2::Image::Id 
    # amzn-ami-hvm-2016.09.1.20161221-x86_64-gp2 
    Default: ami-9be6f38c 
    InstanceType: 
    Description: Instance type to launch EC2 instances. 
    Type: String 
    Default: m3.medium 
    AllowedValues: [ m3.medium, m3.large, m3.xlarge, m3.2xlarge ] 
    AvailabilityZones: 
    Description: Availability Zones for ELB. 
    Type: List<AWS::EC2::AvailabilityZone::Name> 
Resources: 
    InstanceSecurityGroup: 
    Type: AWS::EC2::SecurityGroup 
    Properties: 
     GroupDescription: Allow inbound traffic from Load Balancer 
    SecurityGroupIngress: 
    Type: AWS::EC2::SecurityGroupIngress 
    Properties: 
     IpProtocol: tcp 
     FromPort: 80 
     ToPort: 80 
     GroupName: !Ref InstanceSecurityGroup 
     SourceSecurityGroupName: !GetAtt LoadBalancer.SourceSecurityGroup.GroupName 
     SourceSecurityGroupOwnerId: !GetAtt LoadBalancer.SourceSecurityGroup.OwnerAlias 
    Instance: 
    Type: AWS::EC2::Instance 
    Properties: 
     ImageId: !Ref ImageId 
     InstanceType: !Ref InstanceType 
     SecurityGroups: [!Ref InstanceSecurityGroup] 
     UserData: 
     "Fn::Base64": !Sub | 
      #!/bin/bash 
      /opt/aws/bin/cfn-init -v \ 
      --stack ${AWS::StackName} \ 
      --region ${AWS::Region} \ 
      --resource Instance 
    Metadata: 
     AWS::CloudFormation::Init: 
     config: 
      packages: {yum: {nginx: []}} 
      services: 
      sysvinit: 
       nginx: 
       enabled: true 
       ensureRunning: true 
       files: [/etc/nginx/nginx.conf] 
       sources: 
       - /usr/share/nginx/html 
       - /etc/nginx/conf.d 
       - /etc/nginx/default.d 
    LoadBalancer: 
    Type: AWS::ElasticLoadBalancing::LoadBalancer 
    Properties: 
     AvailabilityZones: !Ref AvailabilityZones 
     Listeners: 
     - LoadBalancerPort: 80 
     InstancePort: 80 
     Protocol: HTTP 
     Instances: [!Ref Instance] 
     HealthCheck: 
     Target: TCP:80 
     HealthyThreshold: 2 
     UnhealthyThreshold: 5 
     Interval: 5 
     Timeout: 2 
    InService: 
    Type: Custom::InService 
    Properties: 
     ServiceToken: !GetAtt InServiceFunction.Arn 
     Instances: 
     - InstanceId: !Ref Instance 
     LoadBalancerName: !Ref LoadBalancer 
    InServiceFunction: 
    Type: AWS::Lambda::Function 
    Properties: 
     Handler: index.handler 
     Role: !GetAtt LambdaExecutionRole.Arn 
     Code: 
     ZipFile: !Sub | 
      var response = require('cfn-response'); 
      var AWS = require('aws-sdk'); 
      exports.handler = (event, context) => { 
      console.log("Request received:\n", JSON.stringify(event)); 
      var physicalId = event.PhysicalResourceId || 'none'; 
      var success = data => response.send(event, context, response.SUCCESS, data, physicalId); 
      var failed = e => response.send(event, context, response.FAILED, e, physicalId); 
      if (event.RequestType == 'Create') { 
       var elb = new AWS.ELB(); 
       var elbParams = event.ResourceProperties; 
       delete elbParams.ServiceToken; 
       elb.waitFor('instanceInService', elbParams).promise(). 
       then((data)=> success({}), (e)=> failed(e)); 
      } else { 
       success({}); 
      } 
      }; 
     Runtime: nodejs4.3 
     Timeout: 300 
    LambdaExecutionRole: 
    Type: AWS::IAM::Role 
    Properties: 
     AssumeRolePolicyDocument: 
     Version: '2012-10-17' 
     Statement: 
     - Effect: Allow 
      Principal: {Service: [lambda.amazonaws.com]} 
      Action: ['sts:AssumeRole'] 
     Path:/
     ManagedPolicyArns: 
     - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole 
     Policies: 
     - PolicyName: ELBPolicy 
     PolicyDocument: 
      Version: '2012-10-17' 
      Statement: 
      - Effect: Allow 
       Action: 
       - 'elasticloadbalancing:DescribeInstanceHealth' 
       Resource: ['*'] 
Outputs: 
    URL: 
    Value: !Sub "http://${LoadBalancer.DNSName}"