我想运行地形只为一个特定的资源

要花很长时间在地形上运行并等待。 所以我想运行它来排除执行时间最长的 rds 或者我只想运行 ec2资源。 在地球化环境中有办法做到这些吗?

88750 次浏览

You can use -target=resource like this:

terraform plan -target=module.mymodule.aws_instance.myinstance
terraform apply -target=module.mymodule.aws_instance.myinstance

or

terraform plan -target=aws_instance.myinstance
terraform apply -target=aws_instance.myinstance

Disclaimer: Before downvoting the answer, please note that he actually asked to either "exclude" or "run only ec2 resource". And after all this time the exclude feature request is still open in the terraform repo.

I would like to run it to exclude rds that takes the longest time

Terraform currently does not support exclusion of resources (aka. inverse targeting).

Issue #2253: "feature request: inverse targeting / exclude"

(Thanks for the link Julio Daniel Reyes.)

Adding to Julio's answer, you could target multiple resources in the following manner:

terraform init
terraform plan -target=resource_type1.resource_name1 -target=resource_type2.resource_name1
terraform apply -target=resource_type1.resource_name1 -target=resource_type2.resource_name1

For those who use variables

terraform plan -var-file dev.tfvars -target=module.mymodule.aws_instance.myinstance
terraform apply -var-file dev.tfvars -target=module.mymodule.aws_instance.myinstance