【问题标题】:Cant lunch EC2 with Terraform无法使用 Terraform 启动 EC2
【发布时间】:2021-09-20 01:04:41
【问题描述】:

尝试使用新的 vpc、rtb、igw 和子网创建新的基础架构 在这个创建的子网中,我想部署一个 EC2,但我遇到了网络问题

我加倍检查,subnet & sg 连接到同一个 VPC

我得到错误:

Error: Error launching source instance: InvalidParameter: 
Security group sg-0d1250aeb8fa894ef and subnet subnet-628e252e belong to different networks

代码:

  region     = "eu-central-1"
  access_key = "X"
  secret_key = "X"
}

resource "aws_vpc" "vpc" {
  cidr_block       = "10.0.0.0/16"
}

resource "aws_subnet" "subnet-1" {
  vpc_id     = aws_vpc.vpc.id
  cidr_block = "10.0.1.0/24"
}

resource "aws_internet_gateway" "gw" {
  vpc_id = aws_vpc.vpc.id
}

resource "aws_route_table" "rtb" {
  vpc_id = aws_vpc.vpc.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.gw.id
  }
}

resource "aws_security_group" "terra-sg" {
  name        = "terra-sg"
  description = "Allow SSH inbound traffic"
  vpc_id      = aws_vpc.vpc.id

  ingress {
    description      = "SSH connection"
    from_port        = 22
    to_port          = 22
    protocol         = "tcp"
    cidr_blocks      = ["0.0.0.0/0"]
  }

  egress {
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "ec2" {
  ami                         = "ami-00f22f6155d6d92c5"
  instance_type               = "t2.micro"
  associate_public_ip_address = true
  key_name = "X"
  vpc_security_group_ids = [aws_security_group.terra-sg.id]
}

【问题讨论】:

    标签: amazon-web-services networking amazon-ec2 terraform


    【解决方案1】:

    您需要为您的实例指定子网 ID:

    resource "aws_instance" "ec2" {
      ami                         = "ami-00f22f6155d6d92c5"
      instance_type               = "t2.micro"
      associate_public_ip_address = true
      key_name = "X"
      vpc_security_group_ids = [aws_security_group.terra-sg.id]
      subnet_id = aws_subnet.subnet-1.id
    }
    

    【讨论】:

      猜你喜欢
      • 2022-10-22
      • 2021-11-13
      • 2020-10-09
      • 2020-03-10
      • 2023-04-02
      • 1970-01-01
      • 2018-06-24
      • 2021-11-10
      • 2018-07-03
      相关资源
      最近更新 更多