
私は 2 つのモジュールを使用しています。1 つはカスタム VPC モジュールで、もう 1 つは Jenkins ec2 インスタンスを起動するモジュールです。
モジュールでは使用できませんdepends_on
が、Jenkins モジュールは次のように VPC モジュールからの特定の出力に依存します...
jenkins_elb_subnets_ids = ["${module.vpc.public_subnets_ids[0]}", "${module.vpc.public_subnets_ids[1]}"]
jenkins_instance_subnets_ids = ["${module.vpc.private_subnets_ids[0]}", "${module.vpc.private_subnets_ids[1]}"]
vpc_id = "${module.vpc.vpc_id}"
vpc_cidr = "${var.vpc_cidr}"
それでも、NAT ゲートウェイが作成される前に Jenkins Ec2 インスタンスが起動するのを防ぐことはできません。
�[0m�[1mmodule.jenkins.aws_launch_configuration.jenkins_lc: Creation complete after 5s (ID: devops-jenkins-lc-20180309131935169800000002)�[0m�[0m
�[0m�[1mmodule.jenkins.aws_autoscaling_group.jenkins_asg: Creating...�[0m
arn: "" => "<computed>"
default_cooldown: "" => "<computed>"
desired_capacity: "" => "1"
force_delete: "" => "false"
health_check_grace_period: "" => "300"
health_check_type: "" => "EC2"
launch_configuration: "" => "devops-jenkins-lc-20180309131935169800000002"
load_balancers.#: "" => "1"
load_balancers.2235174564: "" => "devops-jenkins-elb"
max_size: "" => "1"
metrics_granularity: "" => "1Minute"
min_size: "" => "1"
name: "" => "devops-jenkins-lc-20180309131935169800000002"
protect_from_scale_in: "" => "false"
tags.#: "" => "4"
tags.0.%: "" => "3"
tags.0.key: "" => "Name"
tags.0.propagate_at_launch: "" => "1"
tags.0.value: "" => "devops-jenkins"
tags.1.%: "" => "3"
tags.1.key: "" => "BackupDisable"
tags.1.propagate_at_launch: "" => "1"
tags.1.value: "" => "No"
tags.2.%: "" => "3"
tags.2.key: "" => "Environment"
tags.2.propagate_at_launch: "" => "1"
tags.2.value: "" => "dev"
tags.3.%: "" => "3"
tags.3.key: "" => "AppComponent"
tags.3.propagate_at_launch: "" => "1"
tags.3.value: "" => "Jenkins-master"
target_group_arns.#: "" => "<computed>"
vpc_zone_identifier.#: "" => "2"
vpc_zone_identifier.3355635847: "" => "subnet-4f13e705"
vpc_zone_identifier.3554579391: "" => "subnet-8e92b2d3"
wait_for_capacity_timeout: "" => "0"�[0m
�[0m�[1mmodule.jenkins.aws_autoscaling_group.jenkins_asg: Creation complete after 1s (ID: devops-jenkins-lc-20180309131935169800000002)�[0m�[0m
�[0m�[1mmodule.vpc.aws_vpn_gateway.transit_vgw: Still creating... (10s elapsed)�[0m�[0m
�[0m�[1mmodule.vpc.aws_route53_zone.main: Still creating... (10s elapsed)�[0m�[0m
�[0m�[1mmodule.vpc.aws_nat_gateway.private_nat_gw.1: Still creating... (10s elapsed)�[0m�[0m
�[0m�[1mmodule.vpc.aws_nat_gateway.private_nat_gw.0: Still creating... (10s elapsed)�[0m�[0m
その結果、Jenkins は正常に起動できなくなります。
Cannot find a valid baseurl for repo: amzn-main/latest
Could not retrieve mirrorlist http://repo.us-east-1.amazonaws.com/latest/main/mirror.list error was
12: Timeout on http://repo.us-east-1.amazonaws.com/latest/main/mirror.list: (28, 'Connection timed out after 5001 milliseconds')
Mar 09 13:19:55 cloud-init[2581]: util.py[WARNING]: Failed to install packages: ['git', 'aws-cfn-bootstrap', 'docker', 'jq-libs', 'jq', 'perl-Test-Simple.noarch', 'perl-YAML.noarch', 'gcc', 'amazon-ssm-agent.rpm', 'perl-Switch', 'perl-DateTime', 'perl-Sys-Syslog', 'perl-LWP-Protocol-https', 'perl-Test-Simple.noarch', 'perl-YAML.noarch']
現在、Terraform には AWS チームによる「公式」 VPC モジュールがあります。そのコードを確認しましたが、この問題を軽減する対策は何も講じられていないようです。しかし、90,000 のデプロイメントで問題が 36 件しかないので、彼らが抱えている問題ではないようです。使用できないため自分でテストしていませんが、私のモジュールに問題がある可能性があります。
編集: うまくいきませんでした @sysadmin1138 これを試しました...
resource "aws_autoscaling_group" "jenkins_asg" {
depends_on = ["module.vpc.aws_nat_gateway.private_nat_gw.1", "module.vpc.aws_nat_gateway.private_nat_gw.0"]
そしてこのエラーが発生しました
Initializing the backend...
Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.
Error: aws_autoscaling_group.jenkins_asg: resource depends on non-existent module 'vpc.aws_nat_gateway.private_nat_gw.1'
Error: aws_autoscaling_group.jenkins_asg: resource depends on non-existent module 'vpc.aws_nat_gateway.private_nat_gw.0'
編集2:
VPC モジュールの NAT ゲートウェイからの public_ip 出力を Jenkins モジュールへの入力として追加しようとしました。NAT ゲートウェイが起動して準備ができるまで Jenkins モジュールが待機することを期待していました。これは機能しませんでした。Terraform とモジュールで観察したところ、Jenkins ユーザーデータなど、どこかで変数を使用しない限り、変数は完全に無視されます。モジュールへの入力として持つだけでは不十分で、そのモジュール内のリソースへの入力である必要があります。副作用として、計算された値として、毎回リソースを再作成しようとします。
答え1
jbardinに感謝https://github.com/hashicorp/terraform/issues/14056
これを修正するには、VPC モジュールからの出力を使用する必要があります。aws_nat_gateway
属性を使用することもできますpublic_ip
が、その後にルートを作成したので、aws_nat_gateway
代わりにそれを使用しました。次に、Jenkins モジュールにダミー変数とダミーリソースを作成しました。
resource "null_resource" "dummy" {
provisioner "local-exec" {
command = "echo ${var.dummy}"
}
}
dummy
選択した出力にその変数を割り当てるようにしてください。また、変数は文字列である必要があります。dummy = "${join(",", module.vpc.private_nat_gw_routes)}"
その後、depends_on = ["null_resource.dummy"]
ASG リソースで使用しました。これにより、そのリソースは NAT ゲートウェイ + ルートが作成されるまで待機するようになりましたが、毎回リソースを再作成するという厄介な副作用はありません。
No changes. Infrastructure is up-to-date.
This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed
答え2
モジュールでは使用できないかもしれませdepends_on
んが、モジュールが作成する実際の NAT ゲートウェイ リソースでは使用できると確信しています。Terraform のバージョンによっては、使用してterraform state list
調べるだけで簡単に取得できる場合もあれば、Terraform の状態ファイルを手動で検索して取得する場合もあります。
しかし、どうやら11.xシリーズではまだサポートされていませんHashiCorp チームのメンバーが、ここで明らかに必要なものを提供する方法を提案しているため、Terraform の将来のバージョンではそうなる可能性があります。
問題の核心は、depends_on
モジュールがリソースに展開される前のコンパイル段階で実行されるため、そのパラメータでターゲットにすることができなくなることです。