【问题标题】:RDS not reachable when created with terraform使用 terraform 创建时无法访问 RDS
【发布时间】:2020-08-27 01:16:43
【问题描述】:

我一直在尝试使用 terraform 创建 RDS 实例。我一直在努力解决的问题是,每次创建新实例时,它都无法访问。 我在包含公共子网和私有子网的子网组中创建它,安全组具有允许从我的 IP 访问的规则,此 vpc 中有 Internet 网关。 最奇怪的是,要解决这个问题,我只需要将使用 AWS 控制台的实例类更改为任何其他实例类,例如 f.e.从 db.t2.small 到 db.t2.micro,它突然开始工作。 这是我的代码片段:

resource "aws_db_subnet_group" "dbSubnetGroup" {
  name       = "${var.prefix}-db-subnet-group"
  subnet_ids = concat(aws_subnet.publicSubnet.*.id, aws_subnet.privateSubnet.*.id)

  tags = var.defaultTags
}

resource "aws_security_group" "rdsSecurityGroup" {
  name   = "${var.prefix}-rds-sg"
  vpc_id = aws_vpc.vpc.id

  ingress {
    from_port       = 1433
    to_port         = 1433
    protocol        = "tcp"
    security_groups = [aws_eks_cluster.eksCluster.vpc_config[0].cluster_security_group_id]
  }
  ingress {
    from_port        = 1433
    to_port          = 1433
    protocol         = "tcp"
    cidr_blocks      = [var.myIP]
  }

  egress {
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }
  tags = var.defaultTags
}

resource "random_password" "rdsPassword" {
  length           = 32
  special          = true
  override_special = "!#$%&*()-_=+[]{}<>:?"
}

resource "aws_db_instance" "dbInstance" {
  allocated_storage               = 20
  storage_type                    = "gp2"
  engine                          = var.dbInstanceEngine
  license_model                   = "license-included"
  instance_class                  = var.dbInstanceType
  identifier                      = "${var.prefix}-db-instance"
  username                        = var.dbUserName
  password                        = random_password.rdsPassword.result
  tags                            = var.defaultTags
  db_subnet_group_name            = aws_db_subnet_group.dbSubnetGroup.name
  vpc_security_group_ids          = [aws_security_group.rdsSecurityGroup.id]
  skip_final_snapshot             = true
  allow_major_version_upgrade     = true
  copy_tags_to_snapshot           = true
  performance_insights_enabled    = true
  max_allocated_storage           = 1000
  enabled_cloudwatch_logs_exports = ["error"]
  publicly_accessible             = true
}

我做错了什么还是可能是 aws 提供程序中的错误?

【问题讨论】:

  • 我想知道,由于您的子网组中混合了公共子网和私有子网,也许 rds 会为您选择私有子网。那么当您更改数据库实例时,它会选择公共子网吗?您是否观察到选择了哪些子网?
  • 我将尝试仅使用 public 创建 db,但它始终有效,当我从 aws 控制台创建实例或更改实例类型时,并且在使用 terraform 创建它之后永远不会

标签: amazon-web-services terraform amazon-rds terraform-provider-aws


【解决方案1】:

如果您希望 RDS 可连接,则 DB 子网组必须仅位于公共子网中

【讨论】:

  • 那为什么我可以在更改实例类后进行连接,而无需更改其他任何内容?
  • 如果它在公共子网和私有子网中,您可能只是在访问公共子网中的节点
  • 好的,公共子网有帮助
猜你喜欢
  • 2021-10-17
  • 2021-11-17
  • 1970-01-01
  • 2017-02-14
  • 2017-02-12
  • 2018-11-17
  • 2021-08-01
  • 2014-02-13
  • 2019-05-06
相关资源
最近更新 更多