Terraform Level 1, Task 20: Centralized Configuration with AWS SSM Parameter Store¶
Today's task was a fantastic dive into a core practice of modern cloud applications: centralized configuration management. My objective was to use Terraform to create a parameter in AWS Systems Manager (SSM) Parameter Store. This is the standard, secure way to store and manage configuration data like database connection strings, API keys, and feature flags.
This was a great lesson in decoupling configuration from application code. Instead of hardcoding a value in a file, I learned to store it in a central, managed service. This document is my very detailed, first-person guide to that entire process, with a deep dive into the code and the concepts behind SSM Parameter Store.
Table of Contents¶
- Terraform Level 1, Task 20: Centralized Configuration with AWS SSM Parameter Store
- Table of Contents
The Task¶
My objective was to use Terraform to create a new AWS SSM Parameter. The specific requirements were:
1. All code had to be in a single main.tf file.
2. The parameter's name had to be devops-ssm-parameter.
3. The type had to be String.
4. The value had to be devops-value.
5. The resource had to be created in the us-east-1 region.
My Step-by-Step Solution¶
The process involved writing a simple Terraform file to define the SSM parameter and then running the standard three-step workflow.
Phase 1: Writing the Code¶
In the /home/bob/terraform directory, I created my main.tf file. I wrote the following declarative code to define my SSM parameter exactly as requested.
# 1. Configure the AWS Provider to set the region
provider "aws" {
region = "us-east-1"
}
# 2. Define the SSM Parameter Resource
resource "aws_ssm_parameter" "devops_ssm_parameter_resource" {
name = "devops-ssm-parameter"
type = "String"
value = "devops-value"
tags = {
Name = "devops-ssm-parameter"
}
}
Phase 2: The Terraform Workflow¶
From my terminal in the same directory, I executed the core commands.
- Initialize:
terraform init(to download the AWS provider). - Plan:
terraform plan. The output showed me that Terraform would create oneaws_ssm_parameterresource with all my specified settings. - Apply:
terraform apply. After I confirmed withyes, Terraform created the parameter in my AWS account. The success message confirmed the task was done.
Why Did I Do This? (The "What & Why")¶
- AWS SSM Parameter Store: This is a fully managed service that provides secure, hierarchical storage for configuration data and secrets management. I learned to think of it as a central, secure "locker" for all my application's configuration values.
- The Problem it Solves (Decoupling): The biggest benefit is that it decouples my configuration from my application code. Instead of hardcoding a database password or a feature flag in a file that's checked into Git, I store it in Parameter Store. My application, when it starts up, can then query the Parameter Store API to fetch the latest configuration values.
- Security: I can use SecureString parameters to store secrets, which are encrypted using AWS KMS. I can then use fine-grained IAM policies to control exactly which users or servers are allowed to read which secrets.
- Auditability: SSM tracks the history of each parameter, so I can see who changed what and when.
- Central Management: If I need to update a database password for an application running on 100 servers, I change it in one place (the Parameter Store), and all 100 servers will pick up the new value the next time they start or refresh their configuration.
- Parameter Types:
- String (What I used): Stores a simple plain-text value. Perfect for non-sensitive data like a feature flag (true/false) or a logging level (INFO).
- StringList: Stores a comma-separated list of strings.
- SecureString: The most important type for secrets. It stores the value as encrypted text using an AWS KMS key.
Deep Dive: A Line-by-Line Explanation of My main.tf Script¶
The code for this task was very concise, but it represents a powerful serverless resource.
[Image of an application fetching config from SSM Parameter Store]
# The provider block configures the "aws" provider to work in the correct region.
provider "aws" {
region = "us-east-1"
}
# This is the resource block that defines my SSM Parameter.
# "aws_ssm_parameter" is the Resource TYPE.
# "devops_ssm_parameter_resource" is the local NAME I use to refer to this parameter
# within my Terraform code.
resource "aws_ssm_parameter" "devops_ssm_parameter_resource" {
# The 'name' argument sets the unique name for the parameter. These names can
# be hierarchical, like a file path (e.g., /my-app/prod/db-password).
name = "devops-ssm-parameter"
# The 'type' argument specifies the data type of the parameter. I used 'String'
# for this non-sensitive value. For a password, I would have used 'SecureString'.
type = "String"
# The 'value' argument is the actual data I want to store in the parameter.
value = "devops-value"
# Standard tagging to give the parameter a recognizable name.
tags = {
Name = "devops-ssm-parameter"
}
}
Common Pitfalls¶
- Parameter Name Conflicts: Parameter names must be unique within an AWS account and region. If a parameter with the same name already existed, my terraform apply command would have failed. Using a hierarchical naming convention (e.g., /project/environment/setting) is a best practice to avoid this.
- Permissions: The IAM user or role running Terraform needs ssm:PutParameter permission to create parameters. Similarly, the application that needs to read the parameter at runtime needs ssm:GetParameter permission (and kms:Decrypt if it's a SecureString).
- Storing Secrets as Plain Text: A critical mistake would be to store a real password or API key as a String type. For any sensitive data, I must use the SecureString type to ensure it is encrypted at rest.
Exploring the Essential Terraform Commands¶
This task used the main workflow, but here's a more complete list of the essential commands.
terraform init: Initializes the working directory, downloading provider plugins.terraform validate: Checks the syntax of your Terraform files.terraform fmt: Auto-formats your code to the standard style.terraform plan: A "dry run" that shows you what will be created, changed, or destroyed.terraform apply: Executes the plan and makes the changes to your cloud infrastructure.terraform show: Shows the current state of your managed infrastructure.terraform state list: Lists all the resources that Terraform is currently managing.terraform destroy: The opposite ofapply. It creates a plan to destroy all the infrastructure managed by the current configuration.