Terraform Terragrunt
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed terraform refresh.
https://cloudaffaire.com/terraform-refresh/
In this blog post, we will discuss terraform terragrunt.
What is terraform terragrunt?
Terragrunt is a thin wrapper for Terraform that provides extra tools for keeping your Terraform configurations DRY, working with multiple Terraform modules, and managing remote state. Terragrunt supports all terraform commands with additional features. To get more details on terragrunt, please refer below terragrunt documentation.
https://github.com/gruntwork-io/terragrunt
Next, we are going to explain terraform terragrunt with a demo.
Directory structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
## Directory structure . ├── module │ ├── subnet │ │ ├── main.tf │ │ ├── outputs.tf │ │ └── variables.tf │ └── vpc │ ├── main.tf │ ├── outputs.tf │ └── variables.tf ├── project │ ├── dev │ │ ├── subnet │ │ │ └── terraform.tfvars │ │ └── vpc │ │ └── terraform.tfvars │ ├── prod │ │ ├── subnet │ │ │ └── terraform.tfvars │ │ └── vpc │ │ └── terraform.tfvars │ └── test │ ├── subnet │ │ └── terraform.tfvars │ └── vpc │ └── terraform.tfvars └── terraform_backend ├── main.tf └── terraform.tfstate |
Terraform terragrunt:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
##--------------------------- ## Terraform: Terragrunt ## ##--------------------------- ###################### ##install terragrunt## ###################### cd wget https://github.com/gruntwork-io/terragrunt/releases/download/v0.18.5/terragrunt_linux_386 mv terragrunt_linux_386 /usr/local/bin/terragrunt chmod +x /usr/local/bin/terragrunt terragrunt ########################################################### ##Configure terraform backend for remote state management## ########################################################### ## Create a directory and get inside it mkdir -p terraform/terraform_backend && cd terraform/terraform_backend ## Create backend resources vi main.tf ------------------ #creates a S3 bucket and a dynamoDB table provider "aws" { region = "ap-south-1" shared_credentials_file = "/home/ec2-user/.aws/credentials" profile = "default" } resource "aws_s3_bucket" "mys3bucket" { bucket = "my-s3-terraform-backend-cloudaffaire" #needs to be unique across AWS force_destroy = true tags { Name = "S3 bucket for terraform backend state storage" } } resource "aws_dynamodb_table" "mydynamodbtable" { name = "my-dynamodb-terraform-backend-cloudaffaire" hash_key = "LockID" read_capacity = 5 write_capacity = 5 attribute { name = "LockID" type = "S" } tags { Name = "dynamoDB table for terraform backend state locking" } } ------------------ :wq ## Format code terraform fmt ## Initialize terraform terraform init ## Create the resource terraform apply ## Move to your home directory cd ################## ##create modules## ################## ##module: vpc## ## Create directory for module mkdir -p terraform/module/vpc && mkdir -p terraform/module/subnet ## Vpc module resource vi terraform/module/vpc/main.tf --------------------------- locals { tag = "${var.name}_${var.env}_${var.version}_${var.tag}" } provider "aws" { region = "${var.aws_region}" shared_credentials_file = "${var.shared_credentials_file}" profile = "${var.profile}" } terraform { backend "s3" {} } resource "aws_vpc" "myvpc" { cidr_block = "${var.vpc_cidr}" tags { Name = "${local.tag}" } } --------------------------- :wq ## Vpc module variables vi terraform/module/vpc/variables.tf --------------------------- variable "aws_region" { description = "AWS region where the VPC will be created" default = "ap-south-1" } variable "shared_credentials_file" { description = "credential file to be used to connect to AWS" default = "/home/ec2-user/.aws/credentials" } variable "profile" { description = "profile name of the credentials" default = "default" } variable "vpc_cidr" { description = "CIDR block of VPC" default = "192.168.0.0/16" } variable "name" { description = "name to be used on all the resources as identifier" default = "" } variable "env" { description = "application environment" default = "" } variable "tag" { description = "tag for vpc" default = "" } variable "version" { description = "application version" default = "" } --------------------------- :wq ## Vpc module output vi terraform/module/vpc/outputs.tf --------------------------- output "vpc_id" { description = "The ID of the VPC" value = "${aws_vpc.myvpc.id}" } --------------------------- :wq ##module: subnet## ## Subnet module resource vi terraform/module/subnet/main.tf --------------------------- locals { tag = "${var.name}_${var.env}_${var.version}_${var.tag}" } provider "aws" { region = "${var.aws_region}" shared_credentials_file = "${var.shared_credentials_file}" profile = "${var.profile}" } terraform { backend "s3" {} } data "terraform_remote_state" "vpc" { backend = "s3" config { bucket = "my-s3-terraform-backend-cloudaffaire" key = "${var.name}/${var.env}/${var.version}/vpc/terraform.tfstate" region = "ap-south-1" dynamodb_table = "my-dynamodb-terraform-backend-cloudaffaire" } } resource "aws_subnet" "myvpc_subnet" { vpc_id = "${data.terraform_remote_state.vpc.vpc_id}" cidr_block = "${var.subnet_cidr}" availability_zone = "${var.subnet_availability_zone}" tags { Name = "${local.tag}" } } --------------------------- :wq ## Subnet module variables vi terraform/module/subnet/variables.tf --------------------------- variable "aws_region" { description = "AWS region where the VPC will be created" default = "ap-south-1" } variable "shared_credentials_file" { description = "credential file to be used to connect to AWS" default = "/home/ec2-user/.aws/credentials" } variable "profile" { description = "profile name of the credentials" default = "default" } variable "vpc_id" { description = "VPC ID" default = "" } variable "subnet_cidr" { description = "CIDR block for the subnet" default = "" } variable "subnet_availability_zone" { description = "Availability zone of the subnet" default = "" } variable "name" { description = "name to be used on all the resources as identifier" default = "" } variable "env" { description = "application environment" default = "" } variable "tag" { description = "tag for subnet" default = "" } variable "version" { description = "application version" default = "" } --------------------------- :wq ## Subnet module output vi terraform/module/subnet/outputs.tf --------------------------- output "subnet_id" { description = "Subnet ID" value = "${aws_subnet.myvpc_subnet.id}" } --------------------------- :wq ## Format code terraform fmt ################################### ##github configuration for module## ################################### ## login to github and create a repository named terragrunt ## Copy the repository ssh link ## git@github.com:CloudAffaire/terragrunt.git ## Configure github for ssh ssh-keygen -t rsa -C "cloudaffaire@gmail.com" ## Upload module vpc and subnet to github terragrunt repository cd terraform/module && git init git config --local user.name "debjeet" && \ git config --local user.email "debjeet@cloudaffaire.com" git add . git commit -m "initial commit" git remote add origin git@github.com:CloudAffaire/terragrunt.git git push -u origin master git tag -a v1.0 -m "myapp version 1.0" git push origin v1.0 cd #git push --delete origin v1.0 #git tag --delete v1.0 ################### ##create projects## ################### ##project:prod## ## Create project directory for production mkdir -p terraform/project/prod/vpc/ && mkdir -p terraform/project/prod/subnet/ ## Create terraform.tfvars for prod environment of vpc module vi terraform/project/prod/vpc/terraform.tfvars -------------------------------------------- terragrunt = { remote_state { backend = "s3" config { bucket = "my-s3-terraform-backend-cloudaffaire" key = "myapp/prod/v1/vpc/terraform.tfstate" region = "ap-south-1" dynamodb_table = "my-dynamodb-terraform-backend-cloudaffaire" } } terraform { source = "git::git@github.com:CloudAffaire/terragrunt.git//vpc?ref=v1.0" } } vpc_cidr = "10.0.0.0/16" aws_region = "ap-south-1" name = "myapp" env = "prod" tag = "vpc" version = "v1" -------------------------------------------- :wq ## Create terraform.tfvars for prod environment of subnet module vi terraform/project/prod/subnet/terraform.tfvars -------------------------------------------- terragrunt = { remote_state { backend = "s3" config { bucket = "my-s3-terraform-backend-cloudaffaire" key = "myapp/prod/v1/subnet/terraform.tfstate" region = "ap-south-1" dynamodb_table = "my-dynamodb-terraform-backend-cloudaffaire" } } terraform { source = "git::git@github.com:CloudAffaire/terragrunt.git//subnet?ref=v1.0" } } subnet_cidr = "10.0.0.0/24" aws_region = "ap-south-1" name = "myapp" env = "prod" tag = "subnet" version = "v1" -------------------------------------------- :wq ## Create prod resources using terragrunt cd terraform/project/prod/vpc/ terragrunt plan terragrunt apply cd ../subnet terragrunt plan terragrunt apply cd ##Project:dev## ## Create project directory for development mkdir -p terraform/project/dev/vpc/ && mkdir -p terraform/project/dev/subnet/ ## Create terraform.tfvars for dev environment of vpc module vi terraform/project/dev/vpc/terraform.tfvars -------------------------------------------- terragrunt = { remote_state { backend = "s3" config { bucket = "my-s3-terraform-backend-cloudaffaire" key = "myapp/dev/v1/vpc/terraform.tfstate" region = "ap-south-1" dynamodb_table = "my-dynamodb-terraform-backend-cloudaffaire" } } terraform { source = "git::git@github.com:CloudAffaire/terragrunt.git//vpc?ref=v1.0" } } vpc_cidr = "10.0.0.0/16" aws_region = "ap-south-1" name = "myapp" env = "dev" tag = "vpc" version = "v1" -------------------------------------------- :wq ## Create terraform.tfvars for dev environment of subnet module vi terraform/project/dev/subnet/terraform.tfvars -------------------------------------------- terragrunt = { remote_state { backend = "s3" config { bucket = "my-s3-terraform-backend-cloudaffaire" key = "myapp/dev/v1/subnet/terraform.tfstate" region = "ap-south-1" dynamodb_table = "my-dynamodb-terraform-backend-cloudaffaire" } } terraform { source = "git::git@github.com:CloudAffaire/terragrunt.git//subnet?ref=v1.0" } } subnet_cidr = "10.0.0.0/24" aws_region = "ap-south-1" name = "myapp" env = "dev" tag = "subnet" version = "v1" -------------------------------------------- :wq ## Create dev resources using terragrunt cd terraform/project/dev/vpc/ terragrunt plan terragrunt apply cd ../subnet terragrunt plan terragrunt apply cd ##Project:test## ## Create project directory for testing mkdir -p terraform/project/test/vpc/ && mkdir -p terraform/project/test/subnet/ ## Create terraform.tfvars for test environment of vpc module vi terraform/project/test/vpc/terraform.tfvars -------------------------------------------- terragrunt = { remote_state { backend = "s3" config { bucket = "my-s3-terraform-backend-cloudaffaire" key = "myapp/test/v1/vpc/terraform.tfstate" region = "ap-south-1" dynamodb_table = "my-dynamodb-terraform-backend-cloudaffaire" } } terraform { source = "git::git@github.com:CloudAffaire/terragrunt.git//vpc?ref=v1.0" } } vpc_cidr = "10.0.0.0/16" aws_region = "ap-south-1" name = "myapp" env = "test" tag = "vpc" version = "v1" -------------------------------------------- :wq ## Create terraform.tfvars for test environment of subnet module vi terraform/project/test/subnet/terraform.tfvars -------------------------------------------- terragrunt = { remote_state { backend = "s3" config { bucket = "my-s3-terraform-backend-cloudaffaire" key = "myapp/test/v1/subnet/terraform.tfstate" region = "ap-south-1" dynamodb_table = "my-dynamodb-terraform-backend-cloudaffaire" } } terraform { source = "git::git@github.com:CloudAffaire/terragrunt.git//subnet?ref=v1.0" } } subnet_cidr = "10.0.0.0/24" aws_region = "ap-south-1" name = "myapp" env = "test" tag = "subnet" version = "v1" -------------------------------------------- :wq ## Create test resources using terragrunt cd terraform/project/test/vpc/ terragrunt plan terragrunt apply cd ../subnet terragrunt plan terragrunt apply cd ## cleanup ## cd terraform/project/prod/subnet/ terragrunt destroy cd ../vpc terragrunt destroy cd ../../dev/subnet/ terragrunt destroy cd ../vpc terragrunt destroy cd ../../test/subnet/ terragrunt destroy cd ../vpc terragrunt destroy cd ../../../terraform_backend/ terraform destroy cd && rm -rf terraform #login to github and delete terragrunt repository |
Hope you have enjoyed this article. With this, we are concluding our introductory series on terraform.
To get more details on terragrunt, please refer below terragrunt documentation.
https://github.com/gruntwork-io/terragrunt
To get more details on terraform, please refer below terraform documentation.
https://www.terraform.io/docs/index.html