Hello everyone,
I’m trying to create a new instance of AWS documentdb with a vpc and a subnet for this vpc. Below is the code:
# Create a VPC for the DocumentDB cluster
resource "aws_vpc" "documentdb_vpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "documentdb-vpc"
}
}
# Create a subnet for the DocumentDB cluster
resource "aws_subnet" "documentdb_subnet" {
vpc_id = aws_vpc.documentdb_vpc.id
cidr_block = "10.0.0.0/24"
tags = {
Name = "documentdb-subnet"
}
}
# Create a security group for the DocumentDB cluster
resource "aws_security_group" "documentdb_sg" {
vpc_id = aws_vpc.documentdb_vpc.id
ingress {
from_port = 27017
to_port = 27017
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"]
}
tags = {
Name = "documentdb-security-group"
}
}
# Create the DocumentDB cluster
resource "aws_docdb_cluster" "documentdb_cluster" {
cluster_identifier = "my-documentdb-cluster"
engine = "docdb"
engine_version = "5.0.0"
master_username = "admin" # Update with your desired master username
master_password = "password123"
port = 27017
storage_encrypted = true
backup_retention_period = 7
preferred_backup_window = "07:00-09:00"
vpc_security_group_ids = [aws_security_group.documentdb_sg.id]
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
}
resource "aws_docdb_cluster_instance" "cluster_instances" {
count = 2
identifier = "docdb-cluster-demo-${count.index}"
cluster_identifier = aws_docdb_cluster.documentdb_cluster.id
instance_class = "db.r5.large"
}
# Output the connection string
output "connection_string" {
value = aws_docdb_cluster.documentdb_cluster.endpoint
}
The following error is ocurring after apply:
Error: creating DocumentDB Cluster (my-documentdb-cluster): InvalidParameterCombination: The DB instance and EC2 security group are in different VPCs. The DB instance is in vpc-c33546bb and the EC2 security group is in vpc-0b95b3d5d5a0e0ed1
│ status code: 400, request id: a12f74fa-09ea-49ce-97ba-712be42543ec
│
│ with aws_docdb_cluster.documentdb_cluster,
│ on main.tf line 44, in resource "aws_docdb_cluster" "documentdb_cluster":
│ 44: resource "aws_docdb_cluster" "documentdb_cluster" {
Note that I’m not trying to create a new ec2 instance.
Can you help me?
Thanks.