Terraform Getting Started

This repository demonstrates a step-by-step approach to learning Terraform, from basic concepts to advanced features like modules, remote execution, workspaces, and AWS Secrets Manager integration.
Getting Started
-
Clone this repository:
git clone https://github.com/anveshmuppeda/terraform-getting-started.git
cd terraform-getting-started -
Follow the section guides below for each example.
Structure
.
├── 001-simple-example
├── 002-var-example
├── 003-aws-provider
├── 004-ec2-app
├── 005-simple-module
├── 006-s3-backend
├── 007-remote-exec
├── 008-workspaces
├── 009-awssm-secret
├── 010-rds-awssm
├── 011-rds-awssm-advanced
├── 012-import-example
├── 013-refresh-example
└── README.md
Guide
001-simple-example
- Goal: Introduce the basic Terraform syntax and workflow.
- Files:
main.tf - How to use:
cd 001-simple-exampleterraform initterraform apply
002-var-example
- Goal: Demonstrate input variables.
- Files:
main.tf,variables.tf - How to use:
cd 002-var-example- Edit
variables.tfto set variable defaults or use-varon the CLI. terraform initterraform apply
003-aws-provider
- Goal: Configure the AWS provider and use variables.
- Files:
main.tf,provider.tf,variables.tf,var.tfvars - How to use:
cd 003-aws-provider- Set your AWS credentials (via env vars or AWS CLI).
terraform initterraform apply -var-file=var.tfvars
004-ec2-app
- Goal: Deploy a simple EC2 instance with user data.
- Files:
main.tf,provider.tf,variables.tf,terraform.tfvars - How to use:
cd 004-ec2-app- Update
terraform.tfvarswith your AMI ID and key pair. terraform initterraform apply
005-simple-module
- Goal: Introduce modules for reusable infrastructure.
- Files:
main.tf,modules/ec2-app/main.tf,modules/ec2-app/variables.tf - How to use:
cd 005-simple-moduleterraform initterraform apply
006-s3-backend
- Goal: Use an S3 backend for remote state storage.
- Files:
backend.tf,main.tf,provider.tf,variables.tf,terraform.tfvars - How to use:
cd 006-s3-backend- Edit
backend.tfwith your S3 bucket details. terraform initterraform apply
007-remote-exec
- Goal: Use
remote-execandfileprovisioners to configure EC2 after launch. - Files:
main.tf,index.html,modules/ec2-app/main.tf,modules/ec2-app/variables.tf - How to use:
cd 007-remote-exec- Update variables and
index.htmlas needed. terraform initterraform apply
008-workspaces
- Goal: Manage multiple environments using workspaces and variable files.
- Files:
main.tf,variables.tf,dev.tfvars,staging.tfvars,prod.tfvars,modules/ec2-app/main.tf,modules/ec2-app/variables.tf - How to use:
cd 008-workspaces- Create/select a workspace:
terraform workspace new dev
terraform workspace select dev - Apply with environment-specific variables:
terraform apply -var-file=dev.tfvars - Repeat for
stagingandprodas needed.
009-awssm-secret
- Goal: Use AWS Secrets Manager to inject secrets into your Terraform-managed infrastructure.
- Files:
main.tf,variables.tf,terraform.tfvars,modules/ec2-app/main.tf,modules/ec2-app/variables.tf - How to use:
cd 009-awssm-secret- Ensure you have a secret named
terraform-demo-secretin AWS Secrets Manager with a JSON structure (e.g.,{"username": "myappuser"}). - Update
terraform.tfvarswith your AMI ID, instance type, and other variables as needed. terraform initterraform apply- The EC2 instance will use the secret value (e.g.,
username) as the instance name.
010-rds-awssm
-
Goal: Provision an AWS RDS MySQL instance with credentials managed in AWS Secrets Manager using Terraform modules.
-
Files:
main.tfprovider.tfmodules/secretmanager/main.tf,modules/secretmanager/variables.tfmodules/rds/main.tf,modules/rds/variables.tf
-
How to use:
cd 010-rds-awssm- Edit
main.tfto set your desired username and password for the secret (or use variables). terraform initterraform apply- After creation, connect to your RDS instance using the endpoint, username, and password stored in Secrets Manager.
- Example MySQL connection:
(You can find
mysql -h <rds-endpoint> -P 3306 -u <username> -p<rds-endpoint>in the AWS RDS console or Terraform outputs.)
-
Notes:
- Make sure your local MySQL client is compatible (MySQL 8.x recommended).
- The RDS instance will use the credentials stored in AWS Secrets Manager, managed by the
secretmanagermodule. - Security group and networking setup may be required to allow inbound connections from your IP.
011-rds-awssm-adv
-
Goal: Provision an AWS RDS MySQL instance with credentials securely generated and stored in AWS Secrets Manager, and store all DB connection details (host, port, db name, username, password) in a separate secret for application use.
-
Files:
main.tfmodules/secretmanager/main.tf,modules/secretmanager/variables.tfmodules/rds/main.tf,modules/rds/variables.tf
-
How to use:
cd 011-rds-awssm-adv- Edit
main.tfto set your desired username for the secret (password will be randomly generated). terraform initterraform apply- After creation, you will have:
- A secret in AWS Secrets Manager with the DB credentials (username & random password).
- A separate secret in AWS Secrets Manager containing all connection info (endpoint, port, db name, username, password).
- An RDS instance using these credentials.
- Example MySQL connection:
(You can find
mysql -h <rds-endpoint> -P 3306 -u <username> -p<rds-endpoint>, username, and password in the connection info secret.)
-
Notes:
- The password is generated only once and remains stable unless you taint or change the random password resource.
- Do not overwrite the credentials secret with connection info; always use a separate secret for connection details.
- Security group and networking setup may be required to allow inbound connections from your IP.
012-import-example
-
Goal: Import an existing AWS EC2 instance (created manually) into Terraform management.
-
Files:
main.tfimported-resources.tf(generated during import, then merged intomain.tf)
-
How to use:
- Create the EC2 instance manually in the AWS Console.
- Create a
main.tfwith an import block:import {
id = "i-0e924e12540ecfa2f"
to = aws_instance.imported_ec2_example
} - Initialize Terraform:
terraform init - Generate the resource configuration from the existing instance:
terraform plan -generate-config-out=imported-resources.tf - Move the generated resource configuration from
imported-resources.tfinto yourmain.tf. - Import the resource into Terraform state:
terraform import aws_instance.imported_ec2_example i-0e924e12540ecfa2f - Now, your manually created EC2 instance is managed by Terraform!
-
Notes:
- After import, you can manage, update, or destroy the instance using Terraform as you would with any other resource.
- Always review the generated configuration and adjust tags or settings as needed to match your infrastructure standards.
013-refresh-example
-
Goal: Demonstrate how to use
terraform apply -refresh-onlyto sync Terraform state with real infrastructure changes made outside of Terraform. -
Files:
main.tf
-
How to use:
cd 013-refresh-example- Deploy an EC2 instance using
terraform apply. - Make a manual change to the EC2 instance in the AWS Console (e.g., add or modify a tag).
- Run:
This updates the Terraform state file to reflect the real infrastructure.
terraform apply -refresh-only - Run:
Terraform will show any differences between your configuration and the actual state (e.g., tags present in state but not in
terraform planmain.tf). - To bring your configuration in sync, manually update your
main.tffile as needed and runterraform applyagain.
-
Notes:
terraform apply -refresh-onlyonly updates the state file; it does not change your infrastructure or configuration files.- Always review and manually update your configuration files to match desired state after using refresh-only.
Prerequisites
- Terraform installed
- AWS account and credentials configured (
aws configureor environment variables) - An existing AWS key pair for EC2 instances
Happy Terraforming!