公共IP未从Terraform代码中提取到实例中

huangapple go评论106阅读模式
英文:

Public IP not coming in instances from Terraform code

问题

要求上述代码如下所示:

公司希望体系结构具有以下服务:

  1. 创建具有VPC、2个子网和每个子网中的1个实例的模板。
  2. 将安全组、互联网网关和网络接口附加到实例。

以下是 Terraform 代码,未在实例中获取公共 IP:

  1. # 块设置
  2. terraform {
  3. required_providers {
  4. aws = {
  5. source = "hashicorp/aws"
  6. }
  7. }
  8. }
  9. # 提供者
  10. provider "aws" {
  11. profile = "default"
  12. region = "us-east-2"
  13. }
  14. # VPC
  15. resource "aws_vpc" "TF_VPC" {
  16. cidr_block = "170.31.0.0/16"
  17. tags = {
  18. Name = "TF_VPC"
  19. }
  20. }
  21. # 子网
  22. resource "aws_subnet" "TF_Subnet1" {
  23. vpc_id = aws_vpc.TF_VPC.id
  24. cidr_block = "170.31.1.0/24"
  25. availability_zone = "us-east-2a"
  26. tags = {
  27. Name = "TF_Subnet1"
  28. }
  29. }
  30. resource "aws_subnet" "TF_Subnet2" {
  31. vpc_id = aws_vpc.TF_VPC.id
  32. cidr_block = "170.31.2.0/24"
  33. availability_zone = "us-east-2b"
  34. tags = {
  35. Name = "TF_Subnet2"
  36. }
  37. }
  38. # 安全组
  39. resource "aws_security_group" "TF_SG" {
  40. vpc_id = aws_vpc.TF_VPC.id
  41. # 允许来自任何地方的 SSH 访问
  42. ingress {
  43. from_port = 22
  44. to_port = 22
  45. protocol = "tcp"
  46. cidr_blocks = ["0.0.0.0/0"]
  47. }
  48. # 允许来自任何地方的 HTTP 访问
  49. ingress {
  50. from_port = 80
  51. to_port = 80
  52. protocol = "tcp"
  53. cidr_blocks = ["0.0.0.0/0"]
  54. }
  55. egress {
  56. from_port = 0
  57. to_port = 0
  58. protocol = "-1"
  59. cidr_blocks = ["0.0.0.0/0"]
  60. }
  61. tags = {
  62. Name = "TF_SG"
  63. }
  64. }
  65. # 互联网网关
  66. resource "aws_internet_gateway" "TF_IGW" {
  67. vpc_id = aws_vpc.TF_VPC.id
  68. tags = {
  69. Name = "TF_IGW"
  70. }
  71. }
  72. # 路由表
  73. resource "aws_route_table" "TF_RT" {
  74. vpc_id = aws_vpc.TF_VPC.id
  75. route {
  76. cidr_block = "0.0.0.0/0"
  77. gateway_id = aws_internet_gateway.TF_IGW.id
  78. }
  79. tags = {
  80. Name = "TF_RT"
  81. }
  82. }
  83. # 路由表关联
  84. resource "aws_route_table_association" "TF_RTA1" {
  85. subnet_id = aws_subnet.TF_Subnet1.id
  86. route_table_id = aws_route_table.TF_RT.id
  87. }
  88. resource "aws_route_table_association" "TF_RTA2" {
  89. subnet_id = aws_subnet.TF_Subnet2.id
  90. route_table_id = aws_route_table.TF_RT.id
  91. }
  92. # 网络接口
  93. resource "aws_network_interface" "TF_NI1" {
  94. subnet_id = aws_subnet.TF_Subnet1.id
  95. private_ips = ["170.31.1.5"]
  96. security_groups = [aws_security_group.TF_SG.id]
  97. tags = {
  98. Name = "TF_NI1"
  99. }
  100. }
  101. resource "aws_network_interface" "TF_NI2" {
  102. subnet_id = aws_subnet.TF_Subnet2.id
  103. private_ips = ["170.31.2.5"]
  104. security_groups = [aws_security_group.TF_SG.id]
  105. tags = {
  106. Name = "TF_NI2"
  107. }
  108. }
  109. # EC2 实例
  110. resource "aws_instance" "TF_instance1" {
  111. ami = "ami-024e6efaf93d85776"
  112. instance_type = "t2.micro"
  113. key_name = "assign.ohio"
  114. network_interface {
  115. network_interface_id = aws_network_interface.TF_NI1.id
  116. device_index = 0
  117. }
  118. tags = {
  119. Name = "TF_instance1"
  120. }
  121. }
  122. resource "aws_instance" "TF_instance2" {
  123. ami = "ami-024e6efaf93d85776"
  124. instance_type = "t2.micro"
  125. key_name = "assign.ohio"
  126. network_interface {
  127. network_interface_id = aws_network_interface.TF_NI2.id
  128. device_index = 0
  129. }
  130. tags = {
  131. Name = "TF_instance2"
  132. }
  133. }

