From 1b9cd627afcf64877c4726cdf39433d7e1f6c9c9 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Thu, 16 Aug 2018 03:00:09 -0400 Subject: [PATCH] data-source/aws_ami and data-source/aws_ami_ids: Require owners argument * data-source/aws_ami: Switch owners argument from Optional to Required * data-source/aws_ami_ids: Switch owners argument from Optional to Required * tests: Update aws_ami data sources to use owners instead of filter > name = "owner-alias" make testacc TEST=./aws TESTARGS='-run=TestAcc\(AWSAmiDataSource\|DataSourceAwsAmiIds\)_' ==> Checking that code complies with gofmt requirements... TF_ACC=1 go test ./aws -v -run=TestAcc\(AWSAmiDataSource\|DataSourceAwsAmiIds\)_ -timeout 120m === RUN TestAccDataSourceAwsAmiIds_basic --- PASS: TestAccDataSourceAwsAmiIds_basic (11.55s) === RUN TestAccDataSourceAwsAmiIds_sorted --- PASS: TestAccDataSourceAwsAmiIds_sorted (251.95s) === RUN TestAccAWSAmiDataSource_natInstance --- PASS: TestAccAWSAmiDataSource_natInstance (11.88s) === RUN TestAccAWSAmiDataSource_windowsInstance --- PASS: TestAccAWSAmiDataSource_windowsInstance (22.03s) === RUN TestAccAWSAmiDataSource_instanceStore --- PASS: TestAccAWSAmiDataSource_instanceStore (9.35s) === RUN TestAccAWSAmiDataSource_localNameFilter --- PASS: TestAccAWSAmiDataSource_localNameFilter (16.45s) PASS ok github.com/terraform-providers/terraform-provider-aws/aws 323.842s --- aws/data_source_aws_ami.go | 58 ++----- aws/data_source_aws_ami_ids.go | 59 ++----- aws/data_source_aws_ami_ids_test.go | 25 --- aws/data_source_aws_ami_test.go | 86 +--------- ...data_source_aws_autoscaling_groups_test.go | 12 +- aws/data_source_aws_instance_test.go | 6 +- aws/resource_aws_autoscaling_group_test.go | 162 +++--------------- aws/resource_aws_eip_test.go | 48 ++---- aws/resource_aws_instance_test.go | 6 +- aws/resource_aws_launch_template_test.go | 12 +- ...resource_aws_spot_instance_request_test.go | 6 +- ...esource_aws_storagegateway_gateway_test.go | 18 +- website/docs/d/ami.html.markdown | 30 ++-- website/docs/d/ami_ids.html.markdown | 10 +- 14 files changed, 101 insertions(+), 437 deletions(-) diff --git a/aws/data_source_aws_ami.go b/aws/data_source_aws_ami.go index e764e6e813e9..f63f550b484e 100644 --- a/aws/data_source_aws_ami.go +++ b/aws/data_source_aws_ami.go @@ -6,7 +6,6 @@ import ( "log" "regexp" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" @@ -39,9 +38,12 @@ func dataSourceAwsAmi() *schema.Resource { }, "owners": { Type: schema.TypeList, - Optional: true, - ForceNew: true, - Elem: &schema.Schema{Type: schema.TypeString}, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.NoZeroValues, + }, }, // Computed values. "architecture": { @@ -180,49 +182,15 @@ func dataSourceAwsAmi() *schema.Resource { func dataSourceAwsAmiRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn - executableUsers, executableUsersOk := d.GetOk("executable_users") - filters, filtersOk := d.GetOk("filter") - nameRegex, nameRegexOk := d.GetOk("name_regex") - owners, ownersOk := d.GetOk("owners") - - if !executableUsersOk && !filtersOk && !nameRegexOk && !ownersOk { - return fmt.Errorf("One of executable_users, filters, name_regex, or owners must be assigned") - } - - params := &ec2.DescribeImagesInput{} - if executableUsersOk { - params.ExecutableUsers = expandStringList(executableUsers.([]interface{})) - } - if filtersOk { - params.Filters = buildAwsDataSourceFilters(filters.(*schema.Set)) + params := &ec2.DescribeImagesInput{ + Owners: expandStringList(d.Get("owners").([]interface{})), } - if ownersOk { - o := expandStringList(owners.([]interface{})) - if len(o) > 0 { - params.Owners = o - } + if v, ok := d.GetOk("executable_users"); ok { + params.ExecutableUsers = expandStringList(v.([]interface{})) } - - // Deprecated: pre-2.0.0 warning logging - if !ownersOk { - log.Print("[WARN] The \"owners\" argument will become required in the next major version.") - log.Print("[WARN] Documentation can be found at: https://www.terraform.io/docs/providers/aws/d/ami.html#owners") - - missingOwnerFilter := true - - if filtersOk { - for _, filter := range params.Filters { - if aws.StringValue(filter.Name) == "owner-alias" || aws.StringValue(filter.Name) == "owner-id" { - missingOwnerFilter = false - break - } - } - } - - if missingOwnerFilter { - log.Print("[WARN] Potential security issue: missing \"owners\" filtering for AMI. Check AMI to ensure it came from trusted source.") - } + if v, ok := d.GetOk("filter"); ok { + params.Filters = buildAwsDataSourceFilters(v.(*schema.Set)) } log.Printf("[DEBUG] Reading AMI: %s", params) @@ -232,7 +200,7 @@ func dataSourceAwsAmiRead(d *schema.ResourceData, meta interface{}) error { } var filteredImages []*ec2.Image - if nameRegexOk { + if nameRegex, ok := d.GetOk("name_regex"); ok { r := regexp.MustCompile(nameRegex.(string)) for _, image := range resp.Images { // Check for a very rare case where the response would include no diff --git a/aws/data_source_aws_ami_ids.go b/aws/data_source_aws_ami_ids.go index e7e91d3a85a7..be02f74b7950 100644 --- a/aws/data_source_aws_ami_ids.go +++ b/aws/data_source_aws_ami_ids.go @@ -5,7 +5,6 @@ import ( "log" "regexp" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" @@ -32,9 +31,12 @@ func dataSourceAwsAmiIds() *schema.Resource { }, "owners": { Type: schema.TypeList, - Optional: true, - ForceNew: true, - Elem: &schema.Schema{Type: schema.TypeString}, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.NoZeroValues, + }, }, "ids": { Type: schema.TypeList, @@ -48,50 +50,15 @@ func dataSourceAwsAmiIds() *schema.Resource { func dataSourceAwsAmiIdsRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn - executableUsers, executableUsersOk := d.GetOk("executable_users") - filters, filtersOk := d.GetOk("filter") - nameRegex, nameRegexOk := d.GetOk("name_regex") - owners, ownersOk := d.GetOk("owners") - - if executableUsersOk == false && filtersOk == false && nameRegexOk == false && ownersOk == false { - return fmt.Errorf("One of executable_users, filters, name_regex, or owners must be assigned") - } - - params := &ec2.DescribeImagesInput{} - - if executableUsersOk { - params.ExecutableUsers = expandStringList(executableUsers.([]interface{})) - } - if filtersOk { - params.Filters = buildAwsDataSourceFilters(filters.(*schema.Set)) + params := &ec2.DescribeImagesInput{ + Owners: expandStringList(d.Get("owners").([]interface{})), } - if ownersOk { - o := expandStringList(owners.([]interface{})) - if len(o) > 0 { - params.Owners = o - } + if v, ok := d.GetOk("executable_users"); ok { + params.ExecutableUsers = expandStringList(v.([]interface{})) } - - // Deprecated: pre-2.0.0 warning logging - if !ownersOk { - log.Print("[WARN] The \"owners\" argument will become required in the next major version.") - log.Print("[WARN] Documentation can be found at: https://www.terraform.io/docs/providers/aws/d/ami.html#owners") - - missingOwnerFilter := true - - if filtersOk { - for _, filter := range params.Filters { - if aws.StringValue(filter.Name) == "owner-alias" || aws.StringValue(filter.Name) == "owner-id" { - missingOwnerFilter = false - break - } - } - } - - if missingOwnerFilter { - log.Print("[WARN] Potential security issue: missing \"owners\" filtering for AMI. Check AMI to ensure it came from trusted source.") - } + if v, ok := d.GetOk("filter"); ok { + params.Filters = buildAwsDataSourceFilters(v.(*schema.Set)) } log.Printf("[DEBUG] Reading AMI IDs: %s", params) @@ -103,7 +70,7 @@ func dataSourceAwsAmiIdsRead(d *schema.ResourceData, meta interface{}) error { var filteredImages []*ec2.Image imageIds := make([]string, 0) - if nameRegexOk { + if nameRegex, ok := d.GetOk("name_regex"); ok { r := regexp.MustCompile(nameRegex.(string)) for _, image := range resp.Images { // Check for a very rare case where the response would include no diff --git a/aws/data_source_aws_ami_ids_test.go b/aws/data_source_aws_ami_ids_test.go index 52582eaba1e8..1b3714b2e4f6 100644 --- a/aws/data_source_aws_ami_ids_test.go +++ b/aws/data_source_aws_ami_ids_test.go @@ -54,22 +54,6 @@ func TestAccDataSourceAwsAmiIds_sorted(t *testing.T) { }) } -func TestAccDataSourceAwsAmiIds_empty(t *testing.T) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccDataSourceAwsAmiIdsConfig_empty, - Check: resource.ComposeTestCheckFunc( - testAccCheckAwsAmiDataSourceID("data.aws_ami_ids.empty"), - resource.TestCheckResourceAttr("data.aws_ami_ids.empty", "ids.#", "0"), - ), - }, - }, - }) -} - const testAccDataSourceAwsAmiIdsConfig_basic = ` data "aws_ami_ids" "ubuntu" { owners = ["099720109477"] @@ -117,12 +101,3 @@ data "aws_ami_ids" "test" { } `, uuid) } - -const testAccDataSourceAwsAmiIdsConfig_empty = ` -data "aws_ami_ids" "empty" { - filter { - name = "name" - values = [] - } -} -` diff --git a/aws/data_source_aws_ami_test.go b/aws/data_source_aws_ami_test.go index ae33ec42cee8..4be318799594 100644 --- a/aws/data_source_aws_ami_test.go +++ b/aws/data_source_aws_ami_test.go @@ -127,37 +127,6 @@ func TestAccAWSAmiDataSource_instanceStore(t *testing.T) { }) } -func TestAccAWSAmiDataSource_owners(t *testing.T) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccCheckAwsAmiDataSourceOwnersConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckAwsAmiDataSourceID("data.aws_ami.amazon_ami"), - ), - }, - }, - }) -} - -// Acceptance test for: https://github.com/hashicorp/terraform/issues/10758 -func TestAccAWSAmiDataSource_ownersEmpty(t *testing.T) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccCheckAwsAmiDataSourceEmptyOwnersConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckAwsAmiDataSourceID("data.aws_ami.amazon_ami"), - ), - }, - }, - }) -} - func TestAccAWSAmiDataSource_localNameFilter(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -174,12 +143,7 @@ func TestAccAWSAmiDataSource_localNameFilter(t *testing.T) { }) } -func testAccCheckAwsAmiDataSourceDestroy(s *terraform.State) error { - return nil -} - func testAccCheckAwsAmiDataSourceID(n string) resource.TestCheckFunc { - // Wait for IAM role return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -200,10 +164,8 @@ func testAccCheckAwsAmiDataSourceID(n string) resource.TestCheckFunc { const testAccCheckAwsAmiDataSourceConfig = ` data "aws_ami" "nat_ami" { most_recent = true - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] + filter { name = "name" values = ["amzn-ami-vpc-nat*"] @@ -227,10 +189,8 @@ data "aws_ami" "nat_ami" { const testAccCheckAwsAmiDataSourceWindowsConfig = ` data "aws_ami" "windows_ami" { most_recent = true - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] + filter { name = "name" values = ["Windows_Server-2012-R2*"] @@ -254,10 +214,8 @@ data "aws_ami" "windows_ami" { const testAccCheckAwsAmiDataSourceInstanceStoreConfig = ` data "aws_ami" "instance_store_ami" { most_recent = true - filter { - name = "owner-id" - values = ["099720109477"] - } + owners = ["099720109477"] + filter { name = "name" values = ["ubuntu/images/hvm-instance/ubuntu-trusty-14.04-amd64-server*"] @@ -273,38 +231,6 @@ data "aws_ami" "instance_store_ami" { } ` -// Testing owner parameter -const testAccCheckAwsAmiDataSourceOwnersConfig = ` -data "aws_ami" "amazon_ami" { - most_recent = true - owners = ["amazon"] -} -` - -const testAccCheckAwsAmiDataSourceEmptyOwnersConfig = ` -data "aws_ami" "amazon_ami" { - most_recent = true - owners = [""] - - # we need to test the owners = [""] for regressions but we want to filter the results - # beyond all public AWS AMIs :) - filter { - name = "owner-alias" - values = ["amazon"] - } - - filter { - name = "name" - values = ["amzn-ami-minimal-hvm-*"] - } - - filter { - name = "root-device-type" - values = ["ebs"] - } -} -` - // Testing name_regex parameter const testAccCheckAwsAmiDataSourceNameRegexConfig = ` data "aws_ami" "name_regex_filtered_ami" { diff --git a/aws/data_source_aws_autoscaling_groups_test.go b/aws/data_source_aws_autoscaling_groups_test.go index 67a478c59bf5..7393cb1cd385 100644 --- a/aws/data_source_aws_autoscaling_groups_test.go +++ b/aws/data_source_aws_autoscaling_groups_test.go @@ -83,11 +83,7 @@ func testAccCheckAwsAutoscalingGroupsConfig(rInt1, rInt2, rInt3 int) string { return fmt.Sprintf(` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -161,11 +157,7 @@ func testAccCheckAwsAutoscalingGroupsConfigWithDataSource(rInt1, rInt2, rInt3 in return fmt.Sprintf(` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" diff --git a/aws/data_source_aws_instance_test.go b/aws/data_source_aws_instance_test.go index d68cb0796d3b..45b53be7118d 100644 --- a/aws/data_source_aws_instance_test.go +++ b/aws/data_source_aws_instance_test.go @@ -648,11 +648,7 @@ func testAccInstanceDataSourceConfig_getPasswordData(val bool, rInt int) string # Find latest Microsoft Windows Server 2016 Core image (Amazon deletes old ones) data "aws_ami" "win2016core" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" diff --git a/aws/resource_aws_autoscaling_group_test.go b/aws/resource_aws_autoscaling_group_test.go index fb6af859d856..e5aa0b94a37e 100644 --- a/aws/resource_aws_autoscaling_group_test.go +++ b/aws/resource_aws_autoscaling_group_test.go @@ -991,11 +991,7 @@ func TestAccAWSAutoScalingGroup_launchTemplate_update(t *testing.T) { const testAccAWSAutoScalingGroupConfig_autoGeneratedName = ` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1020,11 +1016,7 @@ resource "aws_autoscaling_group" "bar" { const testAccAWSAutoScalingGroupConfig_namePrefix = ` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1050,11 +1042,7 @@ resource "aws_autoscaling_group" "test" { const testAccAWSAutoScalingGroupConfig_terminationPoliciesEmpty = ` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1080,11 +1068,7 @@ resource "aws_autoscaling_group" "bar" { const testAccAWSAutoScalingGroupConfig_terminationPoliciesExplicitDefault = ` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1111,11 +1095,7 @@ resource "aws_autoscaling_group" "bar" { const testAccAWSAutoScalingGroupConfig_terminationPoliciesUpdate = ` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1143,11 +1123,7 @@ func testAccAWSAutoScalingGroupConfig(name string) string { return fmt.Sprintf(` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1202,11 +1178,7 @@ func testAccAWSAutoScalingGroupConfigUpdate(name string) string { return fmt.Sprintf(` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1263,11 +1235,7 @@ func testAccAWSAutoScalingGroupImport(name string) string { return fmt.Sprintf(` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1430,11 +1398,7 @@ resource "aws_subnet" "main" { data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1477,11 +1441,7 @@ resource "aws_subnet" "main" { data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1509,11 +1469,7 @@ func testAccAWSAutoScalingGroupConfig_withPlacementGroup(name string) string { return fmt.Sprintf(` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1557,11 +1513,7 @@ resource "aws_autoscaling_group" "bar" { const testAccAWSAutoScalingGroupConfig_withServiceLinkedRoleARN = ` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1591,11 +1543,7 @@ resource "aws_autoscaling_group" "bar" { const testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected = ` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1633,11 +1581,7 @@ resource "aws_autoscaling_group" "bar" { const testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected = ` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1708,11 +1652,7 @@ resource "aws_subnet" "alt" { data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1799,11 +1739,7 @@ resource "aws_subnet" "alt" { data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1903,11 +1839,7 @@ resource "aws_subnet" "alt" { data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -1965,11 +1897,7 @@ func testAccAWSAutoScalingGroupWithHookConfig(name string) string { return fmt.Sprintf(` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -2103,11 +2031,7 @@ resource "aws_route" "public_default_route" { data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -2153,11 +2077,7 @@ func testAccAWSAutoScalingGroupConfigWithSuspendedProcesses(name string) string return fmt.Sprintf(` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -2202,11 +2122,7 @@ func testAccAWSAutoScalingGroupConfigWithSuspendedProcessesUpdated(name string) return fmt.Sprintf(` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -2259,11 +2175,7 @@ resource "aws_autoscaling_group" "test" { data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -2304,11 +2216,7 @@ resource "aws_autoscaling_group" "test" { data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -2325,11 +2233,7 @@ resource "aws_launch_configuration" "test" { const testAccAWSAutoScalingGroupConfig_withLaunchTemplate = ` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -2360,11 +2264,7 @@ resource "aws_autoscaling_group" "bar" { const testAccAWSAutoScalingGroupConfig_withLaunchTemplate_toLaunchConfig = ` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -2397,11 +2297,7 @@ resource "aws_autoscaling_group" "bar" { const testAccAWSAutoScalingGroupConfig_withLaunchTemplate_toLaunchTemplateName = ` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -2442,11 +2338,7 @@ resource "aws_autoscaling_group" "bar" { const testAccAWSAutoScalingGroupConfig_withLaunchTemplate_toLaunchTemplateVersion = ` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" diff --git a/aws/resource_aws_eip_test.go b/aws/resource_aws_eip_test.go index d5293da41659..f40de0eb3026 100644 --- a/aws/resource_aws_eip_test.go +++ b/aws/resource_aws_eip_test.go @@ -461,10 +461,8 @@ provider "aws" { data "aws_ami" "amzn-ami-minimal-pv" { most_recent = true - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] + filter { name = "name" values = ["amzn-ami-minimal-pv-*"] @@ -491,10 +489,8 @@ resource "aws_eip" "bar" { const testAccAWSEIPInstanceConfig = ` data "aws_ami" "amzn-ami-minimal-pv" { most_recent = true - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] + filter { name = "name" values = ["amzn-ami-minimal-pv-*"] @@ -518,10 +514,8 @@ resource "aws_eip" "bar" { const testAccAWSEIPInstanceConfig2 = ` data "aws_ami" "amzn-ami-minimal-pv" { most_recent = true - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] + filter { name = "name" values = ["amzn-ami-minimal-pv-*"] @@ -545,10 +539,8 @@ resource "aws_eip" "bar" { const testAccAWSEIPInstanceConfig_associated = ` data "aws_ami" "amzn-ami-minimal-hvm" { most_recent = true - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] + filter { name = "name" values = ["amzn-ami-minimal-hvm-*"] @@ -623,10 +615,8 @@ resource "aws_eip" "bar" { const testAccAWSEIPInstanceConfig_associated_switch = ` data "aws_ami" "amzn-ami-minimal-hvm" { most_recent = true - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] + filter { name = "name" values = ["amzn-ami-minimal-hvm-*"] @@ -787,10 +777,8 @@ variable "server_count" { data "aws_ami" "amzn-ami-minimal-pv" { most_recent = true - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] + filter { name = "name" values = ["amzn-ami-minimal-pv-*"] @@ -864,10 +852,8 @@ resource "aws_route_table_association" "us-east-1b-public" { const testAccAWSEIPAssociate_not_associated = ` data "aws_ami" "amzn-ami-minimal-pv" { most_recent = true - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] + filter { name = "name" values = ["amzn-ami-minimal-pv-*"] @@ -890,10 +876,8 @@ resource "aws_eip" "bar" { const testAccAWSEIPAssociate_associated = ` data "aws_ami" "amzn-ami-minimal-pv" { most_recent = true - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] + filter { name = "name" values = ["amzn-ami-minimal-pv-*"] diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index 4c1141630f66..ea8796c27e4e 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -3233,11 +3233,7 @@ func testAccInstanceConfig_getPasswordData(val bool, rInt int) string { # Find latest Microsoft Windows Server 2016 Core image (Amazon deletes old ones) data "aws_ami" "win2016core" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" diff --git a/aws/resource_aws_launch_template_test.go b/aws/resource_aws_launch_template_test.go index 151b7011ef7a..cbdb836b6ad7 100644 --- a/aws/resource_aws_launch_template_test.go +++ b/aws/resource_aws_launch_template_test.go @@ -422,11 +422,7 @@ resource "aws_launch_template" "test" { const testAccAWSLaunchTemplateConfig_asg_basic = ` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -456,11 +452,7 @@ resource "aws_autoscaling_group" "bar" { const testAccAWSLaunchTemplateConfig_asg_update = ` data "aws_ami" "test_ami" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" diff --git a/aws/resource_aws_spot_instance_request_test.go b/aws/resource_aws_spot_instance_request_test.go index 09d82f71e485..cac037ccc49b 100644 --- a/aws/resource_aws_spot_instance_request_test.go +++ b/aws/resource_aws_spot_instance_request_test.go @@ -756,11 +756,7 @@ func testAccAWSSpotInstanceRequestConfig_getPasswordData(rInt int) string { # Find latest Microsoft Windows Server 2016 Core image (Amazon deletes old ones) data "aws_ami" "win2016core" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" diff --git a/aws/resource_aws_storagegateway_gateway_test.go b/aws/resource_aws_storagegateway_gateway_test.go index ec425c64dfc3..09fb19154ac9 100644 --- a/aws/resource_aws_storagegateway_gateway_test.go +++ b/aws/resource_aws_storagegateway_gateway_test.go @@ -468,11 +468,7 @@ func testAccAWSStorageGateway_FileGatewayBase(rName string) string { return testAccAWSStorageGateway_VPCBase(rName) + fmt.Sprintf(` data "aws_ami" "aws-thinstaller" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -503,11 +499,7 @@ func testAccAWSStorageGateway_TapeAndVolumeGatewayBase(rName string) string { return testAccAWSStorageGateway_VPCBase(rName) + fmt.Sprintf(` data "aws_ami" "aws-storage-gateway-2" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" @@ -692,11 +684,7 @@ resource "aws_vpc_dhcp_options_association" "test" { data "aws_ami" "aws-thinstaller" { most_recent = true - - filter { - name = "owner-alias" - values = ["amazon"] - } + owners = ["amazon"] filter { name = "name" diff --git a/website/docs/d/ami.html.markdown b/website/docs/d/ami.html.markdown index baae6aaf189d..04d6ad1a0b8b 100644 --- a/website/docs/d/ami.html.markdown +++ b/website/docs/d/ami.html.markdown @@ -11,32 +11,36 @@ description: |- Use this data source to get the ID of a registered AMI for use in other resources. -~> **NOTE:** The `owners` argument will be **required** in the next major version. - ## Example Usage ```hcl -data "aws_ami" "nat_ami" { - most_recent = true +data "aws_ami" "example" { executable_users = ["self"] + most_recent = true + name_regex = "^myami-\\d{3}" + owners = ["self"] filter { - name = "owner-alias" - values = ["amazon"] + name = "name" + values = ["myami-*"] } filter { - name = "name" - values = ["amzn-ami-vpc-nat*"] + name = "root-device-type" + values = ["ebs"] } - name_regex = "^myami-\\d{3}" - owners = ["self"] + filter { + name = "virtualization-type" + values = ["hvm"] + } } ``` ## Argument Reference +* `owners` - (Required) List of AMI owners to limit search. At least 1 value must be specified. Valid values: an AWS account ID, `self` (the current account), or an AWS owner alias (e.g. `amazon`, `aws-marketplace`, `microsoft`). + * `most_recent` - (Optional) If more than one result is returned, use the most recent AMI. @@ -47,18 +51,12 @@ recent AMI. several valid keys, for a full reference, check out [describe-images in the AWS CLI reference][1]. -* `owners` - (Optional) Limit search to specific AMI owners. Valid items are the numeric -account ID, `amazon`, or `self`. - * `name_regex` - (Optional) A regex string to apply to the AMI list returned by AWS. This allows more advanced filtering not supported from the AWS API. This filtering is done locally on what AWS returns, and could have a performance impact if the result is large. It is recommended to combine this with other options to narrow down the list AWS returns. -~> **NOTE:** At least one of `executable_users`, `filter`, `owners`, or -`name_regex` must be specified. - ~> **NOTE:** If more or less than a single match is returned by the search, Terraform will fail. Ensure that your search is specific enough to return a single AMI ID only, or use `most_recent` to choose the most recent one. If diff --git a/website/docs/d/ami_ids.html.markdown b/website/docs/d/ami_ids.html.markdown index 6e979f76c141..e64a6d633123 100644 --- a/website/docs/d/ami_ids.html.markdown +++ b/website/docs/d/ami_ids.html.markdown @@ -10,8 +10,6 @@ description: |- Use this data source to get a list of AMI IDs matching the specified criteria. -~> **NOTE:** The `owners` argument will be **required** in the next major version. - ## Example Usage ```hcl @@ -27,6 +25,8 @@ data "aws_ami_ids" "ubuntu" { ## Argument Reference +* `owners` - (Required) List of AMI owners to limit search. At least 1 value must be specified. Valid values: an AWS account ID, `self` (the current account), or an AWS owner alias (e.g. `amazon`, `aws-marketplace`, `microsoft`). + * `executable_users` - (Optional) Limit search to users with *explicit* launch permission on the image. Valid items are the numeric account ID or `self`. @@ -34,18 +34,12 @@ permission on the image. Valid items are the numeric account ID or `self`. are several valid keys, for a full reference, check out [describe-images in the AWS CLI reference][1]. -* `owners` - (Optional) Limit search to specific AMI owners. Valid items are -the numeric account ID, `amazon`, or `self`. - * `name_regex` - (Optional) A regex string to apply to the AMI list returned by AWS. This allows more advanced filtering not supported from the AWS API. This filtering is done locally on what AWS returns, and could have a performance impact if the result is large. It is recommended to combine this with other options to narrow down the list AWS returns. -~> **NOTE:** At least one of `executable_users`, `filter`, `owners` or -`name_regex` must be specified. - ## Attributes Reference `ids` is set to the list of AMI IDs, sorted by creation time in descending