admin管理员组文章数量:1390756
I have a terraform script which is creating an EC2 instance with efs, ebs, etc. I ran the script once and it has created a EC2 instance.
Now I want to run the terraform script again to spin up a new EC2 instance without deleting the existing one, how can I get this done, what changes needs to be made?
Can anyone help me on this?
Thanks
I tried to change the resource names, but that didn't work.
I have a terraform script which is creating an EC2 instance with efs, ebs, etc. I ran the script once and it has created a EC2 instance.
Now I want to run the terraform script again to spin up a new EC2 instance without deleting the existing one, how can I get this done, what changes needs to be made?
Can anyone help me on this?
Thanks
I tried to change the resource names, but that didn't work.
Share Improve this question asked Mar 12 at 12:01 Aditya JainAditya Jain 11 bronze badge 1 |2 Answers
Reset to default 1Terraform is a "desired state" system, which means that your configuration must describe the objects that ought to exist in the remote system after terraform apply
is finished, and then Terraform proposes the changes required to make that be true.
Since you've already run one plan/apply round, all of the objects you described in the configuration now already exist and so there is nothing else for Terraform to do. If you want Terraform to create an additional EC2 instance without modifying what was already created then you need to modify your configuration to tell Terraform that there should be two EC2 instances, and then plan/apply again.
The most straightforward way to achieve your desired result is to write another resource "aws_instance"
block without removing or modifying the existing one. You will then have a configuration that declares two EC2 instances, and Terraform will notice that one of them already exists and so will only propose to create the new one.
If you expect that you'll need to frequently change the number of EC2 instances, there are some other options:
You can use the special
count
argument to tell Terraform that you intend your one resource block to declare a dynamically-decided number of instances. For example, if you addcount = 2
to your existing block then Terraform should notice that there is currently only one instance and so propose to create a second one.In some cases it's better to let AWS itself manage the multiple instances, rather than Terraform directly, using autoscaling groups. In this case instead of telling Terraform to manage individual EC2 instances you instead tell Terraform to configure a remote AWS object called an "autoscaling group", and then the remote AWS service itself uses that to create multiple EC2 instances based on the rules you specify.
This approach is particularly helpful when you are scaling horizontally because you can configure EC2 to ensure that there are always a certain minimum number of identical instances available, and if one of them fails or becomes degraded then the autoscaling system will automatically replace it without you needing to re-run Terraform. This is a service provided by AWS itself rather than a Terraform feature, but you can use Terraform to configure it.
Terraform manages resources based on the desired state defined in your configuration. If you've already applied a configuration that creates one EC2 instance, simply running terraform apply again won’t create a new instance unless you change the configuration to indicate that two should exist.
Two Approaches:
Duplicate Resource Block:
Create another aws_instance resource block with a different name:
Copy
resource "aws_instance" "example1" {
# your existing configuration
}
resource "aws_instance" "example2" {
# duplicate configuration or modifications as needed
}
Use the count Meta-Argument:
Modify your existing resource to use count so that Terraform expects more than one instance. For example, if you want two instances:
Copy
resource "aws_instance" "example" {
count = 2
# rest of your configuration
}
Terraform will see that only one instance exists (indexed as [0]) and create a second one (indexed as [1]).
CLI Commands: After updating your configuration, run the following commands:
bash Copy
# Check the execution plan
terraform plan
# Apply the changes
terraform apply
These commands will show you what Terraform plans to do (create a new instance) and then apply the change to create the additional EC2 instance.
This way, Terraform manages both instances as part of your defined desired state.
本文标签: amazon web servicesHow to create new EC2 with existing Terraform ScriptStack Overflow
版权声明:本文标题:amazon web services - How to create new EC2 with existing Terraform Script - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744754061a2623353.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
count
orfor_each
meta-arguments are used to avoid code repetition. That would also require using import after the fact. Also, please take a look here on how to ask a good question: stackoverflow/help/how-to-ask. – Marko E Commented Mar 12 at 12:32