简体   繁体   English

terraform 数据库实例和 ec2 安全组在不同的 vpc 中

[英]terraform the db instance and ec2 security group are in different vpcs

i am trying to create a vpc with public and private su.net along with Aurora mysql cluster and instance in same vpc with custom security group for RDS.我正在尝试使用公共和私有 su.net 以及 Aurora mysql 集群和实例在同一 vpc 中创建一个具有 RDS 自定义安全组的 vpc。

i've created vpc (public/private su.net, custom security group) in a module.我在模块中创建了 vpc(公共/私有 su.net,自定义安全组)。 also aurora-mysql in different module.也是不同模块中的 aurora-mysql。

My vpc configuration in a module file我在模块文件中的 vpc 配置

resource "aws_vpc" "main" {
    cidr_block       = "${var.vpc_cidr}"
    instance_tenancy = "${var.tenancy}"
    enable_dns_support = "true"
    enable_dns_hostnames = "true"
   tags {
      Name = "${var.tag_name}"
   }
}

resource "aws_subnet" "main-public-1" {
   vpc_id     = "${var.vpc_id}"
   cidr_block = "${var.subnet_cidr_1}"
   availability_zone = "${var.region}a"
   map_public_ip_on_launch = true
   tags {
       Name = "${var.tag_name}-subnet1"
    }
}

resource "aws_subnet" "main-private-1" {
    count      = "${var.create_private_subnet}"
    vpc_id     = "${var.vpc_id}"
    cidr_block = "${var.private_subnet_cidr_1}"
    map_public_ip_on_launch = false
    availability_zone = "${var.region}a"

   tags {
        Name = "${var.tag_name}-private-subnet1"
    }
}
resource "aws_subnet" "main-private-2" {
    count      = "${var.create_private_subnet}"
    vpc_id     = "${var.vpc_id}"
    cidr_block = "${var.private_subnet_cidr_2}"
    map_public_ip_on_launch = false
    availability_zone = "${var.region}b"

    tags {
        Name = "${var.tag_name}-private-subnet2"
    }
}

resource "aws_security_group" "aurora-sg" {
  name   = "aurora-security-group"
  vpc_id = "${var.vpc_id}"
  ingress {
    protocol    = "tcp"
    from_port   = 0
    to_port     = 65535
    cidr_blocks = ["0.0.0.0/0"]
  }

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

My RDS configuration in a module file我在模块文件中的 RDS 配置

resource "aws_rds_cluster" "cluster" {
  cluster_identifier     = "${var.cluster_name}"
  engine                 = "aurora-mysql"
  database_name          = "sample_rds"
  master_username        = "${var.username}"
  master_password        = "${var.password}"
  vpc_security_group_ids = ["${aws_security_group.aurora-sg.id}"]
  skip_final_snapshot    = true
}

resource "aws_rds_cluster_instance" "cluster_instances" {
  identifier         = "${var.cluster_name}-instance"
  cluster_identifier = "${aws_rds_cluster.cluster.id}"
  instance_class     = "${var.instance_class}"
  publicly_accessible = "${var.publicly_accessible}"
  db_subnet_group_name    = 
        "${aws_db_subnet_group.aurora_subnet_group.id}"
}

resource "aws_db_subnet_group" "aurora_subnet_group" {
  name       = "tf-rds-${var.cluster_name}"
  subnet_ids = ["${var.subnets}"]

  tags {
    Name = "tf-rds-${var.cluster_name}"
  }
}

My main terraform script.我的主要 terraform 脚本。 i have passed variables to RDS module like vpc_id, db username and password,private su.net ids and security group id我已经将变量传递给 RDS 模块,如 vpc_id、数据库用户名和密码、私有 su.net id 和安全组 id

module "aurora_mysql" {
  source      = "../modules/rds-aurora"

  vpc_id              = "${module.my_vpc.vpc_id}"
  publicly_accessible = true
  instance_class      = "db.t2.medium"
  username            = "${var.db_username}"
  password            = "${var.db_password}"
  subnets             = 
 ["${module.my_vpc.subnet_id_1[1]}","${module.my_vpc.subnet_id_1[2]}"]
  security_group_ids = "${module.my_vpc.vpc_rds_sg_id}"
}

When i try to apply the configuration vpc created successfully with su.net and security group but get the error Error creating DB Instance: InvalidParameterCombination: DB instance and EC2 security group are in different VPC当我尝试apply使用 su.net 和安全组成功创建的配置 vpc 但收到Error creating DB Instance: InvalidParameterCombination: DB instance and EC2 security group are in different VPC

My RDS instance gets created in the default VPC even though i am passing new vpc private su.net ids and custom security group id.我的 RDS 实例是在默认 VPC 中创建的,即使我正在传递新的 VPC 私有 su.net id 和自定义安全组 id。

Maybe a bit old but i had the same problem.也许有点老,但我有同样的问题。 Maybe interesting for others who have that problem.对于有这个问题的其他人来说,这可能很有趣。 The key is the "db_subnet_group_name" in "aws_rds_cluster" or "aws_rds_cluster_instance".关键是“aws_rds_cluster”或“aws_rds_cluster_instance”中的“db_subnet_group_name”。

From the docs:从文档:

db_subnet_group_name - (Optional) Name of DB subnet group. db_subnet_group_name -(可选)数据库子网组的名称。 DB instance will be created in the VPC associated with the DB subnet group.将在与数据库子网组关联的 VPC 中创建数据库实例。 If unspecified, will be created in the default VPC...如果未指定,将在默认 VPC 中创建...

I saw that you used the "id" instead of the "name"我看到你使用了“id”而不是“name”

db_subnet_group_name    = "${aws_db_subnet_group.aurora_subnet_group.id}"

With name:与名称:

db_subnet_group_name    = "${aws_db_subnet_group.aurora_subnet_group.name}"

Maybe that was the problem.也许这就是问题所在。

DB Subnet Group is a parameter fir the cluster ( aws_rds_cluster ), and not for the instance. DB 子网组是集群 ( aws_rds_cluster ) 的参数,而不是实例的参数。 In your config, you seem to be passing the subnet group in your instance config and not in your cluster config.在您的配置中,您似乎在实例配置中而不是在集群配置中传递子网组。 I believe, this forces RDS to fallback to use the default subnet group, which is a group of subnets from your default VPC.我相信,这会迫使 RDS 回退以使用default子网组,这是来自default VPC 的一组子网。

I'm not a Terrform expert, so I'll leave it up to you to figure out what needs to change in your config to model this correctly.我不是 Terrform 专家,因此我将让您自行确定需要在配置中更改哪些内容才能正确建模。 Hope this helps!希望这可以帮助!

I faced a similar issue.我遇到了类似的问题。 Finally, after a lot of struggle, while creating PostgreSQL DB instance, I found that we need to create a resource call subnet group name with at least two subnets and call that in an instance or cluster resource.最后,经过一番折腾,在创建 PostgreSQL 数据库实例时,我发现我们需要创建一个至少有两个子网的资源调用子网组名,并在一个实例或集群资源中调用它。

Here is my sample code.这是我的示例代码。

resource "aws_db_subnet_group" "postgresql_subnet_group" {
    name       = "postgresubgroup"
    subnet_ids = ["${aws_subnet.postgresql_subnet1.id}",  
        "${aws_subnet.postgresql_subnet2.id}"]

    tags = {
        Name = "PostgreSQL subnet group"
    }
}

db_subnet_group_name = aws_db_subnet_group.postgresql_subnet_group.name

This error can be caused by vpc_id being omitted from the aws_security_group resource or being set to the wrong value.此错误可能是由于vpc_idaws_security_group资源中被省略或被设置为错误值造成的。

In your case, the vpc_id is there, but it might have the wrong value.在您的情况下, vpc_id存在,但它可能具有错误的值。 It's being set to a var.vpc_id .它被设置为var.vpc_id I'm not sure where var.vpc_id is being set, but perhaps it doesn't match the ID of your aws_vpc.main resource.我不确定var.vpc_id的设置位置,但可能它与您的aws_vpc.main资源的 ID 不匹配。 One way to guarantee that the two values match is to set vpc_id = aws_vpc.main.id instead of vpc_id = "${var.vpc_id}" .保证两个值匹配的一种方法是设置vpc_id = aws_vpc.main.id而不是vpc_id = "${var.vpc_id}"

You need to specify the same aws_db_su.net_group_name for both aws_rds_cluster and aws_rds_cluster_instance.您需要为 aws_rds_cluster 和 aws_rds_cluster_instance 指定相同的 aws_db_su.net_group_name。 Your configuration is missing the db_su.net_group_name specification for aws_rds_cluster.您的配置缺少 aws_rds_cluster 的 db_su.net_group_name 规范。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据库实例和EC2安全组在不同的VPC,cloudFormation错误 - The DB instance and EC2 security group are in different VPCs, cloudFormation error 创建数据库实例时出错:InvalidParameterCombination:数据库实例和 EC2 安全组位于不同的 VPC 中 - Error creating DB Instance: InvalidParameterCombination: The DB instance and EC2 security group are in different VPCs 在具有VPC对等功能的不同VPC上,Amazon RDS dbinstance和EC2实例的安全组规则是什么? - What are the security group rules for Amazon RDS dbinstance and EC2 instance over the different VPCs with VPC Peering? 是否可以将ec2实例移动到不同的vpc? - Is it possible to move an ec2 instance to diffreent vpcs? Terraform - 使用在单独文件中创建的安全组 ID 来创建 EC2 实例 - Terraform - Use security group ID created in separate file for EC2 instance creation 如何在 AWS 中 ping 在同一 vpc 同一子网但不同安全组中运行的 EC2 实例 - How to ping an EC2 instance running in the same vpc same subnet but different security group in AWS 使用单个云形成脚本创建超过 2 个 ec2 实例并附加不同的安全组 - Using single cloud formation script to create more then 2 ec2 instance and attach different Security group Terraform 重新部署 EC2 实例 - Terraform redeploys EC2 instance 在 Terraform 中销毁一个 EC2 实例? - Destroy an EC2 instance in Terraform? Terraform EC2 实例导入 - 用户数据不同 - Terraform EC2 instance import - user data different
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM