Terraform Module Library

Terraform Module Library

Guides an AI agent in building reusable, multi-cloud Terraform modules for AWS, Azure, GCP and OCI with a standard module structure and examples.

Skill files3 files

references/

SKILL.md252 lines
1
---
2
name: terraform-module-library
3
description: Build reusable Terraform modules for AWS, Azure, GCP, and OCI infrastructure following infrastructure-as-code best practices. Use when creating infrastructure modules, standardizing cloud provisioning, or implementing reusable IaC components.
4
---
5
6
# Terraform Module Library
7
8
Production-ready Terraform module patterns for AWS, Azure, GCP, and OCI infrastructure.
9
10
## Purpose
11
12
Create reusable, well-tested Terraform modules for common cloud infrastructure patterns across multiple cloud providers.
13
14
## When to Use
15
16
- Build reusable infrastructure components
17
- Standardize cloud resource provisioning
18
- Implement infrastructure as code best practices
19
- Create multi-cloud compatible modules
20
- Establish organizational Terraform standards
21
22
## Module Structure
23
24
```
25
terraform-modules/
26
├── aws/
27
│ ├── vpc/
28
│ ├── eks/
29
│ ├── rds/
30
│ └── s3/
31
├── azure/
32
│ ├── vnet/
33
│ ├── aks/
34
│ └── storage/
35
├── gcp/
36
│ ├── vpc/
37
│ ├── gke/
38
│ └── cloud-sql/
39
└── oci/
40
├── vcn/
41
├── oke/
42
└── object-storage/
43
```
44
45
## Standard Module Pattern
46
47
```
48
module-name/
49
├── main.tf # Main resources
50
├── variables.tf # Input variables
51
├── outputs.tf # Output values
52
├── versions.tf # Provider versions
53
├── README.md # Documentation
54
├── examples/ # Usage examples
55
│ └── complete/
56
│ ├── main.tf
57
│ └── variables.tf
58
└── tests/ # Terratest files
59
└── module_test.go
60
```
61
62
## AWS VPC Module Example
63
64
**main.tf:**
65
66
```hcl
67
resource "aws_vpc" "main" {
68
cidr_block = var.cidr_block
69
enable_dns_hostnames = var.enable_dns_hostnames
70
enable_dns_support = var.enable_dns_support
71
72
tags = merge(
73
{
74
Name = var.name
75
},
76
var.tags
77
)
78
}
79
80
resource "aws_subnet" "private" {
81
count = length(var.private_subnet_cidrs)
82
vpc_id = aws_vpc.main.id
83
cidr_block = var.private_subnet_cidrs[count.index]
84
availability_zone = var.availability_zones[count.index]
85
86
tags = merge(
87
{
88
Name = "${var.name}-private-${count.index + 1}"
89
Tier = "private"
90
},
91
var.tags
92
)
93
}
94
95
resource "aws_internet_gateway" "main" {
96
count = var.create_internet_gateway ? 1 : 0
97
vpc_id = aws_vpc.main.id
98
99
tags = merge(
100
{
101
Name = "${var.name}-igw"
102
},
103
var.tags
104
)
105
}
106
```
107
108
**variables.tf:**
109
110
```hcl
111
variable "name" {
112
description = "Name of the VPC"
113
type = string
114
}
115
116
variable "cidr_block" {
117
description = "CIDR block for VPC"
118
type = string
119
validation {
120
condition = can(regex("^([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}$", var.cidr_block))
121
error_message = "CIDR block must be valid IPv4 CIDR notation."
122
}
123
}
124
125
variable "availability_zones" {
126
description = "List of availability zones"
127
type = list(string)
128
}
129
130
variable "private_subnet_cidrs" {
131
description = "CIDR blocks for private subnets"
132
type = list(string)
133
default = []
134
}
135
136
variable "enable_dns_hostnames" {
137
description = "Enable DNS hostnames in VPC"
138
type = bool
139
default = true
140
}
141
142
variable "tags" {
143
description = "Additional tags"
144
type = map(string)
145
default = {}
146
}
147
```
148
149
**outputs.tf:**
150
151
```hcl
152
output "vpc_id" {
153
description = "ID of the VPC"
154
value = aws_vpc.main.id
155
}
156
157
output "private_subnet_ids" {
158
description = "IDs of private subnets"
159
value = aws_subnet.private[*].id
160
}
161
162
output "vpc_cidr_block" {
163
description = "CIDR block of VPC"
164
value = aws_vpc.main.cidr_block
165
}
166
```
167
168
## Best Practices
169
170
1. **Use semantic versioning** for modules
171
2. **Document all variables** with descriptions
172
3. **Provide examples** in examples/ directory
173
4. **Use validation blocks** for input validation
174
5. **Output important attributes** for module composition
175
6. **Pin provider versions** in versions.tf
176
7. **Use locals** for computed values
177
8. **Implement conditional resources** with count/for_each
178
9. **Test modules** with Terratest
179
10. **Tag all resources** consistently
180
181
**Reference:** See `references/aws-modules.md` and `references/oci-modules.md`
182
183
## Module Composition
184
185
```hcl
186
module "vpc" {
187
source = "../../modules/aws/vpc"
188
189
name = "production"
190
cidr_block = "10.0.0.0/16"
191
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
192
193
private_subnet_cidrs = [
194
"10.0.1.0/24",
195
"10.0.2.0/24",
196
"10.0.3.0/24"
197
]
198
199
tags = {
200
Environment = "production"
201
ManagedBy = "terraform"
202
}
203
}
204
205
module "rds" {
206
source = "../../modules/aws/rds"
207
208
identifier = "production-db"
209
engine = "postgres"
210
engine_version = "15.3"
211
instance_class = "db.t3.large"
212
213
vpc_id = module.vpc.vpc_id
214
subnet_ids = module.vpc.private_subnet_ids
215
216
tags = {
217
Environment = "production"
218
}
219
}
220
```
221
222
223
## Testing
224
225
```go
226
// tests/vpc_test.go
227
package test
228
229
import (
230
"testing"
231
"github.com/gruntwork-io/terratest/modules/terraform"
232
"github.com/stretchr/testify/assert"
233
)
234
235
func TestVPCModule(t *testing.T) {
236
terraformOptions := &terraform.Options{
237
TerraformDir: "../examples/complete",
238
}
239
240
defer terraform.Destroy(t, terraformOptions)
241
terraform.InitAndApply(t, terraformOptions)
242
243
vpcID := terraform.Output(t, terraformOptions, "vpc_id")
244
assert.NotEmpty(t, vpcID)
245
}
246
```
247
248
## Related Skills
249
250
- `multi-cloud-architecture` - For architectural decisions
251
- `cost-optimization` - For cost-effective designs
252

© Seth Hobson · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…