Hardcoding credentials directly in Terraform provider blocks is a common but dangerous practice. This approach exposes sensitive information to anyone with access to the code repository, increasing the risk of unauthorized access and potential breaches[1].
Common patterns of credential exposure include:
These practices can lead to severe security incidents, as demonstrated by numerous real-world breaches where exposed cloud credentials were exploited by malicious actors[1].
Bad Practice
1provider "aws" {
2 access_key = "AKIAXXXXXXXXXXXXXXXX"
3 secret_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
4 region = "us-west-2"
5}
Good Practice
provider "aws" {
region = "us-west-2"
assume_role {
role_arn = "arn:aws:iam::ACCOUNT_ID:role/DeployRole"
}
}
To mitigate the risks associated with hardcoded credentials, consider the following secure patterns:
By adopting these practices, you can significantly reduce the risk of credential exposure and unauthorized access to your cloud resources.
Granting excessive permissions to IAM roles is a common pitfall in Terraform configurations. This often stems from the desire for quick setup or lack of understanding of the principle of least privilege. Overly permissive roles can lead to:
Bad Practice
resource "aws_iam_role_policy" "overly_permissive" {
name = "overly_permissive_policy"
role = aws_iam_role.example.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = "*"
Resource = "*"
}
]
})
}
Good Practice
1resource "aws_iam_role_policy" "least_privilege" {
2 name = "least_privilege_policy"
3 role = aws_iam_role.example.id
4
5 policy = jsonencode({
6 Version = "2012-10-17"
7 Statement = [
8 {
9 Effect = "Allow"
10 Action = [
11 "s3:GetObject",
12 "s3:PutObject"
13 ]
14 Resource = "arn:aws:s3:::example-bucket/*"
15 }
16 ]
17 })
18}
To implement least privilege access:
Terraform state files contain sensitive information about your infrastructure, including resource IDs, IP addresses, and sometimes even passwords or API keys. Exposing these files can lead to:
Bad Practice
terraform {
backend "local" {
path = "terraform.tfstate"
}
}
Good Practice
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "us-west-2"
encrypt = true
dynamodb_table = "terraform-locks"
}
}
To mitigate state file exposure risks:
Insecure backend configurations can lead to unauthorized access to state files, potential data breaches, and compliance issues. Common problems include:
Bad Practice
1terraform {
2 backend "s3" {
3 bucket = "my-terraform-state"
4 key = "prod/terraform.tfstate"
5 region = "us-west-2"
6 }
7}
Good Practice
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "us-west-2"
encrypt = true
kms_key_id = "arn:aws:kms:us-west-2:ACCOUNT_ID:key/KEY_ID"
dynamodb_table = "terraform-locks"
}
}
To enhance backend configuration security:
Improper workspace isolation can lead to:
Bad Practice
1resource "aws_instance" "example" {
2 ami = "ami-0c55b159cbfafe1f0"
3 instance_type = "t2.micro"
4 tags = {
5 Name = "example-instance"
6 }
7}
Good Practice
1resource "aws_instance" "example" {
2 ami = "ami-0c55b159cbfafe1f0"
3 instance_type = "t2.micro"
4 tags = {
5 Name = "example-instance-${terraform.workspace}"
6 Environment = terraform.workspace
7 }
8}
To ensure proper workspace isolation:
By addressing these common pitfalls and implementing secure practices, platform engineers can significantly enhance the security posture of their Terraform-managed infrastructure. Remember, security is an ongoing process that requires constant vigilance and adaptation to new threats and best practices.
Terraform has become an essential tool for managing cloud infrastructure as code. However, with great power comes great responsibility, especially when it comes to access management and authentication. In this guide, we'll explore common pitfalls and provide secure solutions to help platform engineers enhance their Terraform security posture.
Citations:
[1] https://www.aquasec.com/cloud-native-academy/cspm/terraform-security/
[2] https://docs.aws.amazon.com/prescriptive-guidance/latest/terraform-aws-provider-best-practices/security.html
[3] https://spacelift.io/blog/terraform-security
[4] https://mattkimber.co.uk/terraform-beginner-pitfalls/
[5] https://www.reddit.com/r/devops/comments/8ttjfr/encrypt_terraform_state_files/
[6] https://www.reddit.com/r/Terraform/comments/1dutcbv/how_to_keep_backend_config_secure/
[7] https://www.wiz.io/academy/terraform-security-best-practices