> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Resource Attributes and Dep

> This lesson explores how Terraform manages resource attributes and dependencies for dynamic infrastructure provisioning.

In this lesson, we explore how Terraform manages resource attributes and dependencies. When you provision a resource, Terraform stores various details (attributes) related to that resource. These attributes can then be referenced throughout your configuration, enabling you to create dynamic and interconnected infrastructures.

## Understanding Exported Attributes

Earlier, we created an AWS key pair resource that required a user-supplied public key. After its creation, Terraform exported several attributes, which you can inspect using the Terraform show command.

Below is the configuration used to create the AWS key pair resource:

```hcl theme={null}
resource "aws_key_pair" "alpha" {
  key_name   = "alpha"
  public_key = "ssh-rsa AAAAB3NzaC1yc2EAAADAQABAAABAAQD3......alpha@a-server"
}
```

Run the following command to display the resource details:

```bash theme={null}
$ terraform show
```

The command produces output similar to this:

```plaintext theme={null}
# aws_key_pair.alpha:
resource "aws_key_pair" "alpha" {
  arn          = "arn:aws:ec2:us-east-1::key-pair/alpha"
  fingerprint  = "d7:ff:a6:63:18:64:9c:57:a1:ee:ca:a4:ad:c2:81:62"
  id           = "alpha"
  key_name     = "alpha"
  public_key   = "ssh-rsa AAAAB3NzaC1yc2EAAAA...alpha@a-server"
  tags_all     = {}
}
```

This output reveals exported attributes such as ARN, fingerprint, ID, key name, public key, and tags. For more detailed explanations of these attributes, refer to the [Terraform Documentation](https://www.terraform.io/docs) for each resource.

<Callout icon="lightbulb" color="#1CB2FE">
  Remember: Utilizing exported attributes allows you to build dependencies between resources, enabling dynamic infrastructure provisioning.
</Callout>

## Referencing Resource Attributes

Exported resource attributes are often used as inputs for configuring other resources. Consider a scenario where you need to configure an EC2 instance using the AWS key pair resource. You can reference the key pair's attributes in your EC2 instance configuration as follows:

```hcl theme={null}
resource "aws_key_pair" "alpha" {
  key_name   = "alpha"
  public_key = "ssh-rsa..."
}

resource "aws_instance" "cerberus" {
  ami           = var.ami
  instance_type = var.instance_type
  key_name      = aws_key_pair.alpha.key_name
}
```

In this configuration, the EC2 instance's key\_name parameter is set using the reference expression:

resourceType.ResourceName.attribute

Here, `aws_key_pair.alpha.key_name` refers to the key\_name attribute of the key pair resource named "alpha". By running `terraform apply`, both the key pair and the EC2 instance are provisioned in the correct order. Terraform automatically ensures that the key pair is created before the EC2 instance, thanks to the inherent resource dependency.

The sample console output from running `terraform apply` is shown below:

```bash theme={null}
$ terraform apply
...
aws_key_pair.alpha: Creating...
aws_key_pair.alpha: Creation complete after 1s [id=alpha]
aws_instance.cerberus: Creating...
aws_instance.cerberus: Still creating... [10s elapsed]
aws_instance.cerberus: Creation complete after 10s [id=i-c791dc46a6639d4a7]
Apply complete! Resources: 2 added, 0 changed, 0 destroyed
```

<Callout icon="lightbulb" color="#1CB2FE">
  Terraform automatically manages the creation order by analyzing resource dependencies. During deletion, resources are removed in reverse order, ensuring a safe teardown.
</Callout>

## Managing Explicit Dependencies

Sometimes, two resources might not implicitly depend on each other—for instance, two EC2 instances that do not reference one another. In such cases, you can enforce a creation order using the `depends_on` meta-argument.

Imagine you have two EC2 instances: one for your database server and another for your web server. To ensure that Terraform creates the database instance before the web instance, modify the configuration as follows:

```hcl theme={null}
resource "aws_instance" "db" {
  ami           = var.db_ami
  instance_type = var.db_instance_type
}

resource "aws_instance" "web" {
  ami           = var.web_ami
  instance_type = var.web_instance_type
  depends_on = [
    aws_instance.db
  ]
}
```

With the `depends_on` argument, Terraform provisions the database instance first. When removing resources, it deletes the web instance before the database instance, preserving the dependency order.

<Callout icon="triangle-alert" color="#FF6B6B">
  Be cautious with explicit dependencies. Overusing `depends_on` can lead to unnecessarily complex dependency graphs, which might complicate the execution plan.
</Callout>

## Conclusion

This lesson has reviewed how Terraform manages resource attributes and dependencies. By leveraging both implicit and explicit dependencies, you can efficiently control resource creation and deletion order, ensuring a robust infrastructure lifecycle management.

Happy building with Terraform!

## Additional Resources

* [Terraform Documentation](https://www.terraform.io/docs)
* [AWS Provider Docs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/terraform-associate-certification-hashicorp-certified/module/cca81ade-f05a-42b2-af56-1926cade6582/lesson/c980f12b-3dab-4e34-8d4f-beb7178d78be" />
</CardGroup>