所有的东西都被创建,但 EC2 实例没有获取公共 IP。如果我在实例下使用以下代码:

  1. resource "aws_instance" "TF_instance1" {
  2. ami = "ami-024e6efaf93d85776"
  3. instance_type = "t2.micro"
  4. associate_public_ip_address = true ## <-- 添加此行
  5. key_name = "assign.ohio"
  6. network_interface {
  7. network_interface_id = aws_network_interface.TF_NI1.id
  8. device_index = 0
  9. }
  10. tags = {
  11. Name = "TF_instance1"
  12. }
  13. }

会出现以下错误:

  1. 错误:冲突的配置参数
  2. with aws_instance.TF_instance1,
  3. on main.tf line 127, in resource "aws_instance" "TF_instance1":
  4. 127: resource "aws_instance" "TF_instance1" {
  5. "network_interface": conflicts with associate_public_ip_address
英文:

Requirement for above code is as below:
The company wants the Architecture to have the following services:

  1. Create a template with a VPC, 2 subnets and 1 instance in each subnet
  2. Attach Security groups, internet gateway and network interface to the instance

**Public IP not coming in instances from below Terraform code:
**

  1. # Block Settings
  2. terraform {
  3. required_providers {
  4. aws = {
  5. source = &quot;hashicorp/aws&quot;
  6. }
  7. }
  8. }
  9. # Provider
  10. provider &quot;aws&quot; {
  11. profile = &quot;default&quot;
  12. region = &quot;us-east-2&quot;
  13. }
  14. # VPC
  15. resource &quot;aws_vpc&quot; &quot;TF_VPC&quot; {
  16. cidr_block = &quot;170.31.0.0/16&quot;
  17. tags = {
  18. Name = &quot;TF_VPC&quot;
  19. }
  20. }
  21. # Subnets
  22. resource &quot;aws_subnet&quot; &quot;TF_Subnet1&quot; {
  23. vpc_id = aws_vpc.TF_VPC.id
  24. cidr_block = &quot;170.31.1.0/24&quot;
  25. availability_zone = &quot;us-east-2a&quot;
  26. tags = {
  27. Name = &quot;TF_Subnet1&quot;
  28. }
  29. }
  30. resource &quot;aws_subnet&quot; &quot;TF_Subnet2&quot; {
  31. vpc_id = aws_vpc.TF_VPC.id
  32. cidr_block = &quot;170.31.2.0/24&quot;
  33. availability_zone = &quot;us-east-2b&quot;
  34. tags = {
  35. Name = &quot;TF_Subnet2&quot;
  36. }
  37. }
  38. # Security Group
  39. resource &quot;aws_security_group&quot; &quot;TF_SG&quot; {
  40. vpc_id = aws_vpc.TF_VPC.id
  41. # Allow SSH access from anywhere
  42. ingress {
  43. from_port = 22
  44. to_port = 22
  45. protocol = &quot;tcp&quot;
  46. cidr_blocks = [&quot;0.0.0.0/0&quot;]
  47. }
  48. # Allow HTTP access from anywhere
  49. ingress {
  50. from_port = 80
  51. to_port = 80
  52. protocol = &quot;tcp&quot;
  53. cidr_blocks = [&quot;0.0.0.0/0&quot;]
  54. }
  55. egress {
  56. from_port = 0
  57. to_port = 0
  58. protocol = &quot;-1&quot;
  59. cidr_blocks = [&quot;0.0.0.0/0&quot;]
  60. }
  61. tags = {
  62. Name = &quot;TF_SG&quot;
  63. }
  64. }
  65. # Internet Gateway
  66. resource &quot;aws_internet_gateway&quot; &quot;TF_IGW&quot; {
  67. vpc_id = aws_vpc.TF_VPC.id
  68. tags = {
  69. Name = &quot;TF_IGW&quot;
  70. }
  71. }
  72. # Route Table
  73. resource &quot;aws_route_table&quot; &quot;TF_RT&quot; {
  74. vpc_id = aws_vpc.TF_VPC.id
  75. route {
  76. cidr_block = &quot;0.0.0.0/0&quot;
  77. gateway_id = aws_internet_gateway.TF_IGW.id
  78. }
  79. tags = {
  80. Name = &quot;TF_RT&quot;
  81. }
  82. }
  83. # Route Table Association
  84. resource &quot;aws_route_table_association&quot; &quot;TF_RTA1&quot; {
  85. subnet_id = aws_subnet.TF_Subnet1.id
  86. route_table_id = aws_route_table.TF_RT.id
  87. }
  88. resource &quot;aws_route_table_association&quot; &quot;TF_RTA2&quot; {
  89. subnet_id = aws_subnet.TF_Subnet2.id
  90. route_table_id = aws_route_table.TF_RT.id
  91. }
  92. # Network Interface
  93. resource &quot;aws_network_interface&quot; &quot;TF_NI1&quot; {
  94. subnet_id = aws_subnet.TF_Subnet1.id
  95. private_ips = [&quot;170.31.1.5&quot;]
  96. security_groups = [aws_security_group.TF_SG.id]
  97. tags = {
  98. Name = &quot;TF_NI1&quot;
  99. }
  100. }
  101. resource &quot;aws_network_interface&quot; &quot;TF_NI2&quot; {
  102. subnet_id = aws_subnet.TF_Subnet2.id
  103. private_ips = [&quot;170.31.2.5&quot;]
  104. security_groups = [aws_security_group.TF_SG.id]
  105. tags = {
  106. Name = &quot;TF_NI2&quot;
  107. }
  108. }
  109. # EC2 Instances
  110. resource &quot;aws_instance&quot; &quot;TF_instance1&quot; {
  111. ami = &quot;ami-024e6efaf93d85776&quot;
  112. instance_type = &quot;t2.micro&quot;
  113. key_name = &quot;assign.ohio&quot;
  114. network_interface {
  115. network_interface_id = aws_network_interface.TF_NI1.id
  116. device_index = 0
  117. }
  118. tags = {
  119. Name = &quot;TF_instance1&quot;
  120. }
  121. }
  122. resource &quot;aws_instance&quot; &quot;TF_instance2&quot; {
  123. ami = &quot;ami-024e6efaf93d85776&quot;
  124. instance_type = &quot;t2.micro&quot;
  125. key_name = &quot;assign.ohio&quot;
  126. network_interface {
  127. network_interface_id = aws_network_interface.TF_NI2.id
  128. device_index = 0
  129. }
  130. tags = {
  131. Name = &quot;TF_instance2&quot;
  132. }
  133. }

All the things are getting created but EC2 instances not getting public IP.
If I use below code under instance:

  1. ami = &quot;ami-024e6efaf93d85776&quot;
  2. instance_type = &quot;t2.micro&quot;
  3. associate_public_ip_address = true ## &lt;-- Add this
  4. key_name = &quot;assign.ohio&quot;
  5. network_interface {
  6. network_interface_id = aws_network_interface.TF_NI1.id
  7. device_index = 0
  8. }
  9. tags = {
  10. Name = &quot;TF_instance1&quot;
  11. }
  12. }

