ecsAvailabilityZone queries the task endpoint for the metadata URI that ECS injects into the ECS_CONTAINER_METADATA_URI variable in order to retrieve the availability zone where the task is running.
()
| 74 | // injects into the ECS_CONTAINER_METADATA_URI variable in order to retrieve |
| 75 | // the availability zone where the task is running. |
| 76 | func ecsAvailabilityZone() string { |
| 77 | client := http.Client{ |
| 78 | Timeout: time.Second, |
| 79 | Transport: &http.Transport{ |
| 80 | DisableCompression: true, |
| 81 | DisableKeepAlives: true, |
| 82 | }, |
| 83 | } |
| 84 | r, err := client.Get(os.Getenv(ecsContainerMetadataURI) + "/task") |
| 85 | if err != nil { |
| 86 | return "" |
| 87 | } |
| 88 | defer r.Body.Close() |
| 89 | |
| 90 | var md struct { |
| 91 | AvailabilityZone string |
| 92 | } |
| 93 | if err := json.NewDecoder(r.Body).Decode(&md); err != nil { |
| 94 | return "" |
| 95 | } |
| 96 | return md.AvailabilityZone |
| 97 | } |
| 98 | |
| 99 | // ec2AvailabilityZone queries the metadata endpoint to discover the |
| 100 | // availability zone where this code is running. we avoid calling this function |