-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow Data Source for AWS instances to get instances that are in a state other than running #4950
Allow Data Source for AWS instances to get instances that are in a state other than running #4950
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @dieterdemeyer 👋 Thanks for submitting this!
While you have the right intentions here, as written this could be a breaking change in behavior for some Terraform configurations. Some may be relying on the existing behavior of only running
. Please see the below for ideas of how to implement this without breaking backwards compatibility.
Also I'm noting in your acceptance test output a few things:
make testacc TESTARGS='-run=TestAccAWSAvailabilityZones'
The pull request template only provides an example of which acceptance tests to run. Following typical Go conventions, the test names can be found in aws/data_source_aws_instances_test.go
. So in this case: make testacc TESTARGS='-run=TestAccAWSInstancesDataSource'
will run the appropriate acceptance tests for this data source.
provider_test.go:62: AWS_ACCESS_KEY_ID must be set for acceptance tests
You'll need to appropriately set either the AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
environment variables or the AWS_PROFILE
environment variables. The testing suite will spin up real infrastructure on AWS and requires credentials to do so.
Please reach out with any questions or if you do not have time to implement the feedback. Thanks!
aws/data_source_aws_instances.go
Outdated
@@ -50,8 +50,14 @@ func dataSourceAwsInstancesRead(d *schema.ResourceData, meta interface{}) error | |||
params := &ec2.DescribeInstancesInput{ | |||
Filters: []*ec2.Filter{ | |||
&ec2.Filter{ | |||
Name: aws.String("instance-state-name"), | |||
Values: []*string{aws.String("running")}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The enhancement here needs to allow the instance-state
to either be configurable, while leaving the existing default behavior to not break backwards compatibility. This can be accomplished two ways:
Less preferably as a string attribute, but provided as an example, e.g.
"instance_state_name": {
Type: schema.TypeString,
Optional: true,
Default: ec2.InstanceStateNameRunning,
ValidateFunc: validation.StringInSlice([]string{
ec2.InstanceStateNamePending,
ec2.InstanceStateNameRunning,
ec2.InstanceStateNameShuttingDown,
ec2.InstanceStateNameStopped,
ec2.InstanceStateNameStopping,
ec2.InstanceStateNameTerminated,
}, false),
},
Where it is referenced in the code with:
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
&ec2.Filter{
Name: aws.String("instance-state-name"),
Values: []*string{aws.String(d.Get("instance_state_name").(string))},
},
},
}
Or, more preferably, as a set so you can configure one or more instance states:
"instance_state_names": {
Type: schema.TypeSet,
Optional: true,
Elem: schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
ec2.InstanceStateNamePending,
ec2.InstanceStateNameRunning,
ec2.InstanceStateNameShuttingDown,
ec2.InstanceStateNameStopped,
ec2.InstanceStateNameStopping,
ec2.InstanceStateNameTerminated,
}, false),
},
},
Where it is referenced in the code with:
instanceStateNames := []*string{aws.String(ec2.InstanceStateNameRunning)}
if v, ok := d.GetOk("instance_state_names"); ok && len(v.(*schema.Set).List()) > 0 {
instanceStateNames = expandStringSet(v.(*schema.Set))
}
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
&ec2.Filter{
Name: aws.String("instance-state-name"),
Values: instanceStateNames,
},
},
}
Either case should implement a new acceptance test that creates two instances and immediately calls the data source that accepts the pending
(and probably running
as well) instance states.
Thanks for your feedback. |
I made the required changes locally and now I'm trying to write a test case. |
@dieterdemeyer testing against No worries if you cannot figure out the testing. I would push up what you have so far. |
@bflad I modified the required code and tested this locally with one of our AWS accounts. This seems to work. My output is as follows:
|
|
||
data "aws_instances" "test" { | ||
instance_tags { | ||
Name = "*" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To properly setup the resource and data source ordering, you can reference the aws_instance
resources created above 😄
Name = "${aws_instance.test.0.tags["Name"]}"
This should be enough to delay the data source to catch the two new instances 👍 Let me know if this still is causing an issue and we can dive in deeper. Thanks for your work here!
@@ -45,6 +47,8 @@ resource "aws_eip" "test" { | |||
* `instance_tags` - (Optional) A mapping of tags, each pair of which must | |||
exactly match a pair on desired instances. | |||
|
|||
* `instance_state_names` - (Optional) A list of instance states that should be applicable to the desired instances. The permitted values are: `pending, running, shutting-down, stopped, stopping, terminated` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should list the default behavior of returning only running
here 👍
@bflad Thanks for your assistance. I've updated the code and ran the tests. It seems to work with your suggestion and you can find the output below:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks @dieterdemeyer! 🚀
make testacc TEST=./aws TESTARGS='-run=TestAccAWSInstancesDataSource'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSInstancesDataSource -timeout 120m
=== RUN TestAccAWSInstancesDataSource_basic
--- PASS: TestAccAWSInstancesDataSource_basic (100.72s)
=== RUN TestAccAWSInstancesDataSource_tags
--- PASS: TestAccAWSInstancesDataSource_tags (109.24s)
=== RUN TestAccAWSInstancesDataSource_instance_state_names
--- PASS: TestAccAWSInstancesDataSource_instance_state_names (99.12s)
PASS
ok github.com/terraform-providers/terraform-provider-aws/aws 309.122s
@bflad Thanks for getting this merged ! |
This has been released in version 1.26.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks! |
Fixes #3339
Changes proposed in this pull request:
Output from acceptance testing: