I am new to Terraform, but have come a long way, learning a lot through debugging my own script to the best of ability. But, I am stuck here. The issue:
- I am attempting to create two subnets
- I have created the terraform.tfvars file where the string is located
- I have provided the counts: [0], [1] respectively to the two subnets. When I ran “apply”, I get the following:
Error:
Error: Invalid index
│
│ on main.tf line 60, in resource “aws_subnet” “passigan_subnet”:
│ 60: cidr_block = var.subnet_prefix[0]
│ ├────────────────
│ │ var.subnet_prefix is a string
│
│ This value does not have any indices.
╵
╷
│ Error: Invalid index
│
│ on main.tf line 71, in resource “aws_subnet” “passigan_subnet_2”:
│ 71: cidr_block = var.subnet_prefix[1]
│ ├────────────────
│ │ var.subnet_prefix is a string
│
│ This value does not have any indices.
The below is the entire script. What am I doing wrong or have failed to do:
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.54.0"
}
}
}
provider "aws" {
region = "us-east-1"
access_key = ""
secret_key = ""
}
variable subnet_prefix {
type = string
default = ""
description = "description"
}
resource "aws_vpc" "passigan_vpc" {
cidr_block = "10.0.0.0/16"
}
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.passigan_vpc.id
tags = {
Name = "passigan_vpc"
}
}
resource "aws_route_table" "passigan_route_table" {
vpc_id = aws_vpc.passigan_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
tags = {
Name = "passigan_route_table"
}
}
/*-----------------
resource "aws_subnet" "passigan_subnet" {
vpc_id = aws_vpc.passigan_vpc.id
cidr_block = var.subnet_prefix[0]
availability_zone = "us-east-1e"
tags = {
Name = "passigan_subnet"
}
}
resource "aws_subnet" "passigan_subnet" {
vpc_id = aws_vpc.passigan_vpc.id
cidr_block = var.subnet_prefix[1]
availability_zone = "us-east-1e"
tags = {
Name = "passigan_subnet"
}
}
*/
resource "aws_subnet" "passigan_subnet" {
vpc_id = aws_vpc.passigan_vpc.id
cidr_block = var.subnet_prefix[0]
availability_zone = "us-east-1e"
tags = {
Name = "passigan_subnet"
}
}
resource "aws_route_table_association" "a" {
subnet_id = aws_subnet.passigan_subnet.id
route_table_id = aws_route_table.passigan_route_table.id
}
resource "aws_security_group" "allow_tls_passigan_web_traffic" {
name = "allow_tls_passigan_web_traffic"
description = "Allow TLS inbound traffic"
vpc_id = aws_vpc.passigan_vpc.id
ingress {
description = "TLS from VPC-HTTPS_traffic"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "TLS from VPC-HTTP_traffic"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "TLS from VPC-SSH_Connection"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "allow_tls"
}
}
resource "aws_network_interface" "passigan_network_interface" {
subnet_id = aws_subnet.passigan_subnet.id
private_ips = ["10.0.1.50"]
security_groups = [aws_security_group.allow_tls_passigan_web_traffic.id]
}
resource "aws_eip" "one" {
vpc = true
network_interface = aws_network_interface.passigan_network_interface.id
associate_with_private_ip = "10.0.1.50"
depends_on = [aws_internet_gateway.gw]
}
output "my_server_public_IP" {
value = aws_eip.one.public_ip
}
resource "aws_instance" "web_server_instance_passigan" {
ami = "ami-0aa7d40eeae50c9a9"
instance_type = "t2.micro"
key_name = "pascal_access_key"
network_interface {
device_index = 0
network_interface_id = aws_network_interface.passigan_network_interface.id
}
user_data = <<-EOF
#!/bin/bash
sudo yum -y install httpd
sudo systemctl start httpd
sudo systemctl enable httpd
sudo bash -c "echo Your very first aws webserver > /var/www/html/index.html"
EOF
tags = {
Name = "Pascal's Webserver"
}
}
output "My_web_Server_Instance" {
value = aws_instance.web_server_instance_passigan.private_ip
}
resource "aws_redshift_cluster" "pascal_redshift" {
cluster_identifier = "tf-redshift-cluster"
database_name = "passigandb"
master_username = "ppassigan"
master_password = "Ppass8uTydh"
node_type = "dc2.large"
cluster_type = "single-node"
skip_final_snapshot = "true" ```
----------------------------------------------------
# terraform.tfvars file
subnet_prefix = ["10.0.1.0/24","10.0.2.0/24"]
--------------------------------------------------