terraforming icon indicating copy to clipboard operation
terraforming copied to clipboard

Allow filtering of resources

Open iragsdale opened this issue 10 years ago • 21 comments

Thanks so much for writing this, it looks like it's gonna be awesome for migrating existing resources into Terraform!

However, I would prefer to migrate in smaller chunks, so I think it would be awesome if we could specify some basic parameter matching on the command line so that we only generate files for specific resources.

If you're interested in a pull request, I might give this a shot myself.

iragsdale avatar Jun 24 '15 20:06 iragsdale

This would also really help me. +1

nrcxcia avatar Jun 24 '15 20:06 nrcxcia

Like this?

$ terraforming s3 --resource hoge
resource "aws_s3_bucket" "hoge" {
    bucket = "hoge"
    acl    = "private"
}

It sounds good and useful if it exists. @iragsdale Could you send a pull request if you already have an idea of implementation?

dtan4 avatar Jun 25 '15 00:06 dtan4

Something like that, yeah. I'll see if I can take a crack at it soon.

iragsdale avatar Jun 25 '15 00:06 iragsdale

So this isn't quite ready for a pull request, but it's functional for EC2 resources and illustrates a relatively simple approach.

https://github.com/iragsdale/terraforming/commit/d82159c7e44f7022f9cd0e8e2280ac71b028dca2

It lets you specify multiple patterns like:

terraforming ec2 --limit instance_type=m3.medium tags=~whatever placement.availability_zone=us-east-1b

I ran into some trouble with the existing specs, this blows up around 25 of them for some reason and I'm a bit too tired to figure out why at the moment. (Plus my ruby is a bit rusty).

Anyway, if the approach makes sense I don't think it should be too difficult to implement the basic matching for the remaining resource types.

It's also a bit confusing because we're matching on the objects being returned by the SDK but printing out the values expected by Terraform, so the output shows availability_zone but the matcher requires placement.availability_zone. There are some other minor matching issues like that where some cleverness could make it way easier to use (tags are one spot).

Anyway, if you could give this a once over and maybe point me in the right direction on the specs, I could maybe clean it up enough to submit a pull request.

iragsdale avatar Jun 25 '15 06:06 iragsdale

Sorry for the late reply ... :bow: :bow: :bow: It seems good creating Matcher class :smile:

I understood your problem. placement.availability_zone= is not a good option, because user should know about AWS API.

I think maybe constructing the struct which has the same fields as Terraform resource before executing filtering is very helpful... e.g. EC2) SDK returns Aws::EC2::Types::Instance instance, then convert it to the struct like Terraform's instance resource. After that Terraforming executes filtering.

Because this approach may be so heavy, however, how about implement a simple filter to select by resource name?

dtan4 avatar Jun 30 '15 01:06 dtan4

We are using Terraforming on our project as well, and have to reverse engineer a giant pile of EC2 resources. Filtering, especially by things like instance tag, would be incredibly helpful. What is the status of this work in progress? Can it be turned into a pull request within a reasonably speedy timeframe? ;)

chriskilding avatar Feb 18 '16 10:02 chriskilding

I think filtering by tag would be really helpful. Currently I am trying to transform multiple CloudFormation stacks into terraform environments.

bassrock avatar Apr 01 '16 04:04 bassrock

+1

kbroughton avatar Apr 28 '16 23:04 kbroughton

This was an incredibly naive implementation but it worked for my purposes. Our internal priorities changed so I haven't had much time to try to turn it into a suitable PR, but hopefully it'll be helpful for someone: https://github.com/dtan4/terraforming/compare/master...iragsdale:master

iragsdale avatar Apr 29 '16 00:04 iragsdale

Thanks, I pulled your branch master.

I was able to get filtering with terraforming ec2 --limit placement.availability_zone=us-west-2a

The options suggest you can filter by tag, but I'm not sure how to Options: [--merge=MERGE] # tfstate file to merge [--tfstate], [--no-tfstate] # Generate tfstate [--limit=image_id=ami-xxxx placement.availability_zone=us-east-1b tags=~name] # List matching resources. Arguments using =~ match using regular expressions.

I would expect --limit tags=<tag_key>=<tag_value> or --limit tags="<tag_key>,tag_value" so that you can specify the filtering to only match when supplied key,value is a match.

Could you supply the correct syntax?

kbroughton avatar May 02 '16 03:05 kbroughton

@kbroughton I'm sorry, I don't think I'm gonna be able to provide much in the way of support right now. The syntax sort of depends on the structure of the objects being matched, and I'm a bit too busy at work to get set up to figure that out right now. :(

iragsdale avatar May 02 '16 14:05 iragsdale

I ran into a similar problem at work, where I was trying to use terraforming to describe only a small subset of aws resources. I approached the problem by using leveraging the aws sdk opposed to matching. I was wondering what people thought of this approach and if it would be useful to anybody if I submitted a pr with this feature?

soapy1 avatar May 02 '16 16:05 soapy1

That's a great approach. Is there a way to factor it out so you don't have to touch every single file? It seems the current architecture makes it difficult to get the hook into the klass on execute.

I would also be fine for now with just being able to filter after, but i'm so new to ruby, i don't know how to access the dict items from output like resource "aws_instance" "control" { blah: blah tags: { dict i want access to } }

Could you suggest a code snippet to get at tags for me?

kbroughton avatar May 02 '16 16:05 kbroughton

@soapy1 if you have a beta version of your PR i am working on this issue for the next day or two.

kbroughton avatar May 03 '16 15:05 kbroughton

@kbroughton I pushed up my implementation to https://github.com/dtan4/terraforming/compare/master...soapy1:pre-filtering-resources

You can use it like this:

bin/terraforming ec2 --filters '[{"name":"instance-id", "values":["i-0bdd70830cd1a3ac0"]}]'

so far, it's only able to filter ec2 instances.

I haven't written any tests for it yet. If people like the implementation then I will flush it out a bit more, add tests and make a pr.

soapy1 avatar May 03 '16 18:05 soapy1

Thanks a lot soapy! I'm using it now. Nice implementation. I've written some automation to add the snippet equivalents for each resource that is taggable.

          attributes.merge!(tags_attributes_of(instance))

and


      def tags_attributes_of(instance)
        tags = instance.tags
        attributes = { "tags.#" => tags.length.to_s }
        tags.each { |tag| attributes["tags.#{tag.key}"] = tag.value }
        attributes
      end

Now i'm working on spreading the filtering you did for ec2.rb across all resources.

kbroughton avatar May 06 '16 00:05 kbroughton

Hi soapy1,

I got a little further extending your work to other modules and adding better tag support. https://github.com/dtan4/terraforming/pull/215

kbroughton avatar May 09 '16 02:05 kbroughton

@kbroughton any chance we can merge this? I would really benefit from the --filter flag.

mhemken-nyt avatar Feb 06 '20 21:02 mhemken-nyt

I'm afraid I haven't touched this for years, and won't have time to work on it this month. Happy to have someone fork it and re-submit.

kbroughton avatar Feb 06 '20 22:02 kbroughton

@kbroughton I pushed up my implementation to master...soapy1:pre-filtering-resources

You can use it like this:

bin/terraforming ec2 --filters '[{"name":"instance-id", "values":["i-0bdd70830cd1a3ac0"]}]'

so far, it's only able to filter ec2 instances.

I haven't written any tests for it yet. If people like the implementation then I will flush it out a bit more, add tests and make a pr.

Hi Soapy1, Not sure if the filtering was implemented but I really need this feature as I only want to do terraforming with single vpc (prod). pls let me know the resolution on this, thanks a lot.

skambiente avatar Mar 05 '21 12:03 skambiente

@skambiente, I'm in a similar boat at kbroughton here. I haven't touched this in a long time, and I'm not involved in the project, so I don't know. It seems like the PR that add this feature is still open #215. You might be able to use a check out of that branch to use the feature.

soapy1 avatar Mar 05 '21 15:03 soapy1