Skip to content

Instantly share code, notes, and snippets.

@moinuddin14
Created December 13, 2022 06:54
Show Gist options
  • Save moinuddin14/8ce7ebc899e5ea65c80e0b68001a8bf9 to your computer and use it in GitHub Desktop.
Save moinuddin14/8ce7ebc899e5ea65c80e0b68001a8bf9 to your computer and use it in GitHub Desktop.
Terraform snippet here that would instantiate a Postgres RDS in AWS (Sydney region)
# Have the provider and terraform block of code in a file called provider.tf
provider "aws" {
region = "ap-southeast-2"
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
# Have the resource "aws_rds_instance" in a file called main.tf
resource "aws_db_instance" {
db_name = var.rds_inst_id
engine = "postgres"
engine_version = 14
instance_class = var.instance_class
allocated_storage = var.allocated_storage
storage_type = var.storage_type
username = var.postgres_username
password = var.postgres_password
skip_final_snapshot = true
}
# Place the below variable(s) in a seperate file called variables.tf
variable "rds_inst_id" {
type = string
description = "RDS Instance Identifer"
default = "demo-rds-instance"
}
variable "instance_class" {
type = string
description = "RDS Instance Class"
default = "db.t2.micro"
}
variable "allocated_storage" {
type = number
description = "RDS Instance Allocated Storage"
default = "db.t2.micro"
}
variable "storage_type" {
type = string
description = "RDS Instance Storage Type"
default = "gp2"
}
variable "postgres_username" {
description = "RDS Instance Database administrator username"
type = string
sensitive = true
default = "secureusername"
}
variable "postgres_password" {
description = "RDS Instance Database administrator password"
type = string
sensitive = true
default = "securepassword"
}
# Create an outputs.tf file to log mentioned values to the console output
output "id" {
description = "The RDS Instance Identifier."
value = aws_db_instance.main.id
}
output "resource_id" {
description = "The RDS Resource ID of this instance"
value = aws_db_instance.main.resource_id
}
output "arn" {
description = "The ARN of the RDS instance"
value = aws_db_instance.main.arn
}
output "endpoint" {
description = "The hostname of the RDS instance."
value = aws_db_instance.main.address
}
output "port" {
description = "The database port."
value = aws_db_instance.main.port
}
output "username" {
description = "Username for the master DB user."
value = aws_db_instance.main.username
}
output "database_name" {
description = "The database name."
value = aws_db_instance.main.name
}
# Create a file called secrets.tfvars to fill in the values for database username and password
postgres_username = "admin"
postgres_password = "insecurepassword"
# To execute the terraform code use `terraform apply -var-file="secret.tfvars"`
# If we want to override the secrets, then we can do the following
# export TF_VAR_postgres_username=supersecureusername TF_VAR_postgres_password=supersecurepassword
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment