You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We can change list type to`number`, `bool`, or `any`. It will fail if we try to use a different type of value.
306
+
We can change list type to`number`, `bool`, or `any`. It will fail if we try to use a different type of value.
307
307
308
308
Map:
309
309
@@ -542,7 +542,7 @@ Terraform automatically manages resource dependencies based on the order of reso
542
542
543
543
Like we saw an an example above, the `random_pet` resource depends on the `local_file` resource. So, Terraform will create the `local_file` resource first and then create the `random_pet` resource. And when we destroy the resources, Terraform will destroy the `random_pet` resource first and then destroy the `local_file` resource.
544
544
545
-
1.**Explicit Dependencies**: But there are times where we don't don't use the attributes of other resources and we still want to create the resources in a specific order. Because they might be indirectly dependent on each other. In such cases, we can use the `depends_on` argument to define explicit dependencies between resources. A real life example of this is when we want to create a VPC and then create an EC2 instance in that VPC. We can use the `depends_on` argument to define the dependency between the resources.
545
+
2.**Explicit Dependencies**: But there are times where we don't don't use the attributes of other resources and we still want to create the resources in a specific order. Because they might be indirectly dependent on each other. In such cases, we can use the `depends_on` argument to define explicit dependencies between resources. A real life example of this is when we want to create a VPC and then create an EC2 instance in that VPC. We can use the `depends_on` argument to define the dependency between the resources.
546
546
547
547
For our local file and random pet example, we can define the explicit dependency like this:
548
548
@@ -634,7 +634,7 @@ Mutable infrastructure, servers and resources are updated or modified in place.
634
634
635
635
This approach can lead to configuration drift, security vulnerabilities, and inconsistencies between environments. It can also make it difficult to scale and manage large, complex environments.
636
636
637
-
### Mutable Infrastructure
637
+
### Immutable Infrastructure
638
638
639
639
Immutable infrastructure, servers and resources are treated as disposable and are replaced with new instances when changes are required. Instead of updating existing servers, new servers are created with the desired configuration and the old servers are destroyed. This approach is common in cloud-native environments and is used to ensure consistency, reliability, and scalability.
640
640
@@ -651,9 +651,9 @@ resource "local_file" "pet" {
651
651
}
652
652
```
653
653
654
-
####Lifecycle Rules
654
+
## Lifecycle Rules
655
655
656
-
Without License Rules, Terraform will destroy the old resource and create a new one. But with Lifecycle Rules, we can control the behavior of the resources. We can use `create_before_destroy` to create the new resource before destroying the old one. This can be useful when we want to avoid downtime or data loss.
656
+
Without Lifecycle Rules, Terraform will destroy the old resource and create a new one. But with Lifecycle Rules, we can control the behavior of the resources. We can use `create_before_destroy` to create the new resource before destroying the old one. This can be useful when we want to avoid downtime or data loss.
657
657
658
658
```hcl
659
659
# local.tf
@@ -740,6 +740,7 @@ The data read from data source is available under data object. We can use the da
740
740
741
741
### Resource vs Data Source
742
742
743
+
In simple words resources are used to create and manage infrastructure resources like EC2 instances, S3 buckets, and databases. Data sources are used to fetch information from external sources like APIs, databases, and other resources.
743
744

744
745
745
746
## Meta-Arguments
@@ -761,7 +762,6 @@ resource "local_file" "pet" {
761
762
filename = var.filenames[count.index]
762
763
}
763
764
764
-
765
765
variable "filenames" {
766
766
default = [
767
767
"/root/pets.txt",
@@ -771,9 +771,9 @@ variable "filenames" {
771
771
}
772
772
```
773
773
774
-
This will create three local files with filenames `pets-0.txt`, `pets-1.txt`, and `pets-2.txt`.
774
+
This will create three local files with filenames `pets.txt`, `dogs.txt`, and `cats.txt`. We can use `count.index` to reference the index of the resource. The index starts from 0.
775
775
776
-
### For_each
776
+
### For Each
777
777
778
778
The `for_each` meta-argument allows you to create multiple instances of a resource or data source based on a map or set of strings. It takes a map or set of strings and creates an instance of the resource or data source for each key or value in the map or set.
779
779
@@ -936,16 +936,23 @@ Data is store in form of buckets. Everything under a bucket is an object. We can
Once the bucket is created we can access it via unique URL. We can also use the bucket to host static websites. it's in format of `http://<bucket-name>.<region>.amazonaws.com`. For eg. `http://my-bucket.s3.ap-south-1.amazonaws.com`.
939
+
Once the bucket is created we can access it via unique URL. We can also use the bucket to host static websites. it's in format of `http://<bucket-name>.<region>.amazonaws.com`. For eg. `http://my-bucket.ap-south-1.amazonaws.com`.
940
940
941
-
We can access the files in the bucket using the URL `http://<bucket-name>.<region>.amazonaws.com/<object-key>`. For eg. `http://my-bucket.s3.ap-south-1.amazonaws.com/index.html`.
941
+
We can access the files in the bucket using the URL `http://<bucket-name>.<region>.amazonaws.com/<object-key>`. For eg. `http://my-bucket.ap-south-1.amazonaws.com/image.jpg`.
Any object stored in the bucket has the object data and the Metadata. The metadata contains information about the object like owner, size, last modified date, etc, in key-value pairs.
Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. DynamoDB is highly scalable, single-digit millisecond latency, and fully managed.
952
+
953
+
Even tho it is NoSQL database, it has a table structure. Each table has a primary key that uniquely identifies each item in the table. We can use the AWS CLI to create, update, and delete DynamoDB tables. We can also use the AWS Management Console to manage DynamoDB tables.
We use heredoc syntax to define the policy (`<<EOF`). We can use `terraform plan` to see the changes and `terraform apply` to apply the changes.
1044
+
What we did is first we created an IAM user, then we created an IAM policy with full access to all the resources, and then we attached the policy to the IAM user. We use heredoc syntax to define the policy (`<<EOF`). It's not mandatory to use `EOF`, we can use any string. We can use `terraform plan` to see the changes and `terraform apply` to apply the changes.
Here, `aws_s3_bucket` resource is used to create an S3 bucket, `aws_s3_bucket_object` resource is used to upload a file to the bucket, and `aws_s3_bucket_policy` resource is used to create a bucket policy to allow access to the bucket. Additionally, we are using the `data` block to fetch information about an IAM group.
1148
+
Here, `aws_s3_bucket` resource is used to create an S3 bucket, `aws_s3_bucket_object` resource is used to upload a file to the bucket, and `aws_s3_bucket_policy` resource is used to create a bucket policy to allow access to the bucket. Additionally, we are using the `data` block to fetch information about an IAM group.
1149
+
1150
+
:::Note
1151
+
The bucket naming should not contain uppercase letters, underscores, or special characters. `ss_aa` is not allowed due to DNS compatibility.
1152
+
:::
1153
+
1154
+
### DynamoDB
1155
+
1156
+
Here we are creating a DynamoDB table with a primary key and a sort key.
1157
+
1158
+
```hcl
1159
+
# main.tf
1160
+
resource "aws_dynamodb_table" "cars" {
1161
+
name = "cars"
1162
+
hash_key = "VIN"
1163
+
billing_mode = "PAY_PER_REQUEST"
1164
+
attribute {
1165
+
name = "VIN"
1166
+
type = "S"
1167
+
}
1168
+
}
1169
+
1170
+
Here `hash_key` is the primary key and `attribute` is the sort key. We can use `billing_mode` to specify the billing mode for the table. We can use `PAY_PER_REQUEST` for on-demand capacity mode and `PROVISIONED` for provisioned capacity mode. In the attribute block, we can specify the name and type of the attribute. Here we are using `S` for string type. We can also use `N` for number type and `B` for binary type.
1171
+
1172
+
To insert data into the table, we can use the `aws_dynamodb_table_item` resource.
0 commit comments