postgresql 无法从本地计算机连接到AWS RDS Postgres示例

h9vpoimq  于 2023-05-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(158)

我使用下面的Terraform manifest成功地为Postgres创建了AWS RDS示例,但无法从我的本地计算机中建立连接以进行测试。尝试超时。

- host: blog.abcdefg12345.eu-west-1.rds.amazonaws.com
- port: 5432
- name: mydatabase
- user: myusername
- pass: mypassword

我检查了这两篇文章,但仍然找不到任何明显的东西。

resource "aws_db_instance" "blog" {
  identifier        = "blog"
  engine            = "postgres"
  engine_version    = "12.10"
  instance_class    = "db.t2.micro"
  storage_type      = "gp2"
  allocated_storage = 5

  db_name  = "mydatabase"
  username = "myusername"
  password = "mypassword"

  publicly_accessible = true
  skip_final_snapshot = true
}

tfstate(隐藏)

{
  "version": 4,
  "terraform_version": "1.4.4",
  "serial": 7,
  "lineage": "324343434-fd2f-vv7d-2ac1-11f1011d222v",
  "outputs": {
    "blog_database": {
      "value": "blog.abcdefg12345.eu-west-1.rds.amazonaws.com:5432",
      "type": "string"
    }
  },
  "resources": [
    {
      "mode": "managed",
      "type": "aws_db_instance",
      "name": "blog",
      "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
      "instances": [
        {
          "schema_version": 1,
          "attributes": {
            "address": "blog.abcdefg12345.eu-west-1.rds.amazonaws.com",
            "allocated_storage": 5,
            "allow_major_version_upgrade": null,
            "apply_immediately": null,
            "arn": "arn:aws:rds:eu-west-1:123456789012:db:blog",
            "auto_minor_version_upgrade": true,
            "availability_zone": "eu-west-1a",
            "backup_retention_period": 0,
            "backup_window": "00:22-00:52",
            "ca_cert_identifier": "rds-ca-2019",
            "character_set_name": "",
            "copy_tags_to_snapshot": false,
            "customer_owned_ip_enabled": false,
            "db_name": "mydatabase",
            "db_subnet_group_name": "default",
            "delete_automated_backups": true,
            "deletion_protection": false,
            "domain": "",
            "domain_iam_role_name": "",
            "enabled_cloudwatch_logs_exports": null,
            "endpoint": "blog.abcdefg12345.eu-west-1.rds.amazonaws.com:5432",
            "engine": "postgres",
            "engine_version": "12.10",
            "engine_version_actual": "12.10",
            "final_snapshot_identifier": null,
            "hosted_zone_id": "ABCDEFGHIJKLMNO",
            "iam_database_authentication_enabled": false,
            "id": "blog",
            "identifier": "blog",
            "identifier_prefix": "",
            "instance_class": "db.t2.micro",
            "iops": 0,
            "kms_key_id": "",
            "latest_restorable_time": "",
            "license_model": "postgresql-license",
            "maintenance_window": "thu:01:29-thu:01:59",
            "max_allocated_storage": 0,
            "monitoring_interval": 0,
            "monitoring_role_arn": "",
            "multi_az": false,
            "name": "mydatabase",
            "nchar_character_set_name": "",
            "network_type": "IPV4",
            "option_group_name": "default:postgres-12",
            "parameter_group_name": "default.postgres12",
            "password": "mypassword",
            "performance_insights_enabled": false,
            "performance_insights_kms_key_id": "",
            "performance_insights_retention_period": 0,
            "port": 5432,
            "publicly_accessible": true,
            "replica_mode": "",
            "replicas": [],
            "replicate_source_db": "",
            "resource_id": "db-ABCDEFGHIJKLMNOPRSTOPUWYZ",
            "restore_to_point_in_time": [],
            "s3_import": [],
            "security_group_names": null,
            "skip_final_snapshot": true,
            "snapshot_identifier": null,
            "status": "available",
            "storage_encrypted": false,
            "storage_type": "gp2",
            "tags": null,
            "tags_all": {},
            "timeouts": null,
            "timezone": "",
            "username": "myusername",
            "vpc_security_group_ids": [
              "sg-0053deef5e6484f4f"
            ]
          },
          "sensitive_attributes": [],
          "private": "qwertyuio123456789"
        }
      ]
    }
  ],
  "check_results": null
}
q8l4jmvw

q8l4jmvw1#

除了将publicly_accessible设置为true之外,您的数据库至少需要一个允许在端口5432上进行连接的安全组。
例如,您将需要一个类似于以下内容的安全组:

resource "aws_security_group" "postgres_sg" {
  name   = "postgres-security-group"
  vpc_id = .... # specify your VPC id
  ingress {
    protocol    = "tcp"
    from_port   = 5432
    to_port     = 5432
    cidr_blocks = ["0.0.0.0/0"]  # This will open connectivity to everybody, you could use your public IP for better security
  }

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

您可以使用vpc_security_group_ids属性将此安全组关联到数据库:

resource "aws_db_instance" "blog" {
  identifier        = "blog"
  engine            = "postgres"
  engine_version    = "12.10"
  ...
  vpc_security_group_ids = [aws_security_group.postgres_sg.id] # It has to be an array
}

相关问题