# Day 63/64 - Terraform Variables and Terraform with AWS

Task-01: Creating a Local File with Terraform

Create a file named `create_file.tf` with the following content:

```bash
resource "local_file" "devops" {
  filename = var.filename
  content  = var.content
}
```

Next, create a file named [`variables.tf`](http://variables.tf) with the following content:

```bash
variable "filename" {
  type    = string
  default = "example.txt"
}

variable "content" {
  type    = string
  default = "This is an example file."
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687754189882/991d4586-b1d4-4754-aa5b-5ef5bdd99bf3.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687754242033/ea7d812f-a324-4ab3-bda7-5af01002f2ab.png align="center")

**Task-02: Use terraform to demonstrate usage of List, Set and Object datatypes**

List Data Type:

A list is an ordered collection of elements, where each element can be of any data type. Let's create a list variable named `tags` as an example:

```bash
variable "tags" {
  type    = list(string)
  default = ["tag1", "tag2", "tag3"]
}
```

In the above code, we define the `tags` variable as a list of strings with a default value of `["tag1", "tag2", "tag3"]`. You can access individual elements of the list using indexing, such as `var.tags[0]` to retrieve the first element.

1. A set is an unordered collection of unique elements. It ensures that each element appears only once. Let's create a set variable named `users`:
    

```bash
variable "users" {
  type    = set(string)
  default = ["user1", "user2", "user3"]
}
```

In the above example, we define a variable `users` as a set of strings. It has a default value of `["user1", "user2", "user3"]`. Sets automatically eliminate duplicate elements, ensuring that each element appears only once.  

1. An object is a collection of key-value pairs, where each key is a string and each value can be of any data type. Let's create an object variable named `instance`:
    

```bash
variable "instance" {
  type = object({
    name     = string
    size     = string
    region   = string
    tags     = list(string)
    enabled  = bool
  })
  default = {
    name     = "web-server"
    size     = "t2.micro"
    region   = "us-west-2"
    tags     = ["web", "prod"]
    enabled  = true
  }
}
```

Practical Example:

Now, let's put these data types into practice by creating an EC2 instance using the AWS provider. We'll use the object data type to define the configuration details of the EC2 instance, including the AMI, instance type, subnet IDs, tags, and security groups. Follow the steps below:  
  
1\. Create a file named [`variables.tf`](http://variables.tf) with the following content:

```bash
variable "ec2_instance" {
  type = object({
    ami           = string
    instance_type = string
    subnet_ids    = list(string)
    tags          = map(string)
    security_groups = set(string)
  })
  default = {
    ami           = "ami-053b0d53c279acc90"
    instance_type = "t2.micro"
    subnet_ids    = ["subnet-0da4178ade750f5e2", "subnet-070fd0b5c4195efbd"]
    tags          = {
      Name        = "MyEC2Instance"
      Environment = "Production"
    }
    security_groups = ["sg-045f7a9bc0af55aa1", "sg-09b8101c3ee5cbac8"]
  }
}
```

1. Create a file named [`provider.tf`](http://provider.tf) with the following content:
    

```bash
provider "aws" {
  region = "us-east-1"
}
```

1. Create a file named `create_instance.tf` with the following content:
    

```bash
resource "aws_instance" "ec2_instance" {
  ami           = var.ec2_instance.ami
  instance_type = var.ec2_instance.instance_type
  subnet_id     = var.ec2_instance.subnet_ids[0]
  tags          = var.ec2_instance.tags
  vpc_security_group_ids = var.ec2_instance.security_groups
}
```

Before running `terraform plan` and `terraform apply`, ensure that you have properly configured the AWS CLI with the necessary access and secret keys for EC2.

Now run `terraform plan` and `terraform apply`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687756704576/f988c0c6-c9b9-4de0-a538-c245a8057df9.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687756776931/fcc2d834-6a96-4bbd-a372-c09e8c9ec345.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687756786557/d51d02c0-41b0-41e6-9ded-89ac3dba94cb.png align="center")

That was all for today. If you have queries/suggestions please write in comment. See you another day with another challenge
