public static void SetRoutes(FargateResourceV2 resource, FargateResourceV2 resourceNew, ELBHelper elb, Route53Helper r53, CloudWatchHelper cw) { Console.WriteLine("Fetching DNS Private Record..."); var newPrivateRecord = r53.GetCNameRecordSet(resourceNew.ZonePrivate, resourceNew.DNSCName, throwIfNotFound: true) .Result.PrintResponse(); Console.WriteLine($"Upserting Health Check..."); var healthCheck = r53.UpsertCloudWatchHealthCheckAsync( resource.HealthCheckName, alarmName: resourceNew.ELBHealthyMetricAlarmName, alarmRegion: resource.Region, throwIfNotFound: false, insufficientDataHealthStatus: Amazon.Route53.InsufficientDataHealthStatus.Healthy, inverted: false ).Result.PrintResponse(); ResourceRecordSet newPublicRecord = null; Console.WriteLine("Updating Private Route53 Record Set (PRIMARY)..."); var t1 = r53.UpsertCNameRecordAsync(resource.ZonePrivate, resource.DNSCName, newPrivateRecord.ResourceRecords.Single().Value, ttl: resourceNew.TTL, failover: "PRIMARY", setIdentifier: "PRIMARY-PRIVATE", healthCheckId: healthCheck.Id); Console.WriteLine("Updating Private Route53 Record Set (SECONDARY)..."); var t2 = r53.UpsertCNameRecordAsync(resourceNew.ZonePrivate, resource.DNSCName, newPrivateRecord.ResourceRecords.Single().Value, ttl: resourceNew.TTL, failover: "SECONDARY", setIdentifier: "SECONDARY-PRIVATE"); if (resourceNew.IsPublic) { Console.WriteLine("Fetching DNS Public Record..."); newPublicRecord = r53.GetCNameRecordSet(resourceNew.ZonePublic, resourceNew.DNSCName, throwIfNotFound: resourceNew.IsPublic) .Result.PrintResponse(); Console.WriteLine("Updating Public Route53 Record Set (PRIMARY)..."); var t3 = r53.UpsertCNameRecordAsync(resource.ZonePublic, resource.DNSCName, newPublicRecord.ResourceRecords.Single().Value, ttl: resourceNew.TTL, failover: "PRIMARY", setIdentifier: "PRIMARY-PUBLIC", healthCheckId: healthCheck.Id); Console.WriteLine("Updating Public Route53 Record Set (SECONDARY)..."); var t4 = r53.UpsertCNameRecordAsync(resourceNew.ZonePublic, resource.DNSCName, newPublicRecord.ResourceRecords.Single().Value, ttl: resourceNew.TTL, failover: "SECONDARY", setIdentifier: "SECONDARY-PUBLIC"); Task.WhenAll(t1, t3); Task.WhenAll(t2, t4); } t1.Await(); t2.Await(); Console.WriteLine($"Awaiting {(resource.TTL + 1) * 2} [s] for DNS route update based on TTL..."); Thread.Sleep((resource.TTL + 1) * 2 * 1000); Console.WriteLine($"Awaiting Health Check Healthy Status..."); r53.WaitForHealthCheckAsync(name: healthCheck.Id, status: Route53Helper.HealthCheckStatus.Healthy, timeout_s: resource.HealthCheckTimeout).Result.PrintResponse(); Console.WriteLine($"Ensuring DNS Route53 Resolution into new address after Health Check change..."); AwaitDnsUpsert(resource, resourceNew, r53, resource.DnsResolveTimeout).PrintResponse(); }
public static void SwapRoutes(FargateResourceV2 resource, FargateResourceV2 resourceNew, ELBHelper elb, Route53Helper r53, CloudWatchHelper cw) { Console.WriteLine("Fetching DNS Private Record..."); var newPrivateRecord = r53.GetCNameRecordSet(resourceNew.ZonePrivate, resourceNew.DNSCName, throwIfNotFound: true) .Result.PrintResponse(); ResourceRecordSet newPublicRecord = null; Console.WriteLine("Updating Private Route53 Record Set (SECONDARY)..."); r53.UpsertCNameRecordAsync(resourceNew.ZonePrivate, resource.DNSCName, newPrivateRecord.ResourceRecords.Single().Value, ttl: resourceNew.TTL, failover: "SECONDARY", setIdentifier: "SECONDARY-PRIVATE").Result.PrintResponse(); if (resourceNew.IsPublic) { Console.WriteLine("Fetching DNS Public Record..."); newPublicRecord = r53.GetCNameRecordSet(resourceNew.ZonePublic, resourceNew.DNSCName, throwIfNotFound: resourceNew.IsPublic) .Result.PrintResponse(); Console.WriteLine("Updating Public Route53 Record Set (SECONDARY)..."); r53.UpsertCNameRecordAsync(resourceNew.ZonePublic, resource.DNSCName, newPublicRecord.ResourceRecords.Single().Value, ttl: resourceNew.TTL, failover: "SECONDARY", setIdentifier: "SECONDARY-PUBLIC").Result.PrintResponse(); } var recordsExist = false; ResourceRecordSet rrsPrivate = null; ResourceRecordSet rrsPublic = null; HealthCheck healthCheck = null; Console.WriteLine($"Veryfying PRIMARY Record and Health Check..."); if ((rrsPrivate = r53.GetCNameRecordSet(resource.ZonePrivate, resource.DNSCName, failover: "PRIMARY", throwIfNotFound: false).Result) != null && (healthCheck = r53.GetHealthCheckAsync(rrsPrivate.HealthCheckId, throwIfNotFound: false).Result) != null) { if (resource.IsPublic && (rrsPublic = r53.GetCNameRecordSet(resource.ZonePublic, resource.DNSCName, failover: "PRIMARY", throwIfNotFound: false).Result) != null && rrsPublic.HealthCheckId == healthCheck.Id) { recordsExist = true; } else { recordsExist = true; } } Console.WriteLine($"DNS PRIMARY Record and Health Check were {(recordsExist ? "" : "NOT ")}present."); Console.WriteLine($"Awaiting Desired State of the Cloud Watch Metric Alarm '{resourceNew.ELBHealthyMetricAlarmName}'..."); cw.WaitForMetricState(resourceNew.ELBHealthyMetricAlarmName, Amazon.CloudWatch.StateValue.OK, resource.HealthCheckTimeout) .Result.PrintResponse(); Console.WriteLine($"Upserting Health Check..."); healthCheck = r53.UpsertCloudWatchHealthCheckAsync( healthCheck?.Id ?? resource.HealthCheckName, alarmName: resourceNew.ELBHealthyMetricAlarmName, alarmRegion: resource.Region, throwIfNotFound: false, insufficientDataHealthStatus: Amazon.Route53.InsufficientDataHealthStatus.Healthy, inverted: true //OK as long as old health check is failed ).Result.PrintResponse(); if (recordsExist) { Console.WriteLine($"Awaiting Health Check Unhealthy Status..."); r53.WaitForHealthCheckAsync(name: healthCheck.Id, status: Route53Helper.HealthCheckStatus.Unhealthy, timeout_s: resource.HealthCheckTimeout) .Result.PrintResponse(); Console.WriteLine($"Awaiting DNS Route53 Resolution into new address..."); AwaitDnsUpsert(resource, resourceNew, r53, resource.DnsResolveTimeout).PrintResponse(); Console.WriteLine($"Awaiting {(resource.TTL + 1) * 2} [s] for DNS route update based on TTL..."); Thread.Sleep((resource.TTL + 1) * 2 * 1000); } Console.WriteLine("Updating Private Route53 Record Set (PRIMARY)..."); r53.UpsertCNameRecordAsync(resource.ZonePrivate, resource.DNSCName, newPrivateRecord.ResourceRecords.Single().Value, ttl: resourceNew.TTL, failover: "PRIMARY", setIdentifier: "PRIMARY-PRIVATE", healthCheckId: healthCheck.Id).Result.PrintResponse(); if (resource.IsPublic) { Console.WriteLine("Updating Public Route53 Record Set (PRIMARY)..."); r53.UpsertCNameRecordAsync(resource.ZonePublic, resource.DNSCName, newPublicRecord.ResourceRecords.Single().Value, ttl: resourceNew.TTL, failover: "PRIMARY", setIdentifier: "PRIMARY-PUBLIC", healthCheckId: healthCheck.Id).Result.PrintResponse(); } Console.WriteLine($"Awaiting {resourceNew.DnsUpdateDelay} [ms] for PRIMARY DNS Record update..."); Thread.Sleep(resourceNew.DnsUpdateDelay); Console.WriteLine($"Upserting Health Check..."); healthCheck = r53.UpsertCloudWatchHealthCheckAsync( healthCheck?.Id, alarmName: resourceNew.ELBHealthyMetricAlarmName, alarmRegion: resource.Region, throwIfNotFound: false, insufficientDataHealthStatus: Amazon.Route53.InsufficientDataHealthStatus.Unhealthy, inverted: false ).Result.PrintResponse(); Console.WriteLine($"Awaiting Health Check Healthy Status..."); r53.WaitForHealthCheckAsync(name: healthCheck.Id, status: Route53Helper.HealthCheckStatus.Healthy, timeout_s: 240).Result.PrintResponse(); Console.WriteLine($"Awaiting {(resource.TTL+1)*2} [s] for DNS route update based on TTL..."); Thread.Sleep((resource.TTL + 1) * 2 * 1000); Console.WriteLine($"Ensuring DNS Route53 Resolution into new address after Health Check change..."); AwaitDnsUpsert(resource, resourceNew, r53, resource.DnsResolveTimeout).PrintResponse(); }