Skip to content

Commit bcd2730

Browse files
authored
Merge pull request #24 from Raviadonis/master
Terraform Resources topic updated
2 parents 333417b + 7307327 commit bcd2730

File tree

6 files changed

+130
-3
lines changed

6 files changed

+130
-3
lines changed

‎README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ Terraform Labs brings you tutorials that help you get hands-on experience using
3939

4040
## From Terraform INIT To APPLY
4141

42-
- [Terraform providers](https://github.com/collabnix/terraform/blob/master/beginners/providers/README.md)
43-
- [Terraform resources](https://github.com/collabnix/terraform/blob/master/beginners/resources/README.md)
42+
- [Terraform providers](https://github.com/Raviadonis/terraform-1/blob/master/beginners/providers/Terraform_Providers.md)
43+
- [Terraform resources](https://github.com/Raviadonis/terraform-1/blob/master/beginners/resources/Terraform_Resources.md)
4444
- [Variable Resources](https://github.com/collabnix/terraform/blob/master/beginners/resources/variables/README.md)
4545
- [Output Resources](https://github.com/collabnix/terraform/blob/master/beginners/resources/output/README.md)
4646
- [Terraform CLI](https://github.com/collabnix/terraform/blob/master/beginners/CLI/README.md)

‎Terraform_Resource_Identifier.png

93.5 KB
Loading

‎beginners/resources/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Terraform Resources
2+
## Component or Object
3+
Terraform has two different type of component/object
4+
- `resource`
5+
- `data`
6+
7+
### Resource:
8+
Resource are the daily bread of Terraform. They illustrate the infrastructure pieces that you want to manage such as networks, servers, firewalls, etc. Terraform will use the cloud provider APIs to perform the create, read, update, and delete(CRUD) operations. The `resource` object is constructed of a provider-name_resource-type, local identifier and the block containing the configuration of the resource. This would be better understood with the below diagram.
9+
10+
<p align="center">
11+
<img src="https://github.com/Raviadonis/terraform-1/blob/master/images/Terraform_Resource_definition.png" width="640">
12+
</p>
13+
14+
All the resources are linked to a provider. From the above diagram, **`aws_instance`** indicates that this resource type is provided by the `aws` provider. Next is the local identifier name for the resource, which is specified by you, here we have named as `web` so that we can reference it elsewhere and Terraform keep track of it in the `.tfstate` file. Concept of `state` file will be covered in the upcoming tracks.
15+
16+
See the below example code for the resource creation and how it is referenced to another piece of resource block.
17+
18+
```hcl
19+
resource "aws_instance" "collabnix_node" {
20+
ami = "ami-21f78e11"
21+
availability_zone = var.availability_zone
22+
instance_type = var.instance_type
23+
24+
tags {
25+
Name = "Collabnix_terraform"
26+
}
27+
}
28+
29+
resource "aws_ebs_volume" "collabnix_vol" {
30+
availability_zone = "us-east-1c"
31+
size = 1
32+
33+
tags {
34+
Name = "Collabnix_terraform_vol"
35+
}
36+
}
37+
38+
resource "aws_volume_attachment" "collabnix_vol_attachment" {
39+
device_name = "/dev/sdh"
40+
volume_id = aws_ebs_volume.collabnix_vol.id
41+
instance_id = aws_instance.collabnix_node.id
42+
}
43+
```
44+
45+
From the above, we are creating a single EC2 instance, EBS volume, and attaching that volume to the instance. The first two resource blocks (EC2 instance & EBS volume) will be created independently. While trying to attach the volume to the instance, Terraform requires IDs of instance and volume to be attached. In this case you need to refer the local identifier name and the required attributes of the resources. See the below diagram for better understanding:
46+
47+
<p align="center">
48+
<img src="https://github.com/Raviadonis/terraform-1/blob/master/images/Terraform_Resource_Identifier.png" width="640">
49+
</p>
50+
51+
Now terraform has enough information to take the necessary action. Here the `id` attributes are accessed using the dot-separated notation, like `aws_instance.collabnix_node.id`. Each type of resource will export thier own set of attribute values. Here, for example we used the `id` attribute. Refer this link for more details ([Attribute reference](https://www.terraform.io/docs/providers/aws/r/instance.html#attributes-reference))
52+
53+
### Data sources:
54+
These are very similar to regular [resource](https://github.com/Raviadonis/terraform-1/blob/master/beginners/resources/README.md#resource) object which represents a piece of read-only information that can be fetched from the `provider` (here it is AWS) or from an [external](https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/data_source) data source. This cannot be used for any operations like CREATE, UPDATE, or DELETE. Instead, they can only return several informations (meta-data) like AMI ID, Private IP, Instance ID and so on from an existing resources.
55+
56+
```hcl
57+
data "aws_ami" "app-ami" {
58+
most_recent = true
59+
owners = ["self"]
60+
}
61+
62+
resource "aws_instance" "community" {
63+
ami = data.aws_ami.app-ami.id
64+
instance_type = "t2.micro"
65+
tags = {
66+
Name = "Collabnix"
67+
}
68+
}
69+
```
70+
From the above example code, we are creating an EC2 instance by using the existing AMI ID. Let's assume we have already created an AMI manually or with a different set of tools like `Packer`. Now terraform needs AMI-ID to create an instance and it fetches the ID from the data source `app-ami`.
71+
72+
**Note:**
73+
The combination of resource type and the local identifier name must be unique in your configuration. The below configuration
74+
will through an error like:
75+
`aws_instance.collabnix_node: resource repeated multiple times`
76+
77+
```hcl
78+
resource "aws_instance" "collabnix_node" {
79+
ami = "ami-21f78e11"
80+
instance_type = var.instance_type
81+
}
82+
83+
resource "aws_instance" "collabnix_node" {
84+
ami = "ami-21f78e11"
85+
instance_type = var.instance_type
86+
}
87+
```
88+
89+
## Arguments
90+
91+
This is just a syntax of assining the vaules within the configuration blocks. It looks like the below
92+
```hcl
93+
resource "aws_instance" "collabnix_node" {
94+
ami = "ami-21f78e11" # <IDENTIFIER> = <EXPRESSION>
95+
instance_type = var.instance_type
96+
}
97+
```
98+
Each type of resources will have the list of supported arguments (required and optional) you can consume within your configuration blocks.
99+
100+
Morover, you can also use the special kind of arguments called [meta-arguments](https://www.terraform.io/docs/configuration/resources.html#meta-arguments) within any type of resource. Primarily, these meta-arguments are used to change the behavior of the resource. See the list of meta-arguments below
101+
- `depends_on`
102+
- `count`
103+
- `for_each`
104+
- `provider`
105+
- `lifecycle`
106+
- `provisioner`
107+
- `connection`
108+
109+
```hcl
110+
resource "aws_instance" "collabnix_node" {
111+
depends_on = [aws_s3_bucket.collabnix_bucket]
112+
count = 2
113+
ami = "ami-21f78e11"
114+
instance_type = var.instance_type
115+
}
116+
117+
resource "aws_s3_bucket" "collabnix_bucket" {
118+
bucket = "lab-bucket"
119+
acl = "private"
120+
versioning {
121+
enabled = true
122+
}
123+
tags {
124+
Name = "test-s3-terraform-bucket"
125+
}
126+
}
127+
```
128+
The above configuration blocks are just an example of how the meta-arguments can be used. Please do not go deeper and try to understand how it works in this beginner track.
93.5 KB
Loading
157 KB
Loading

0 commit comments

Comments
 (0)