Getting error as below:

  1. with aws_instance.TF_instance1,
  2. on main.tf line 127, in resource &quot;aws_instance&quot; &quot;TF_instance1&quot;:
  3. 127: resource &quot;aws_instance&quot; &quot;TF_instance1&quot; {
  4. &quot;network_interface&quot;: conflicts with associate_public_ip_address

答案1

得分: 1

你需要在你的EC2实例上关联公共IP,示例如下:

  1. resource "aws_instance" "TF_instance2" {
  2. ami = "ami-024e6efaf93d85776"
  3. instance_type = "t2.micro"
  4. associate_public_ip_address = true ## <-- 添加这一行
  5. key_name = "assign.ohio"
  6. network_interface {
  7. network_interface_id = aws_network_interface.TF_NI2.id
  8. device_index = 0
  9. }
  10. tags = {
  11. Name = "TF_instance2"
  12. }
  13. }

查看文档以供参考。

英文:

You need to associate public IP on your EC2 instance, example below:

  1. resource &quot;aws_instance&quot; &quot;TF_instance2&quot; {
  2. ami = &quot;ami-024e6efaf93d85776&quot;
  3. instance_type = &quot;t2.micro&quot;
  4. associate_public_ip_address = true ## &lt;-- Add this
  5. key_name = &quot;assign.ohio&quot;
  6. network_interface {
  7. network_interface_id = aws_network_interface.TF_NI2.id
  8. device_index = 0
  9. }
  10. tags = {
  11. Name = &quot;TF_instance2&quot;
  12. }
  13. }

Check the docs for reference.

答案2

得分: 0

以下是翻译好的部分:

  • 如果您能ping一下您收到的错误,那将是很好的。
  • 或者,您可以创建弹性 IP 并附加到实例上。
  • 尝试以下代码:
  1. resource "aws_instance" "TF_instance2" {
  2. ami = "ami-024e6efaf93d85776"
  3. instance_type = "t2.micro"
  4. associate_public_ip_address = true ## <-- 添加这一行
  5. key_name = "assign.ohio"
  6. network_interface {
  7. network_interface_id = aws_network_interface.TF_NI2.id
  8. device_index = 1
  9. }
  10. tags = {
  11. Name = "TF_instance2"
  12. }
  13. }

请注意,我已将HTML实体编码(如")还原为原始文本。

英文:

It would be good if you ping the error you are receiving.
Alternatively, you can create Elastic IP and attach to the instance.

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip

Try the below code:

  1. resource &quot;aws_instance&quot; &quot;TF_instance2&quot; {
  2. ami = &quot;ami-024e6efaf93d85776&quot;
  3. instance_type = &quot;t2.micro&quot;
  4. associate_public_ip_address = true ## &lt;-- Add this
  5. key_name = &quot;assign.ohio&quot;
  6. network_interface {
  7. network_interface_id = aws_network_interface.TF_NI2.id
  8. device_index = 1
  9. }
  10. tags = {
  11. Name = &quot;TF_instance2&quot;
  12. }
  13. }

huangapple
  • 本文由 发表于 2023年7月10日 18:00:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定