Skip to the page

AWS interview question · hard

What are the best practices for managing sensitive information, such as AWS credentials, in Terraform without hardcoding them?

The short answerStoring sensitive information like AWS credentials, database passwords, or API keys directly in Terraform configuration files is a security risk.

The full answer

Storing sensitive information like AWS credentials, database passwords, or API keys directly in Terraform configuration files is a security risk. Instead, use the following strategies to securely manage secrets while provisioning infrastructure with Terraform.

1. Use AWS IAM Roles for Authentication

Instead of hardcoding AWS credentials in Terraform, configure IAM roles and attach them to EC2 instances, Lambda functions, or other AWS services. This allows AWS services to authenticate securely via IAM without requiring static credentials.

  • If running Terraform from an EC2 instance, assign an IAM Role with the necessary permissions.
  • If using AWS Cloud9 or AWS CodeBuild, AWS credentials can be dynamically inherited from the environment.

2. Use Environment Variables for AWS Credentials

AWS provides a way to configure credentials via environment variables, which Terraform automatically picks up:

export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"

This ensures credentials are not stored in Terraform files but remain available during execution.

3. Use AWS Credentials File (~/.aws/credentials)

AWS CLI users can store credentials in the ~/.aws/credentials file, and Terraform will automatically detect and use them.

  • Set up credentials using AWS CLI:
    aws configure
    
  • Verify stored credentials in ~/.aws/credentials:
    [default]
    aws_access_key_id=your-access-key
    aws_secret_access_key=your-secret-key
    

4. Store Sensitive Variables in Terraform Variable Files or AWS Secrets Manager

Instead of writing secrets directly in main.tf, define them in a separate variable file and exclude it from version control using .gitignore.

  • Define variables in variables.tf:
    variable "db_password" {}
    
  • Pass the variable securely during runtime:
    terraform apply -var="db_password=supersecurepassword"
    

Alternatively, AWS Secrets Manager can be used to store and retrieve secrets securely, and Terraform can access them dynamically.

5. Use Terraform Cloud or Vault for Secrets Management

  • Terraform Cloud provides a remote backend to securely store and manage sensitive variables.
  • HashiCorp Vault allows dynamic secrets management and access control via policies.

6. Use .gitignore to Prevent Leaking Sensitive Files

Ensure that sensitive files like terraform.tfvars or backend credentials are excluded from version control to prevent accidental exposure.

Example .gitignore entries:

terraform.tfvars
*.auto.tfvars
.terraform/

Tecyfy Takeaway: Never hardcode AWS credentials or sensitive information in Terraform files. Instead, use IAM roles, environment variables, AWS Secrets Manager, and Terraform Vault to manage secrets securely. This approach enhances security, prevents credential leaks, and ensures compliance with best practices.

Last updated 12 February 2025