Default subnet not found - terraform

When I run terraform apply with the resource:
resource "aws_default_subnet" "my_az_default_subnet" {
availability_zone = "eu-north-1"
}
I get the error:
aws_default_subnet.learntf_default_subnet: Creating...
Error: Default subnet not found
even if the default VPC and subnet exists in the specified availability_zone "eu-north-1".
provider.tf:
provider "aws" {
access_key = var.aws_access_key
secret_key = var.aws_secret_key
region = "eu-north-1"
}
variables.tf:
variable "aws_access_key" {}
variable "aws_secret_key" {}
network.tf:
resource "aws_default_subnet" "learntf_default_subnet" {
availability_zone = "eu-north-1"
}

My mistake. It worked with.
resource "aws_default_subnet" "def_subnet" {
availability_zone = "eu-north-1a"
}

Related

Terraform Error: Cycle: azurerm_subnet_service_endpoint_storage_policy.stg

unable to attach service_endpoint_policy_ids to the subnet
service_endpoints created successfully but storage policy unable to attach to subnet
Ended up with below error
Error: Cycle: azurerm_subnet_service_endpoint_storage_policy.stg, azurerm_subnet.backend, module.storage_bsai.var.vnet_subnet_id (expand), module.storage_bsai.azurerm_storage_account.storageaccount_name, module.storage_bsai.output.id (expand)
provider
azurerm version = "2.65.0"
Terraform resource for storage policy and subnet
resource "azurerm_subnet_service_endpoint_storage_policy" "stg" {
name = "storage-policy-bsai"
resource_group_name = "${var.env}-bsai"
location = var.region
definition {
name = "storage"
#description = "definition1"
service_resources = [
module.resource_group.id,
module.storage_bsai.id
]
}
}
resource "azurerm_subnet" "backend" {
depends_on = [module.vnet]
name = "backend"
virtual_network_name = "${var.env}-${var.region}-bsai"
resource_group_name = "${var.env}-bsai"
address_prefixes = ["10.0.0.0/24"]
service_endpoints = ["Microsoft.Storage", "Microsoft.AzureCosmosDB", "Microsoft.ServiceBus", "Microsoft.Web", "Microsoft.ContainerRegistry"]
service_endpoint_policy_ids = [azurerm_subnet_service_endpoint_storage_policy.stg.id]
delegation {
name = "delegation"
service_delegation {
name = "Microsoft.Web/serverFarms"
actions = ["Microsoft.Network/virtualNetworks/subnets/action"]
}
}
}
service_endpoint_policy_ids should be a list:
service_endpoint_policy_ids = [azurerm_subnet_service_endpoint_storage_policy.stg.id[
Found the issue -
This appears that you have a cyclic dependency in your config? (i.e. where 2 or more resources depend on each other, meaning Terraform cannot reconcile what needs to happen in what order)
https://github.com/terraform-providers/terraform-provider-azurerm/issues/12593#issuecomment-881192611

Terraform - A managed resource has not been declared in the root module

i'm trying create ec2 instance and setup load balancer using terraform but i'm facing follwing error. How to create instance and configure load balacer in a single main.tf file?
Error: Reference to undeclared resource
"aws_lb_target_group" "front-end":27: vpc_id = "${aws_vpc.terrafom-elb.id}"
A managed resource "aws_vpc" "terrafom-elb" has not been declared in the root
module.source`
code:
region = "us-east-1"
access_key = "*********************"
secret_key = "**********************"
}
resource "aws_instance" "terraform" {
ami = "ami-07ebfd5b3428b6f4d"
instance_type = "t2.micro"
security_groups = ["nodejs","default"]
tags = {
Name = "terrafom-elb"
}
}
resource "aws_lb" "front-end"{
name = "front-end-lb"
internal = false
security_groups = ["nodejs"]
}
resource "aws_lb_target_group" "front-end" {
name = "front-end"
port = 8989
protocol = "HTTP"
vpc_id = "${aws_vpc.terrafom-elb.id}"
depends_on = [aws_instance.terraform]
}
There's a typo where you're assigning the vpc_id:
vpc_id = "${aws_vpc.terrafom-elb.id}"
should be:
vpc_id = "${aws_vpc.terraform-elb.id}"
note the missing 'r' in the word 'terraform'
You can add a data structure to the top and pass VPC ID as variable:
data "aws_vpc" "selected" {
id = var.vpc_id
}
And reference it as vpc_id = data.aws_vpc.selected.id

Terraform EB environment not finding subnet with explicit dependency

I have a Terraform configuration with various AWS resources in one file, including a VPC, three private, three public subnets, and an EB environment.
My Terraform version is 0.12.0, and the AWS provider version is ~> 2.12
The VPC looks like this:
resource "aws_vpc" "terraform-vpc" {
cidr_block = "${var.cidr_block}"
assign_generated_ipv6_cidr_block = true
}
My six subnets look like this:
resource "aws_subnet" "private-a" {
vpc_id = "${aws_vpc.terraform-vpc.id}"
availability_zone = "eu-west-2a"
cidr_block = "10.0.1.0/24"
}
resource "aws_subnet" "public-a" {
vpc_id = "${aws_vpc.terraform-vpc.id}"
availability_zone = "eu-west-2a"
cidr_block = "10.0.2.0/24"
}
resource "aws_subnet" "private-b" {
vpc_id = "${aws_vpc.terraform-vpc.id}"
availability_zone = "eu-west-2b"
cidr_block = "10.0.3.0/24"
}
resource "aws_subnet" "public-b" {
vpc_id = "${aws_vpc.terraform-vpc.id}"
availability_zone = "eu-west-2b"
cidr_block = "10.0.4.0/24"
}
resource "aws_subnet" "private-c" {
vpc_id = "${aws_vpc.terraform-vpc.id}"
availability_zone = "eu-west-2c"
cidr_block = "10.0.5.0/24"
}
resource "aws_subnet" "public-c" {
vpc_id = "${aws_vpc.terraform-vpc.id}"
availability_zone = "eu-west-2c"
cidr_block = "10.0.6.0/24"
}
Then, in my aws_elastic_beanstalk_environment resource I have:
...
setting {
namespace = "aws:ec2:vpc"
name = "Subnets"
value = "#{aws_subnet.private-a.id},#{aws_subnet.private-b.id},#{aws_subnet.private-c.id}"
}
setting {
namespace = "aws:ec2:vpc"
name = "ELBSubnets"
value = "#{aws_subnet.public-a.id},#{aws_subnet.public-b.id},#{aws_subnet.public-c.id}"
}
...
depends_on = [
aws_security_group.default, # created earlier in the same file
aws_subnet.public-a,
aws_subnet.public-b,
aws_subnet.public-c,
aws_subnet.private-a,
aws_subnet.private-b,
aws_subnet.private-c
]
The problem is I always get an error that one of the subnets does not exist, for example:
Error: ConfigurationValidationException: Configuration validation exception:
Invalid option value: '["#{aws_subnet.private-c.id}","#{aws_subnet.private-b.id}","#{aws_subnet.private-a.id}"]'
(Namespace: 'aws:ec2:vpc', OptionName: 'Subnets'): The subnet '#{aws_subnet.private-c.id}' does not exist.
It is always related to a different subnet, sometimes one of the private ones used for the Subnets option, sometimes one of the public ones used in the ELBSubnets option.
What baffles is me is that I have explicitly defined them as dependencies, although I think it should work even without the explicit dependency. And in the terraform apply logs all of the subnets are always created before the eb environment:
aws_subnet.private-c: Creation complete after 1s [id=subnet-some-id]
aws_subnet.public-a: Creation complete after 1s [id=subnet-some-id]
aws_subnet.public-c: Creation complete after 1s [id=subnet-some-id]
aws_subnet.public-b: Creation complete after 1s [id=subnet-some-id]
aws_subnet.private-a: Creation complete after 6s [id=subnet-some-id]
aws_subnet.private-b: Creation complete after 6s [id=subnet-some-id]
...
aws_elastic_beanstalk_environment.default: Creating...
Upon checking in AWS, everything the subnets are created as expected and as reported by apply. What could be the reason for this error?
As ydaetskcor mentioned in the interpolation of the subnets and used # instead of $, so #{aws_subnet.public-a.id},#{aws_subnet.public-b.id} should be ${aws_subnet.public-a.id},${aws_subnet.public-b.id}.

Terraform for aws failing

I am using terraform first time in order to create resources on my AWS account . I ran the below template for test but it always fails with the issue:
Error parsing <path>/main.tf: At 1:10: illegal char
provider “aws” {
access_key = “${var.access_key}”
secret_key = “${var.access_secret_key}”
region = “${var.region}”enter code here }
resource "aws_vpc" "${var.vpc_name}" { cidr_block = "10.0.0.0/16" enable_dns_hostnames = true }
Error: terraform init There are some problems with the configuration, described below.
Use this instead and let us know
provider "aws" {
access_key = "${var.access_key}"
secret_key = "${var.access_secret_key}"
region = "${var.region}"
}
resource "aws_vpc" "${var.vpc_name}" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
}

Unknown resource referenced in variable with Terraform

I am a beginner to Terraform.
I am trying to execute following code from Terraform Getting started guide.
provider "aws" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
}
resource "aws_instance" "example" {
ami = "${lookup(var.amis, var.region)}"
instance_type = "t2.micro"
tags {
Name = "newprovisionerstest"
}
provisioner "local-exec" {
command = "echo ${aws_instance.example.public_ip} > ip_address.txt"
}
}
output "ip" {
value = "${aws_eip.ip.public_ip}"
}
When I run
terraform apply
or
terraform refresh
It gives following error:
Error: output 'ip': unknown resource 'aws_eip.ip' referenced in variable aws_eip.ip.public_ip
Why is it so? Is it because "aws_eip" resource is not declared anywhere?
Like you said it yourself, there is no aws_eip resource called ip.
If you use the
aws_instance.example.public_ip
it should work totally fine

Resources