23
loading...
This website collects cookies to deliver better user experience
terraform.tfstate
in plain text! With that in mind, I set S3 as my backend and encrypted it. The key is the path to where we want to store the state file.resource "aws_s3_bucket" "terraform_state" {
bucket = "terraform-state-acgappperf"
versioning {
enabled = true
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
}
terraform {
backend "s3" {
bucket = "terraform-state-acgappperf"
region = "us-east-1"
key = "global/s3/terraform.tfstate"
encrypt = true
}
}
variable "db_username" {
description = "The username for the DB master user"
type = string
sensitive = true
}
variable "db_password" {
description = "The password for the DB master user"
type = string
sensitive = true
}
# Set secrets via environment variables
export TF_VAR_username=(the username)
export TF_VAR_password=(the password)
#When you run Terraform, it'll pick up the secrets automatically
terraform apply
template\_file
function and the provisioner "file"
block to pass through the variables to configure the Postgres RDS.data "template_file" "init" {
template = file("./user-data.sh.tpl")
vars = {
DBUSER = var.db_username
DBPASS = var.db_password
DBNAME = aws_db_instance.acg-db.name
DBHOST = aws_db_instance.acg-db.address
REDISHOST = aws_elasticache_cluster.acg-redis.cache_nodes[0].address
}
}
def fetch(sql):
ttl = 10 # Time to live in seconds
try:
params = config(section='redis')
cache = redis.Redis.from_url(params['url'])
result = cache.get(sql)
if result:
print('Redis result')
return result
else:
# connect to database listed in database.ini
conn = connect()
cur = conn.cursor()
cur.execute(sql)
# fetch one row
result = cur.fetchone()
print('Closing connection to database...')
cur.close()
conn.close()
# cache result
cache.setex(sql, ttl, ''.join(result))
return result