Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ override.tf.json

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*
.DS_Store
.DS_Store

scripts
235 changes: 129 additions & 106 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,178 +1,201 @@
# terraform-awsAdd commentMore actions
Terraform AWS provisioning examples for beginners
# terraform-aws

## Hit the Star! ⭐
If you are planning to use this Terraform repo for learning, please hit the star. Thanks!
Examples for provisioning AWS with Terraform using simple environment layouts.

## Terraform Init With s3 Backend
If this repo helps you, please ⭐ it.

```
terraform init \
-backend-config="key=dev/vpc.tfstate" \
-backend-config="bucket=dcube-terraform-state" \
-backend-config="region=us-west-2" \
-backend-config="dynamodb_table=terraform-state-lock" \
-var-file=../../../vars/dev/vpc.tfvars
```
---

#### VPC Provisioning
## Table of Contents

cd into the `environments/dev/vpc` directory and run the following commands:
- [Prerequisites](#prerequisites)
- [Init with S3 Backend](#init-with-s3-backend)
- [VPC Provisioning](#vpc-provisioning)
- [RDS Provisioning](#rds-provisioning)
- [ALB + ASG Provisioning](#alb--asg-provisioning)
- [EC2 Provisioning](#ec2-provisioning)
- [AWS Organization Tag Policy](#aws-organization-tag-policy)
- [Terraform Command Reference](#terraform-command-reference)
- [Notes](#notes)

1. Init Terraform in the directory `environments/dev/vpc`
---

```
terraform init
```
2. To preview the changes in code
## Prerequisites

```
terraform plan -var-file=../../../vars/dev/vpc.tfvars
```
3. To apply the changes
- Terraform installed
- AWS credentials configured (e.g., `aws configure`)
- S3 bucket and DynamoDB table for remote state and locking (if using the backend example)

```
terraform apply -var-file=../../../vars/dev/vpc.tfvars
```
4. To destroy the resources created using the code
---

```
terraform destroy -var-file=../../../vars/dev/vpc.tfvars

## Init with S3 Backend

#### RDS Provisioning
Use this when your workspace uses an S3 remote backend with DynamoDB locking.

cd into the `environments/dev/rds` directory and run the following commands:
```sh
terraform init \
-backend-config="key=dev/vpc.tfstate" \
-backend-config="bucket=dcube-terraform-state" \
-backend-config="region=us-west-2" \
-backend-config="dynamodb_table=terraform-state-lock" \
-var-file=../../../vars/dev/vpc.tfvars
````

1. Init Terraform in the directory `environments/dev/rds`
> Adjust `key`, `bucket`, `region`, and `dynamodb_table` to your setup.

```
---

## VPC Provisioning

From `environments/dev/vpc`:

```sh
# 1) Initialize
terraform init
```
2. To preview the changes in code

```
terraform plan -var-file=../../../vars/dev/rds.tfvars
```
3. To apply the changes
# 2) Preview
terraform plan -var-file=../../../vars/dev/vpc.tfvars

```
terraform apply -var-file=../../../vars/dev/rds.tfvars
```
4. To destroy the resources created using the code
# 3) Apply
terraform apply -var-file=../../../vars/dev/vpc.tfvars

```
terraform destroy -var-file=../../../vars/dev/rds.tfvars
# 4) Destroy (when needed)
terraform destroy -var-file=../../../vars/dev/vpc.tfvars
```

### ALB and ASG Provisioning
---

cd into the `environments/dev/alb-asg` directory and run the following commands:
## RDS Provisioning

1. Init Terraform in the directory `environments/dev/alb-asg`
From `environments/dev/rds`:

```
```sh
terraform init
terraform plan -var-file=../../../vars/dev/rds.tfvars
terraform apply -var-file=../../../vars/dev/rds.tfvars
terraform destroy -var-file=../../../vars/dev/rds.tfvars
```
2. To preview the changes in code

```
terraform plan -var-file=../../../vars/dev/alb-asg.tfvars
```
3. To apply the changes
---

```
terraform apply -var-file=../../../vars/dev/alb-asg.tfvars
```
4. To destroy the resources created using the code
## ALB + ASG Provisioning

```
From `environments/dev/alb-asg`:

```sh
terraform init
terraform plan -var-file=../../../vars/dev/alb-asg.tfvars
terraform apply -var-file=../../../vars/dev/alb-asg.tfvars
terraform destroy -var-file=../../../vars/dev/alb-asg.tfvars
```

## EC2 Instance Provisioning
---

## EC2 Provisioning

1. Navigate to the `environment/dev` folder:
From `environments/dev/ec2` (edit `vars/dev/ec2.tfvars` first):

```bash
cd environment/dev
```sh
terraform init
terraform plan -var-file=../../../vars/dev/ec2.tfvars
terraform apply -var-file=../../../vars/dev/ec2.tfvars
terraform destroy -var-file=../../../vars/dev/ec2.tfvars
```

2. Open the `ec2.tfvars` file and modify it with your desired details. This file contains variables used in the Terraform configuration.
> Always review the plan before applying.

### Deployment
---

1. Initialize Terraform in the working directory:
## AWS Organization Tag Policy

```bash
From `environments/dev/tag-policy` (edit `vars/dev/tag-policy.tfvars` as needed):

```sh
terraform init
terraform plan -var-file=../../../vars/dev/tag-policy.tfvars
terraform apply -var-file=../../../vars/dev/tag-policy.tfvars
terraform destroy -var-file=../../../vars/dev/tag-policy.tfvars
```

2. Create an execution plan:
---

```bash
terraform plan -var-file=../../../vars/dev/ec2.tfvars
```
## EKS + Karpenter Deployment

3. Apply the changes to create the EC2 instance:
This section covers deploying an Amazon EKS cluster with Karpenter for dynamic node provisioning.

```bash
terraform apply -var-file=../../../vars/dev/ec2.tfvars
```
### Prerequisites

4. To destroy the EC2 instance and associated resources:
- AWS CLI configured with necessary permissions
- `kubectl` installed
- AWS IAM permissions for EKS and Karpenter operations

```bash
terraform destroy -var-file=../../../vars/dev/ec2.tfvars
```
### 1. Deploy EKS Cluster

**Note**: Always review the execution plan (`terraform plan`) before applying changes to avoid unintended modifications.
From `infra/eks-cluster`:

## AWS Organization Tag Policy Creation.
```sh
# Initialize with backend config
terraform init

1. Navigate to the `environment/dev` folder:
# Plan and review changes
terraform plan -var-file=../../vars/dev/eks.tfvars

```bash
cd environment/tag-policy
# Apply the configuration
terraform apply -var-file=../../vars/dev/eks.tfvars
```

2. Open the `tag-policy.tfvars` file and modify it with your desired details. This file contains variables used in the Terraform configuration.

### Deployment
### 2. Deploy Karpenter

1. Initialize Terraform in the working directory:
From `infra/eks-karpenter`:

```bash
```sh
# Initialize with backend config
terraform init
```

2. Create an execution plan:
# Plan and review changes
terraform plan -var-file=../../vars/dev/karpenter.tfvars

```bash
terraform plan -var-file=../../../vars/dev/tag-policy.tfvars
# Apply the configuration
terraform apply -var-file=../../vars/dev/karpenter.tfvars
```

3. Apply the changes to create the Tag Policy:
### 3. Verify Installation

```bash
terraform apply -var-file=../../../vars/dev/tag-policy.tfvars
```
After deployment:

4. To destroy the Tag Policy:
```sh
# Update kubeconfig
aws eks update-kubeconfig --region <your-region> --name <cluster-name>

```bash
terraform destroy -var-file=../../../vars/dev/tag-policy.tfvars
# Verify Karpenter pods
kubectl get pods -n karpenter
```

**Note**: Always review the execution plan (`terraform plan`) before applying changes to avoid unintended modifications.
---

## Terraform Command Reference

Update all outputs:
Refresh state (re-reads remote objects and updates outputs):

```sh
terraform refresh
```

Show current state and outputs:

```sh
terraform show
```

---

## Notes

<pre>terraform refresh</pre>
* Run Terraform **inside** an environment folder (e.g., `environments/dev/vpc`).
* Variable files live under `vars/dev/*.tfvars`.
* Backend config values in examples are placeholders—change them to match your infra.

Show all outputs:
---

<pre>terraform show</pre>

2 changes: 1 addition & 1 deletion infra/eks-karpenter/backend.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
backend "s3" {
bucket = "techies-terraform"
key = "dev/eks/eks.tfstate"
key = "dev/eks-karpenter/eks.tfstate"
region = "us-east-1"
use_lockfile = true
}
Expand Down
Loading