# Terraform Code

We will start with writing terraform code in our local machine.

Before we begin I have uploaded the codes on the [Github Repository](https://github.com/rishawsingh/terraform-automate.git).

Following are the list of things we are going to add in our Terraform Code:

* First configure provider (i.e we have used AWS)
* Create resources for simplicity we have created EC2 instances, VPC, security groups and S3 bucket.
* Once the files are written they need to be validated using plan and apply command.

**Step 1**

**Create Provider.tf file.**

![](/files/pCLgVjSfRjR9yQhL6T6w)

````
```terraform
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

provider "aws" {
  access_key = "${var.aws_access_key}"
  secret_key = "${var.aws_secret_key}"
  region     = "us-east-1"
}

variable "aws_access_key" {}
variable "aws_secret_key" {}
```
````

**Step 2:**

**Create main.tf file**

![](/files/8SoVcVvym8N0IQc9w2d0)

````
```terraform
resource "aws_vpc" "my_vpc" {
  cidr_block = "10.0.0.0/16"
}



# Create the security group
resource "aws_security_group" "my_sg" {
  name_prefix = "my-sg"
  vpc_id      = aws_vpc.my_vpc.id

  # Allow incoming SSH traffic from anywhere
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

# Create the EC2 instance
resource "aws_instance" "my_ec2" {
  ami           = "ami-00c39f71452c08778" 
  # Amazon Linux 2 AMI
  instance_type = "t2.micro"
}

# Create the S3 bucket
resource "aws_s3_bucket" "my_s3_bucket" {
  bucket = "rishaw-bucket-shaw"
}


output "ec2_public_ip" {
  value = aws_instance.my_ec2.public_ip
}
```
````

Once the files are created, you can initialize the working directory containing Terraform configuration files using the command:

```
terraform init
```

To see what changes will be made in the infrastructure, use the command:

```
terraform plan -var aws_access_key="YOUR ACCESS KEY" -var aws_secret_key="YOUR_SECREt_KEY"
```

After that we can simply make infrastructure changes using the command:

```
terraform apply -auto-approve aws_access_key="YOUR ACCESS KEY" -var aws_secret_key="YOUR_SECREt_KEY"
```

![](/files/1BO95KYs0QG1g5uYQh18)

Once the changes are made and you do not want the infrastructure up any longer you can destroy it using the command:

```
terraform destroy -auto-approve aws_access_key="YOUR ACCESS KEY" -var aws_secret_key="YOUR_SECREt_KEY"
```

&#x20;![](/files/AO60cJvgacQT8HfA6mLW)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rishaws-projects.gitbook.io/devops-project/initial/terraform-code.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
