普通に terraform apply を実行すると適用されているすべてのモジュールが処理されます。
モジュールが少ないときはまだ良いのですが、次第に増えてくると一部の修正のみでも実際にその修正部分が処理されるまで、時間がかかる場合があります。
そこで処理するモジュールを指定することで、Terraformの実行時間を短縮できるメリットがあります。
ただ注意点として、特定moduleのみ実行しているとソースコードと実際のインフラが乖離していく恐れがあるため、日常的に使用するものではなくミスした部分の修正など例外的に使用するものとされています。
本番環境での使用は控えたほうがいいかもしれませんが、開発環境で試行錯誤しているときなどはかなり便利です。
手順
基本構文・実行例
構文は以下になります。
terraform [plan|apply|destroy] -target=module.[モジュール名]
例として前回の記事のソースコードを元にplanを実行してみたいと思います。
このモジュール群は、network → security → hello-app-server の順番で依存関係を張っています。
% cd environments/dev
%
## モジュールの確認
% grep ^module main.tf
module "network" {
module "security" {
module "hello-app-server" {
%
networkモジュールのみ指定して実行します。
% terraform plan -target=module.network | grep module
# module.network.aws_internet_gateway.test-gw will be created
# module.network.aws_route_table.test-route will be created
# module.network.aws_route_table_association.sub-east will be created
# module.network.aws_route_table_association.sub-west will be created
# module.network.aws_subnet.test-sub-east will be created
# module.network.aws_subnet.test-sub-west will be created
# module.network.aws_vpc.test-vpc will be created
%
ためしに依存関係の最後のモジュール hello-app-server を指定してみます。
hello-app-server だけでなく、依存が明示的に張られている network、security のモジュールの resource も実行されますが、明示的に張られていない(実行されない)resource が何個かあったため最終的には apply が失敗しました。
% terraform plan -target=module.hello-app-server | grep module
# module.hello-app-server.aws_instance.test-server-east will be created
# module.hello-app-server.aws_instance.test-server-west will be created
# module.hello-app-server.aws_key_pair.test-key will be created
# module.hello-app-server.aws_lb.test-lb will be created
# module.hello-app-server.aws_lb_listener.test-listener will be created
# module.hello-app-server.aws_lb_target_group.test-terget will be created
# module.hello-app-server.aws_lb_target_group_attachment.test-server-east will be created
# module.hello-app-server.aws_lb_target_group_attachment.test-server-west will be created
# module.network.aws_subnet.test-sub-east will be created
# module.network.aws_subnet.test-sub-west will be created
# module.network.aws_vpc.test-vpc will be created
# module.security.aws_security_group.test-sg-lb will be created
# module.security.aws_security_group.test-sg-server will be created
%
複数ターゲットの指定
-targetを繰り返し記述してください。
% terraform plan -target=module.network -target=module.security | grep module
# module.network.aws_internet_gateway.test-gw will be created
# module.network.aws_route_table.test-route will be created
# module.network.aws_route_table_association.sub-east will be created
# module.network.aws_route_table_association.sub-west will be created
# module.network.aws_subnet.test-sub-east will be created
# module.network.aws_subnet.test-sub-west will be created
# module.network.aws_vpc.test-vpc will be created
# module.security.aws_security_group.test-sg-lb will be created
# module.security.aws_security_group.test-sg-server will be created
%
リソースまで指定
更に以下のようにモジュールの中のリソースまで指定できます。
terraform [plan|apply|destroy] -target=module.[モジュール名].[リソースタイプ].[リソース名]
% terraform plan -target=module.hello-app-server.aws_instance.test-server-east | grep module
# module.hello-app-server.aws_instance.test-server-east will be created
# module.network.aws_subnet.test-sub-east will be created
# module.network.aws_vpc.test-vpc will be created
# module.security.aws_security_group.test-sg-lb will be created
# module.security.aws_security_group.test-sg-server will be created
%
以上です〜ノシ
参考
(о ̄∇ ̄)/ぁざ~~っす