我们日常工作中,不可能只是创建一个instance这一个任务,我们要给instance创建静态IP地址,我们要给他创建security group,我们要为instance 创建ELB等等…
那么当我们一个tf文件中存在多个资源(resource)的时候,他们之间的依赖关系以及创建的先后顺序我们该如何处理
我们来看一下新的文件内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
provider "aws" { profile = "default" region = "us-east-1" } resource "aws_instance" "jeremyzhang-test-instance" { ami = "ami-b374d5a5" instance_type = "t2.micro" tags = { Name = "jeremy-test" } } resource "aws_eip" "ip" { vpc = true instance = aws_instance.jeremyzhang-test-instance.id } |
例子中我们在创建insance的资源之后,又创建了一个新的资源(Resource),类型是aws_eip 这个资源的名字叫做: ip (注意,并不是我们创建在资源在aws中的Name叫ip)
注意,新的资源ip中有一个参数是instance,这个参数其实就是让我们输入我们的Elastic IP 创建完成后分配给哪个instance ,这个时候我们输入了
aws_instance.jeremyzhang-test-instance.id 这个命名结构为:资源类型.资源的名字.资源中的具体参数
让我们尝试运行脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
terraform apply An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_eip.ip will be created + resource "aws_eip" "ip" { + allocation_id = (known after apply) + association_id = (known after apply) + domain = (known after apply) + id = (known after apply) + instance = (known after apply) + network_interface = (known after apply) + private_dns = (known after apply) |
等我们输入yes以后
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes aws_instance.jeremyzhang-test-instance: Creating... aws_instance.jeremyzhang-test-instance: Still creating... [10s elapsed] aws_instance.jeremyzhang-test-instance: Still creating... [20s elapsed] aws_instance.jeremyzhang-test-instance: Still creating... [30s elapsed] aws_instance.jeremyzhang-test-instance: Creation complete after 38s [id=i-id] aws_eip.ip: Creating... aws_eip.ip: Creation complete after 6s [id=eipalloc-sdab32f1b] Apply complete! Resources: 2 added, 0 changed, 0 destroyed. |
这个时候我们要注意,因为我第二个resource使用到了第一个resource的id,这个id必须是第一个resource创建成功后才会有的,Terraform会隐式依赖的方式决定以什么顺序来创建资源
但是有些时候,资源与资源之间没有这种显示的调用,但实际上确实是存在依赖关系的,这个时候我们就需要使用 depends_on
举个例子,我们要创建的ec2 需要用一个特殊的S3,但是具体的调用关系是在代码中调用的, 并不是显示的声明依赖关系
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# New resource for the S3 bucket our application will use. resource "aws_s3_bucket" "example" { # NOTE: S3 bucket names must be unique across _all_ AWS accounts, so # this name must be changed before applying this example to avoid naming # conflicts. bucket = "terraform-getting-started-guide" acl = "private" } # Change the aws_instance we declared earlier to now include "depends_on" resource "aws_instance" "example" { ami = "ami-2757f631" instance_type = "t2.micro" # Tells Terraform that this EC2 instance must be created only after the # S3 bucket has been created. depends_on = [aws_s3_bucket.example] } |
正如例子中解释的,我们的aws_instance资源 depend_on aws_s3_bucket.example,也就是等同于告诉Terraform要先创建s3然后创建ec2
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021