[英]How to add a new resource to an existing resource group in Terraform
This would appear to be a fairly simple and basic scenario but I'm frankly at a loss on how to get around this using Terraform and would appreciate any suggestions.这似乎是一个相当简单和基本的场景,但坦率地说,我不知道如何使用 Terraform 解决这个问题,并希望得到任何建议。
The issue is this.问题是这个。 In Azure, I have a number of resource groups, each containing a number of resources, including virtual networks, subnets, storage accounts, etc. What I would now like to do is add new resources to one or two of the resource groups.在 Azure 中,我有多个资源组,每个资源组都包含多个资源,包括虚拟网络、子网、存储帐户等。我现在要做的是将新资源添加到一两个资源组中。 Typical example, I would like to provision a new virtual machine in each of the resource groups.典型示例,我想在每个资源组中配置一个新的虚拟机。
Now, so far all of the documentation and blogs I seem to come across only provide guidance on how to create resources whereby you also create a new resource group, vnet, subnet, from scratch.现在,到目前为止,我似乎遇到的所有文档和博客都只提供了有关如何创建资源的指导,您还可以从头开始创建新的资源组、vnet、子网。 This is definitely not what I wish to do.这绝对不是我想做的。
All I'm looking to do is get Terraform to add a single virtual machine to an existing resource group, going on to configure it to connect to existing networking resources such as a VNet, Subnet, etc. Any ideas?我想要做的就是让 Terraform 将单个虚拟机添加到现有资源组,继续配置它以连接到现有网络资源,例如 VNet、子网等。有什么想法吗?
If you just want to be able to reference your existing resources in your TF script, you normally would use date sources in TF to fetch their information.如果您只想能够在 TF 脚本中引用现有资源,通常会使用 TF 中的 日期源来获取它们的信息。
So for resource group, you would use data source azurerm_resource_group , for vnet there is azurerm_virtual_network and so forth.因此,对于资源组,您将使用数据源azurerm_resource_group ,对于 vnet 有azurerm_virtual_network等等。
These data sources would allow you to only reference and get details of existing resources, not to manage them in your TF script.这些数据源将允许您仅参考和获取现有资源的详细信息,而不是在您的 TF 脚本中管理它们。 Thus if you would like to actually manage these resources using TF (modify, delete, etc), you would have to import them first to TF.因此,如果您想使用 TF 实际管理这些资源(修改、删除等),您必须先将它们导入到 TF。
I tested for ECS by destroying the launch configuration.我通过破坏启动配置来测试 ECS。
terraform destroy -target module.ecs.module.ec2_alb.aws_launch_configuration.launchcfg
I recreated the launch configuration and it worked:我重新创建了启动配置并且它起作用了:
terraform plan -target=module.ecs.module.ec2_alb.aws_launch_configuration
terraform apply -target=module.ecs.module.ec2_alb.aws_launch_configuration
Also, you can go read more on Terraform target here: https://learn.hashicorp.com/tutorials/terraform/resource-targeting此外,您可以在此处阅读有关 Terraform 目标的更多信息: https : //learn.hashicorp.com/tutorials/terraform/resource-targeting
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.