Example usage for com.amazonaws.services.route53.model ResourceRecordSet setHealthCheckId

List of usage examples for com.amazonaws.services.route53.model ResourceRecordSet setHealthCheckId

Introduction

In this page you can find the example usage for com.amazonaws.services.route53.model ResourceRecordSet setHealthCheckId.

Prototype


public void setHealthCheckId(String healthCheckId) 

Source Link

Document

If you want Amazon Route 53 to return this resource record set in response to a DNS query only when the status of a health check is healthy, include the HealthCheckId element and specify the ID of the applicable health check.

Usage

From source file:com.deploymentio.ec2namer.helpers.DnsRegistrar.java

License:Apache License

protected Change createName(NamerRequest req, LambdaContext context, RequestedName additionalName,
        String assignedName) {/*from   www  . j  av  a 2s  .co m*/

    String fullAssignedName = req.createFqdn(assignedName);
    String fullAdditionalName = req.createFqdn(additionalName.getName());

    Instance instance = instanceLookup.lookup(context, req.getInstanceId());
    boolean usePublicHostname = req.isAlwaysUsePublicName() || StringUtils.isEmpty(instance.getVpcId());

    ResourceRecordSet set = new ResourceRecordSet(fullAdditionalName + ".", RRType.CNAME).withTTL(60l)
            .withResourceRecords(
                    new ResourceRecord(usePublicHostname ? instance.getPublicDnsName() : fullAssignedName))
            .withSetIdentifier(fullAssignedName).withWeight(Long.valueOf(additionalName.getWeight()));

    if (additionalName.isHealthChecked()) {

        String uuid = instance.getInstanceId() + "-" + UUID.randomUUID().toString();
        String hcId = null;

        for (int i = 0; i < 5; i++) {
            try {

                if (i > 0) {
                    Thread.sleep(5000);
                }

                CreateHealthCheckResult result = route53.createHealthCheck(
                        new CreateHealthCheckRequest().withCallerReference(uuid).withHealthCheckConfig(
                                new HealthCheckConfig().withFullyQualifiedDomainName(fullAssignedName)
                                        .withIPAddress(instance.getPublicIpAddress())
                                        .withPort(additionalName.getHealthCheckPort())
                                        .withType(additionalName.getHealthCheckType())
                                        .withResourcePath(additionalName.getHealthCheckUri())));

                hcId = result.getHealthCheck().getId();
                context.log("Created health-check: Name=" + fullAdditionalName + " Protocol="
                        + additionalName.getHealthCheckType() + " Port=" + additionalName.getHealthCheckPort()
                        + " Uri=" + additionalName.getHealthCheckUri() + " Id=" + hcId);

                // create tags on health-check
                route53.changeTagsForResource(new ChangeTagsForResourceRequest().withResourceId(hcId)
                        .withResourceType(TagResourceType.Healthcheck).withAddTags(
                                new Tag().withKey("Name").withValue(req.getEnvironment() + ":" + assignedName),
                                new Tag().withKey("InstanceId").withValue(req.getInstanceId())));

                break;

            } catch (Exception e) {
                context.log("Can't create health-check: Attempt= " + (i + 1) + " Name=" + fullAdditionalName
                        + " Protocol=" + additionalName.getHealthCheckType() + " Port="
                        + additionalName.getHealthCheckPort() + " Uri=" + additionalName.getHealthCheckUri()
                        + " Id=" + hcId + " Ref=" + uuid + " Error=" + e.getMessage());
            }
        }

        if (!StringUtils.isEmpty(hcId)) {
            set.setHealthCheckId(hcId);
        }
    }

    return new Change(ChangeAction.UPSERT, set);
}