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-example
terraform init
terraform apply
002-var-example
- Goal: Demonstrate input variables.
- Files:
main.tf
,variables.tf
- How to use:
cd 002-var-example
- Edit
variables.tf
to set variable defaults or use-var
on the CLI. terraform init
terraform 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 init
terraform 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.tfvars
with your AMI ID and key pair. terraform init
terraform 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-module
terraform init
terraform 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.tf
with your S3 bucket details. terraform init
terraform apply
007-remote-exec
- Goal: Use
remote-exec
andfile
provisioners 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.html
as needed. terraform init
terraform 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
staging
andprod
as 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-secret
in AWS Secrets Manager with a JSON structure (e.g.,{"username": "myappuser"}
). - Update
terraform.tfvars
with your AMI ID, instance type, and other variables as needed. terraform init
terraform 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.tf
provider.tf
modules/secretmanager/main.tf
,modules/secretmanager/variables.tf
modules/rds/main.tf
,modules/rds/variables.tf
-
How to use:
cd 010-rds-awssm
- Edit
main.tf
to set your desired username and password for the secret (or use variables). terraform init
terraform 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
secretmanager
module. - 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.tf
modules/secretmanager/main.tf
,modules/secretmanager/variables.tf
modules/rds/main.tf
,modules/rds/variables.tf
-
How to use:
cd 011-rds-awssm-adv
- Edit
main.tf
to set your desired username for the secret (password will be randomly generated). terraform init
terraform 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.tf
imported-resources.tf
(generated during import, then merged intomain.tf
)
-
How to use:
- Create the EC2 instance manually in the AWS Console.
- Create a
main.tf
with 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.tf
into 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-only
to 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 plan
main.tf
). - To bring your configuration in sync, manually update your
main.tf
file as needed and runterraform apply
again.
-
Notes:
terraform apply -refresh-only
only 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 configure
or environment variables) - An existing AWS key pair for EC2 instances
Happy Terraforming!