Skip to content

Instantly share code, notes, and snippets.

@Bharathkumarraju
Created September 9, 2019 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bharathkumarraju/49e3501ca9a191b1ea23467293b40aa2 to your computer and use it in GitHub Desktop.
Save Bharathkumarraju/49e3501ca9a191b1ea23467293b40aa2 to your computer and use it in GitHub Desktop.
Terraform - Create database password securely and store in remote-state
provider "aws" {
region = var.aws_region
profile = var.aws_profile
}
terraform {
backend "s3" {
bucket = "bharaths-terraform-up-and-running"
key = "development/data-stores/mysql/terraform.tfstate"
region = "us-east-2"
dynamodb_table = "bharaths-terraform-up-and-running-locks"
encrypt = true
}
}
resource "random_string" "db_password" {
length = 16
special = true
override_special = "!#()-[]<>"
}
resource "aws_db_instance" "bharaths_mysql" {
instance_class = "db.t2.micro"
identifier_prefix = "bharaths-terraform-up-and-running"
engine = "mysql"
allocated_storage = 10
name = "bharaths_example_database"
username = "bharath_admin"
skip_final_snapshot = true
apply_immediately = true
password = random_string.db_password.result
lifecycle {
ignore_changes = ["password"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment