You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When an ec2 instance is terminated it can still appear in results/response for describing ec2 instances.
Describe the problem
When an ec2 instance is terminated it can still appear in results/response for describing ec2 instances. Eventually AWS removes it. A terminated instance passes the it { should exist }. This is not correct. it can also lead to problems when you have more than one instance with same name. One is terminated and other is non-terminated state(running, stopped, etc). The aws_ec2_instance(name: 'foo') resource will fail because more than once instance matching name is returned. The aws_ec2_instances resource does not allow filter by instance state. The resources should filter out terminated instances by default or give ability to do so. Not sure if anyone would ever want them to be included given how transient the terminated state is.
Possible Solution
I suggest filtering out terminated by default. I suppose could also add instance state as column in FilterTable?
def initialize(opts = {})
opts = { instance_id: opts } if opts.is_a?(String)
super(opts)
validate_parameters(require_any_of: %i(instance_id name))
state_filter = {
name: 'instance-state-name',
values: [
'pending',
'running',
'shutting-down',
'stopping',
'stopped'
]
}
if opts[:instance_id] && !opts[:instance_id].empty? # Use instance_id, if provided
if !opts[:instance_id].is_a?(String) || opts[:instance_id] !~ /(^i-[0-9a-f]{8})|(^i-[0-9a-f]{17})$/
raise ArgumentError, "#{@__resource_name__}: `instance_id` must be a string in the format of 'i-' followed by 8 or 17 hexadecimal characters."
end
@display_name = opts[:instance_id]
instance_arguments = { instance_ids: [opts[:instance_id]], filters: [state_filter] }
elsif opts[:name] && !opts[:name].empty? # Otherwise use name, if provided
@display_name = opts[:name]
instance_arguments = { filters: [{ name: "tag:Name", values: [opts[:name]] }, state_filter] }
else
raise ArgumentError, "#{@__resource_name__}: either instance_id or name must be provided."
end
The text was updated successfully, but these errors were encountered:
When an ec2 instance is terminated it can still appear in results/response for describing ec2 instances.
Describe the problem
When an ec2 instance is terminated it can still appear in results/response for describing ec2 instances. Eventually AWS removes it. A terminated instance passes the
it { should exist }
. This is not correct. it can also lead to problems when you have more than one instance with same name. One is terminated and other is non-terminated state(running, stopped, etc). Theaws_ec2_instance(name: 'foo')
resource will fail because more than once instance matching name is returned. Theaws_ec2_instances
resource does not allow filter by instance state. The resources should filter out terminated instances by default or give ability to do so. Not sure if anyone would ever want them to be included given how transient the terminated state is.Possible Solution
I suggest filtering out terminated by default. I suppose could also add instance state as column in FilterTable?
Change fetch_data
inspec-aws/libraries/aws_ec2_instances.rb
Line 30 in a23887f
This might not be ideal but I like idea of filtering out terminated instance here
inspec-aws/libraries/aws_ec2_instance.rb
Line 19 in a23887f
The text was updated successfully, but these errors were encountered: