Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AWS SSM #7

Closed
bobbyiliev opened this issue Jan 31, 2025 · 1 comment
Closed

Add support for AWS SSM #7

bobbyiliev opened this issue Jan 31, 2025 · 1 comment

Comments

@bobbyiliev
Copy link
Collaborator

Currently, we create the necessary RDS objects like databases using a Kubernetes job from the Helm terraform module.

Ideally, we would like to have a better control over that and possibly use the Postgres Terraform module instead.

We can use an EC2 bastion host running AWS SSM Session Manager to tunnel access to RDS, eg:

resource "aws_instance" "bastion" {
  ami                    = data.aws_ami.amazon_linux.id
  instance_type          = "t3.micro"
  subnet_id              = var.database_subnet_ids[0] # Inside the RDS VPC
  vpc_security_group_ids = [aws_security_group.bastion.id]

  iam_instance_profile = aws_iam_instance_profile.ssm_instance_profile.name

  tags = { Name = "${local.name_prefix}-bastion" }
}

resource "aws_iam_role" "ssm_role" {
  name = "${local.name_prefix}-ssm-role"

  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Action = "sts:AssumeRole"
      Effect = "Allow"
      Principal = { Service = "ec2.amazonaws.com" }
    }]
  })
}

resource "aws_iam_instance_profile" "ssm_instance_profile" {
  name = "${local.name_prefix}-ssm-profile"
  role = aws_iam_role.ssm_role.name
}

resource "aws_iam_role_policy_attachment" "ssm_core" {
  role       = aws_iam_role.ssm_role.name
  policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}

Then, the Terraform PostgreSQL provider can connect via the bastion’s private IP.

provider "postgresql" {
  host            = aws_instance.bastion.private_ip
  port            = 5432
  database        = var.database_name
  username        = var.database_username
  password        = var.database_password
  sslmode         = "require"
}
@bobbyiliev
Copy link
Collaborator Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant