Skip to content

Commit 435ab46

Browse files
authored
docs(blog): update terraform post (#6324)
1 parent 326c5d1 commit 435ab46

File tree

1 file changed

+159
-5
lines changed

1 file changed

+159
-5
lines changed

documentation/blog/2023-11-28-terraform-aws.md documentation/blog/2024-09-12-terraform-aws.md

+159-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ image: https://refine.ams3.cdn.digitaloceanspaces.com/blog/2023-11-28-terraform-
88
hide_table_of_contents: false
99
---
1010

11+
**This article was last updated on September 12, 2024, to add sections on State Management in Terraform, Using Terraform Modules, Managing Secrets and Sensitive Data, Workspaces for Multi-Environment Management, Security Best Practices, Automating Terraform with CI/CD, Handling Resource Dependencies, and Cost Estimation with Terraform.**
12+
1113
## Introduction
1214

1315
Managing infrastructure across multiple environments and regions can be an operational headache for teams as applications scale. Provisioning resources manually is boring and time-consuming while scripting the process requires significant engineering effort.
@@ -20,14 +22,18 @@ Whether you're already using AWS or looking to explore it with an IaC approach,
2022

2123
Steps we'll cover:
2224

23-
- [AWS Account and Credentials](#aws-account-and-credentials)
24-
- [AWS CLI](#aws-cli)
25-
- [Terraform](#terraform)
2625
- [Configuring AWS Credentials](#configuring-aws-credentials)
27-
- [Access Keys and Secret Keys](#access-keys-and-secret-keys)
28-
- [~/.aws/credentials File](#awscredentials-file)
2926
- [Creating a Simple Configuration](#creating-a-simple-configuration)
27+
- [State Management in Terraform](#state-management-in-terraform)
28+
- [Terraform Workspaces for Multi-Environment Management](#terraform-workspaces-for-multi-environment-management)
29+
- [Security Best Practices](#security-best-practices)
30+
- [Automating Terraform with CI/CD Pipelines](#automating-terraform-with-cicd-pipelines)
31+
- [Handling Resource Dependencies](#handling-resource-dependencies)
3032
- [Modifying Infrastructure](#modifying-infrastructure)
33+
- [Cleaning Up](#cleaning-up)
34+
- [Cost Estimation with Terraform](#cost-estimation-with-terraform)
35+
- [Managing Secrets and Sensitive Data](#managing-secrets-and-sensitive-data)
36+
- [Using Terraform Modules](#using-terraform-modules)
3137

3238
## Prerequisites
3339

@@ -165,6 +171,103 @@ Terraform reads your config file and outlines what infrastructure it will create
165171

166172
Run `terraform apply` and Terraform will call out to AWS and create the EC2 instance.
167173

174+
## State Management in Terraform
175+
176+
Terraform maintains a **state file** that tracks the infrastructure it manages. To ensure consistency and collaboration across teams, we can recommend storing this state file in a remote backend like **AWS S3**, which prevents conflicts when multiple team members are working on the same infrastructure.
177+
178+
Example of remote state configuration with S3:
179+
180+
```hcl
181+
terraform {
182+
backend "s3" {
183+
bucket = "my-terraform-state-bucket"
184+
key = "terraform.tfstate"
185+
region = "us-west-2"
186+
}
187+
}
188+
```
189+
190+
By configuring remote state, team members can access the same state file, avoiding overwrites.
191+
192+
## Terraform Workspaces for Multi-Environment Management
193+
194+
A section on **workspaces** will show how users can manage different environments (dev, staging, production) within the same configuration. Workspaces allow Terraform to handle separate states for each environment without duplicating configuration files.
195+
196+
Example of using workspaces:
197+
198+
```bash
199+
terraform workspace new dev
200+
terraform workspace new prod
201+
terraform workspace select dev
202+
```
203+
204+
## Security Best Practices
205+
206+
It’s important to highlight enforcing security best practices using **IAM roles and policies**. We could also introduce **Terraform Sentinel** to enforce security policies during the `plan` and `apply` stages.
207+
208+
Example of assigning an IAM role to an EC2 instance:
209+
210+
```hcl
211+
resource "aws_iam_instance_profile" "ec2_profile" {
212+
name = "example_profile"
213+
role = aws_iam_role.ec2_role.name
214+
}
215+
216+
resource "aws_instance" "example" {
217+
ami = "ami-0fc5d935ebf8bc3bc"
218+
instance_type = "t2.micro"
219+
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
220+
}
221+
```
222+
223+
## Automating Terraform with CI/CD Pipelines
224+
225+
Integrating Terraform into CI/CD pipelines like **GitHub Actions** or **Jenkins** will automate infrastructure deployments. A section could include a GitHub Actions workflow example that automates `terraform plan` and `terraform apply`.
226+
227+
Example of a GitHub Actions workflow:
228+
229+
```yaml
230+
name: Terraform Apply
231+
on:
232+
push:
233+
branches:
234+
- main
235+
236+
jobs:
237+
terraform:
238+
runs-on: ubuntu-latest
239+
steps:
240+
- uses: actions/checkout@v2
241+
- name: Setup Terraform
242+
uses: hashicorp/setup-terraform@v1
243+
- name: Terraform Init
244+
run: terraform init
245+
- name: Terraform Plan
246+
run: terraform plan
247+
- name: Terraform Apply
248+
run: terraform apply -auto-approve
249+
```
250+
251+
This allows teams to automate infrastructure provisioning and management, ensuring faster, more reliable deployments.
252+
253+
## Handling Resource Dependencies
254+
255+
While Terraform automatically manages resource dependencies, users may need to explicitly define dependencies in some cases. A section on using `depends_on` will help clarify resource creation order.
256+
257+
Example of managing resource dependencies:
258+
259+
```hcl
260+
resource "aws_instance" "web" {
261+
ami = "ami-0fc5d935ebf8bc3bc"
262+
instance_type = "t2.micro"
263+
}
264+
265+
resource "aws_elb" "web_elb" {
266+
depends_on = [aws_instance.web]
267+
instances = [aws_instance.web.id]
268+
}
269+
```
270+
168271
## Modifying Infrastructure
169272
170273
A key advantage of infrastructure as code is how you can evolve your stack over time through code changes.
@@ -222,6 +325,57 @@ Properly cleaning up infrastructure keeps your accounts lean, extends the experi
222325
223326
You can recreate your stack easily at any time by running `terraform apply`. Your configuration code serves as the single source of truth.
224327
328+
## Cost Estimation with Terraform
329+
330+
Finally, we could include a section on cost estimation using tools like **Infracost**. This helps users estimate the cost of infrastructure before deployment, preventing unexpected AWS bills.
331+
332+
Example of cost estimation with Infracost:
333+
334+
```bash
335+
infracost breakdown --path=./path_to_your_terraform
336+
```
337+
338+
This provides insight into the cost impact of infrastructure decisions.
339+
340+
## Managing Secrets and Sensitive Data
341+
342+
Handling secrets like AWS access keys securely is crucial. We can include a section on managing sensitive data using **environment variables** or **AWS Secrets Manager**. This ensures sensitive information isn’t hardcoded in configuration files.
343+
344+
Example of using environment variables for secrets:
345+
346+
```bash
347+
export AWS_ACCESS_KEY_ID=<your_access_key>
348+
export AWS_SECRET_ACCESS_KEY=<your_secret_key>
349+
```
350+
351+
In the Terraform configuration:
352+
353+
```hcl
354+
variable "secret_key" {
355+
type = string
356+
sensitive = true
357+
}
358+
```
359+
360+
## Using Terraform Modules
361+
362+
Modules help you **re-use and organize infrastructure code**, which is particularly useful for repetitive tasks like setting up EC2 instances or VPCs. A section on creating and using modules will help users keep their code DRY (Don’t Repeat Yourself).
363+
364+
Example of using a module for EC2 instances:
365+
366+
```hcl
367+
module "ec2_instance" {
368+
source = "./modules/ec2"
369+
instance_type = "t2.micro"
370+
ami = "ami-0fc5d935ebf8bc3bc"
371+
tags = {
372+
Name = "refine-dev"
373+
}
374+
}
375+
```
376+
377+
By reusing modules, users can create standardized components and avoid redundant code.
378+
225379
## Conclusion
226380
227381
Through this getting started, hopefully, you now appreciate why contractors, startups, and enterprises alike are adopting IaC practices with Terraform. I touched on several benefits like configuration files serving as the single source of truth.

0 commit comments

Comments
 (0)