This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Understanding Explicit Dependencies between Resources or modules in Terraform


On this article, I’m going to indicate you the way the specific dependencies have an effect on the conduct and order of various assets and modules created and destroyed based mostly on their configurations. For this, you will want to outline specific dependencies between the assets and modules, utilizing depends_on argument.

Please go to my GitHub Repository for Terraform articles on numerous subjects being up to date on fixed foundation.

Let’s get began!

1. Create infrastructure for Specific dependencies

2. Delete (Destroy) your infrastructure

  • AWS consumer account with admin entry, not a root account.
  • Cloud9 IDE with AWS CLI.

I’ve used an information supply for pulling in an AMI ID as a substitute of a hard-coded worth for creating an EC2 Occasion. I’ve used Terraform documentation for this function.

Terraform documentation for AMI.

data source for pulling in an AMI ID.

sqs queue.



1. Create infrastructure for Specific dependencies

  • Let’s create the next organizational construction as proven beneath.

Image description

  • Specific dependencies between assets are usually not seen to Terraform.

  • Nonetheless, the depends_on argument is accepted by any useful resource or module block and accepts an inventory of assets to create specific dependencies for.

  • Within the following instance, we are going to create an infrastruction with an software working on an ec2 occasion "ec2_1" which depends_on an aws s3 bucket "rev_123456".

  • We may also create a sqs queue "ec2_1_sqs_queue" which depends_on aws s3 bucket "rev_123456" and an ec2 occasion "ec2_1" creation.

This dependency is configured inside the appliance, and thus not seen to Terraform.

You should use depends_on to explicitly declare the dependency.

  • You too can specify a number of assets within the depends_on argument, and Terraform will wait till all of them have been created earlier than creating the goal useful resource.

  • However by including specific dependencies, it should improve the size of time it takes for Terraform to create your infrastructure.

  • Create a fundamental.tf file. This can deploy 1 Linux EC2 situations "ec2_1" with a safety group "ec2_sg", 1 s3 bucket "rev_123456" and a sqs module "ec2_1_sqs_queue".

# PROVIDERS BLOCK
terraform {
  required_providers {
    aws = {
      supply  = "hashicorp/aws"
      model = "~> 4.23"
    }
  }
  required_version = ">= 1.2.0"
}

supplier "aws" {
  area  = var.aws_region
}


useful resource "aws_s3_bucket" "rev_123456" { }


# EC2 BLOCK
knowledge "aws_ami" "linux" {
   most_recent = true
   house owners      = ["amazon"]

  filter {
    identify   = "identify"
    values = ["amzn2-ami-hvm-*-x86_64-gp2"]
  }

  filter {
    identify   = "virtualization-type"
    values = ["hvm"]
  }
}


# SECURITY BLOCK 
useful resource "aws_security_group" "ec2_sg" {
   identify        = "ec2_sg"
   description = "permit inbound HTTP site visitors"

   # HTTP from vpc
   ingress {
      from_port   = 80
      to_port     = 80
      protocol    = "tcp"
      cidr_blocks = ["0.0.0.0/0"]     
   }

  # outbound guidelines
  # web entry to wherever
  egress {
     from_port   = 0
     to_port     = 0
     protocol    = "-1"
     cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
     identify = "ec2_sg"
  }
}


useful resource "aws_instance" "ec2_1" {
  ami                = knowledge.aws_ami.linux.id
  instance_type      = "t2.micro"
  vpc_security_group_ids = [aws_security_group.ec2_sg.id]

  depends_on = [aws_s3_bucket.rev_123456]

  tags = {
    Identify = "ec2_1"
  }
}


module "ec2_1_sqs_queue" {
  supply  = "terraform-aws-modules/sqs/aws"
  model = "= 4.0"

  depends_on = [aws_s3_bucket.rev_123456, aws_instance.ec2_1]
}


# VARIABLES BLOCK
variable "aws_region" {
  description = "AWS area for all assets."
  kind        = string
  default     = "us-east-1"
}



# OUTPUTS BLOCK
output "instance_id_1" {
  worth = aws_instance.ec2_1.id
}

output "s3_bucket_id" {
  worth = aws_s3_bucket.rev_123456.id
}

output "s3_bucket_name" {
  worth = "${aws_s3_bucket.rev_123456.arn}"
}

Enter fullscreen mode

Exit fullscreen mode

  • This configuration features a new module, terraform-aws-modules/sqs/aws.

  • Modules have to be put in earlier than Terraform can use them.

  • Run terraform get to put in the module.

Image description

Image description

Since each the ec2 occasion "ec2_1" and the SQS Queue Module "ec2_1_sqs_queue" are dependent upon the s3 bucket "rev_123456", Terraform waits till the bucket is created to start creating the opposite two assets.



2. Delete (Destroy) your infrastructure

  • Specific dependencies have an effect on the order by which assets are destroyed in addition to created.

  • Run terraform destroy to destroy your infrastructure. Settle for the adjustments by typing sure when prompted.

  • Watch for 4-5 minutes to destroy your assets.

See that the SQS Queue Module "ec2_1_sqs_queue", and the ec2 occasion "ec2_1" are destroyed earlier than the assets they rely upon are, i.e the s3 bucket "rev_123456".

  • The output will probably be just like the next.

Image description

  • We now have efficiently demonstrated how the specific dependencies have an effect on the conduct and order of various assets created and destroyed based mostly on their configurations.

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?