-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/aws: Add aws_availability_zones source
This commit adds a data source with a single set, `available` for the schema which gets populated with the availability zones to which an account has access.
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceAwsAvailabilityZones() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsAvailabilityZonesRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"available": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: schema.HashString, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsAvailabilityZonesRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
log.Printf("[DEBUG] Reading availability zones") | ||
d.SetId(time.Now().UTC().String()) | ||
|
||
req := &ec2.DescribeAvailabilityZonesInput{DryRun: aws.Bool(false)} | ||
azresp, err := conn.DescribeAvailabilityZones(req) | ||
if err != nil { | ||
return fmt.Errorf("Error listing availability zones: %s", err) | ||
} | ||
|
||
raw := schema.NewSet(schema.HashString, nil) | ||
for _, v := range azresp.AvailabilityZones { | ||
raw.Add(*v.ZoneName) | ||
} | ||
|
||
if err := d.Set("available", raw); err != nil { | ||
return fmt.Errorf("[WARN] Error setting availability zones") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
website/source/docs/providers/aws/d/availability_zones.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_availability_zones" | ||
sidebar_current: "docs-aws-datasource-availability-zones" | ||
description: |- | ||
Provides a list of availability zones which can be used by an AWS account | ||
--- | ||
|
||
# aws\_availability_zones | ||
|
||
The Availability Zones data source allows access to the list of AWS | ||
Availability Zones which can be accessed by an AWS account within the region | ||
configured in the provider. | ||
|
||
## Example Usage | ||
|
||
``` | ||
# Declare the data source | ||
data "aws_availability_zones" "zones" {} | ||
# Create a subnet in each availability zone | ||
resource "aws_subnet" "public" { | ||
count = "${length(data.aws_availability_zones.zones.available)}" | ||
availability_zone = "${data.aws_availability_zones.zones.available[count.index]}" | ||
# Other properties... | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
There are no arguments for this data source. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `available` - A list of the availability zone names available to the account. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters