English 中文(简体)
Retrieve IDs in Terraform before script execution
原标题:

I m a freshman and I don t know if I have a general thinking error but how do I get around the issue that when I want to roll out a script that there are some IDs missing that I would only get with splitting my project. Right now I m stuck at the nat gateway creation which requires the subnet ID. But the subnets are created in the same vpc.tf and I dont know if it is meant to be used in different files.

Here s my script so far. Thanks for your answers

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "5.1.0"
    }
  }
}

provider "aws" {
  region = "eu-central-1"
}

resource "aws_vpc" "main" {
  cidr_block = "172.31.0.0/16"
  tags = {
    Name = "main-vpc"
  }
}

resource "aws_subnet" "presentationtier-a" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "172.31.1.0/27"
  map_public_ip_on_launch = "true"
  availability_zone = "eu-central-1a"
  tags = {
    Name = "presentationtier-subnet-a"
  }
}

resource "aws_subnet" "apptier-a" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "172.31.2.0/27"
  availability_zone = "eu-central-1a"
  tags = {
    Name = "apptier-subnet-a"
  }
}

resource "aws_subnet" "datatier-a" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "172.31.3.0/27"
  availability_zone = "eu-central-1a"
  tags = {
    Name = "datatier-subnet-a"
  }
}

resource "aws_subnet" "datatier-b" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "172.31.4.0/27"
  availability_zone = "eu-central-1b"
  tags = {
    Name = "datatier-subnet-b"
  }
}

/*
Elastic IP
resource "aws_eip" "vpc" {
  domain   = "vpc"
}

// I created it through the AWS console so it wouldn t get removed with  terraform destroy  and to keep the ID for the nat gateway.
*/


resource "aws_nat_gateway" "main" {
  allocation_id = "eipalloc-0..."
  subnet_id = ""
  tags = {
    Name = "gw NAT"
  }

  # To ensure proper ordering, it is recommended to add an explicit dependency
  # on the Internet Gateway for the VPC.
  # depends_on = [aws_internet_gateway.example]

}
问题回答

暂无回答




相关问题
Retrieve IDs in Terraform before script execution

I m a freshman and I don t know if I have a general thinking error but how do I get around the issue that when I want to roll out a script that there are some IDs missing that I would only get with ...

Amazon VPC testing

I sell a product that runs on Amazon EC2. A company now wants to purchase and install it within their perimeter... This also implies the use of a VPN connection to the EC2 datacenter. I want to test ...

Why does vpc comes out from aws-cdk-lib/aws-ec2 ?

Please understand that I m a newbie, just starting to learn the CDK and AWS networks. While configuring a VPC using the CDK, I came across the code below. import * as cdk from aws-cdk-lib ; import * ...

Working with External server inside Amazon VPC

I have an external server hosted and managed on my side. There s another server I use, which is based on Amazon EC2. I would like to get them to work together using Amazon VPC (Virtual Private Cloud). ...

热门标签