2016-03-06 3 views
9

更新日: これについては、他にも取り組んでいます。 2つのサブネットとSSHの要塞で動作する設定を取得できないようです。 *は2つのプライベートサブネットを作成します *バスルームを作成します *バスティオンを介して設定された各サブネット上でec2インスタンスをスピンします(バスルーム経由で任意のシェルコマンドを実行します) *インターネットを持っています 私はテラフォームを学び、プロトタイプを構築しようとしています:ゲートウェイが は* *は、ルートおよびセキュリティグループがAWS VPCのTerraformで2つのサブネット間をルーティングする方法は?

オリジナルポストに応じて設定されたプライベートサブネット上のホストのNATゲートウェイを備えるように構成しました。私はAWS VPCをTerraform経由で設定しています。 DMZサブネットに加えて、インターネットからのトラフィックを受信するパブリックサブネット「Web」があります。私はインターネットからアクセスできないプライベートサブネット「アプリ」を持っています。私はterraformがプライベート "app"サブネット上にインスタンスをプロビジョニングできるように、要塞ホストを設定しようとしています。私はまだこれを働かせることができませんでした。

私が要塞に向かっているとき、私は要塞ホストからプライベートサブネット内のインスタンスまでSSHすることはできません。私はルーティング問題があると思う。いくつかの例とドキュメントを使ってこのプロトタイプを構築しています。多くの例では、awsプロバイダを使用してわずかに異なるテクニックとテラフォームのルーティング定義が使用されています。

「ウェブ」サブネット上のインスタンスがアクセスできるように、これら3つのサブネット(パブリック 'web'、パブリック 'dmz' /バス停、プライベート 'app')を定義する理想的または適切な方法を提供できますかDMZ内の要塞ホストがプライベート「アプリ」サブネットにインスタンスをプロビジョニングできることを確認します。

私のconfigsのスニップは以下の通りです:

resource "aws_subnet" "dmz" { 
    vpc_id = "${aws_vpc.vpc-poc.id}" 
    cidr_block = "${var.cidr_block_dmz}" 
} 

resource "aws_route_table" "dmz" { 
    vpc_id = "${aws_vpc.vpc-poc.id}" 
    route { 
     cidr_block = "0.0.0.0/0" 
     gateway_id = "${aws_internet_gateway.gateway.id}" 
    } 
} 

resource "aws_route_table_association" "dmz" { 
    subnet_id = "${aws_subnet.dmz.id}" 
    route_table_id = "${aws_route_table.dmz.id}" 
} 

resource "aws_subnet" "web" { 
    vpc_id = "${aws_vpc.vpc-poc.id}" 
    cidr_block = "10.200.2.0/24" 
} 

resource "aws_route_table" "web" { 
    vpc_id = "${aws_vpc.vpc-poc.id}" 
    route { 
     cidr_block = "0.0.0.0/0" 
     instance_id = "${aws_instance.bastion.id}" 
    } 
} 

resource "aws_route_table_association" "web" { 
    subnet_id = "${aws_subnet.web.id}" 
    route_table_id = "${aws_route_table.web.id}" 
} 

resource "aws_subnet" "app" { 
    vpc_id = "${aws_vpc.vpc-poc.id}" 
    cidr_block = "10.200.3.0/24" 
} 

resource "aws_route_table" "app" { 
    vpc_id = "${aws_vpc.vpc-poc.id}" 
    route { 
     cidr_block = "0.0.0.0/0" 
     instance_id = "${aws_instance.bastion.id}" 
    } 
} 

resource "aws_route_table_association" "app" { 
    subnet_id = "${aws_subnet.app.id}" 
    route_table_id = "${aws_route_table.app.id}" 
} 
+1

あなたはこれでより多くの助けが必要な場合は、私はルートはここ(アウトバウンドの欠如以外の問題であるとは考えていないとしても、いずれかのセキュリティグループ/ NACLsなどを表示するために与えられたあなたのTFファイルを展開する必要があります。あなたの砦のボックスがNATゲートウェイとしても機能している場合を除き、プライベートサブネットでのWebアクセス) – ydaetskcoR

答えて

3

ここでは、役立つスニペットを示します。これはテストされていませんが、プライベートサブネットでVMをプロビジョニングする私のterraformファイルの1つから取得されました。私はこれが1つのプライベートサブネットで動作することを知っている、私はあなたの元の質問のようにここで2つを実装しようとしました。

NATインスタンスを突き抜けて、プライベートサブネットボックスをTerraformでプロビジョニングします。セキュリティグループが正しく設定されていれば、正常に動作します。私のためにいくつかの実験が必要でした。

/* VPC creation */ 
resource "aws_vpc" "vpc_poc" { 
    cidr_block = "10.200.0.0/16" 
} 

/* Internet gateway for the public subnets */ 
resource "aws_internet_gateway" "gateway" { 
    vpc_id = "${aws_vpc.vpc_poc.id}" 
} 

/* DMZ subnet - public */ 
resource "aws_subnet" "dmz" { 
    vpc_id = "${aws_vpc.vpc_poc.id}" 
    cidr_block = "10.200.1.0/24" 
    /* may help to be explicit here */ 
    map_public_ip_on_launch = true 
    /* this is recommended in the docs */ 
    depends_on = ["aws_internet_gateway.gateway"] 
} 

resource "aws_route_table" "dmz" { 
    vpc_id = "${aws_vpc.vpc_poc.id}" 
    route { 
     cidr_block = "0.0.0.0/0" 
     gateway_id = "${aws_internet_gateway.gateway.id}" 
    } 
} 

resource "aws_route_table_association" "dmz" { 
    subnet_id = "${aws_subnet.dmz.id}" 
    route_table_id = "${aws_route_table.dmz.id}" 
} 

/* Web subnet - public */ 
resource "aws_subnet" "web" { 
    vpc_id = "${aws_vpc.vpc_poc.id}" 
    cidr_block = "10.200.2.0/24" 
    map_public_ip_on_launch = true 
    depends_on = ["aws_internet_gateway.gateway"] 
} 

resource "aws_route_table" "web" { 
    vpc_id = "${aws_vpc.vpc_poc.id}" 
    route { 
     cidr_block = "0.0.0.0/0" 
     /* your public web subnet needs access to the gateway */ 
     /* this was set to bastion before so you had a circular arg */ 
     gateway_id = "${aws_internet_gateway.gateway.id}" 
    } 
} 

resource "aws_route_table_association" "web" { 
    subnet_id = "${aws_subnet.web.id}" 
    route_table_id = "${aws_route_table.web.id}" 
} 

/* App subnet - private */ 
resource "aws_subnet" "app" { 
    vpc_id = "${aws_vpc.vpc_poc.id}" 
    cidr_block = "10.200.3.0/24" 
} 

/* Create route for DMZ Bastion */ 
resource "aws_route_table" "app" { 
    vpc_id = "${aws_vpc.vpc_poc.id}" 
    route { 
     cidr_block = "0.0.0.0/0" 
     /* this send traffic to the bastion to pass off */ 
     instance_id = "${aws_instance.nat_dmz.id}" 
    } 
} 

/* Create route for App Bastion */ 
resource "aws_route_table" "app" { 
    vpc_id = "${aws_vpc.vpc_poc.id}" 
    route { 
     cidr_block = "0.0.0.0/0" 
     /* this send traffic to the bastion to pass off */ 
     instance_id = "${aws_instance.nat_web.id}" 
    } 
} 

resource "aws_route_table_association" "app" { 
    subnet_id = "${aws_subnet.app.id}" 
    route_table_id = "${aws_route_table.app.id}" 
} 

/* Default security group */ 
resource "aws_security_group" "default" { 
    name = "default-sg" 
    description = "Default security group that allows inbound and outbound traffic from all instances in the VPC" 
    vpc_id = "${aws_vpc.vpc_poc.id}" 

    ingress { 
    from_port = "0" 
    to_port  = "0" 
    protocol = "-1" 
    self  = true 
    } 

    egress { 
    from_port = "0" 
    to_port  = "0" 
    protocol = "-1" 
    self  = true 
    } 
} 

/* Security group for the nat server */ 
resource "aws_security_group" "nat" { 
    name  = "nat-sg" 
    description = "Security group for nat instances that allows SSH and VPN traffic from internet. Also allows outbound HTTP[S]" 
    vpc_id  = "${aws_vpc.vpc_poc.id}" 

    ingress { 
    from_port = 80 
    to_port  = 80 
    protocol = "tcp" 
    /* this your private subnet cidr */ 
    cidr_blocks = ["10.200.3.0/24"] 
    } 
    ingress { 
    from_port = 443 
    to_port  = 443 
    protocol = "tcp" 
    /* this is your private subnet cidr */ 
    cidr_blocks = ["10.200.3.0/24"] 
    } 
    ingress { 
    from_port = 22 
    to_port  = 22 
    protocol = "tcp" 
    cidr_blocks = ["0.0.0.0/0"] 
    } 
    ingress { 
    from_port = -1 
    to_port  = -1 
    protocol = "icmp" 
    cidr_blocks = ["0.0.0.0/0"] 
    } 

    egress { 
    from_port = 80 
    to_port  = 80 
    protocol = "tcp" 
    cidr_blocks = ["0.0.0.0/0"] 
    } 
    egress { 
    from_port = 443 
    to_port  = 443 
    protocol = "tcp" 
    cidr_blocks = ["0.0.0.0/0"] 
    } 
    egress { 
    from_port = 22 
    to_port  = 22 
    protocol = "tcp" 
    /* this is the vpc cidr block */ 
    cidr_blocks = ["10.200.0.0/16"] 
    } 
    egress { 
    from_port = -1 
    to_port  = -1 
    protocol = "icmp" 
    cidr_blocks = ["0.0.0.0/0"] 
    } 
} 

/* Security group for the web */ 
resource "aws_security_group" "web" { 
    name = "web-sg" 
    description = "Security group for web that allows web traffic from internet" 
    vpc_id = "${aws_vpc.vpc_poc.id}" 

    ingress { 
    from_port = 80 
    to_port = 80 
    protocol = "tcp" 
    cidr_blocks = ["0.0.0.0/0"] 
    } 

    ingress { 
    from_port = 443 
    to_port = 443 
    protocol = "tcp" 
    cidr_blocks = ["0.0.0.0/0"] 
    } 
} 

/* Install deploy key for use with all of our provisioners */ 
resource "aws_key_pair" "deployer" { 
    key_name = "deployer-key" 
    public_key = "${file("~/.ssh/id_rsa")}" 
} 

/* Setup NAT in DMZ subnet */ 
resource "aws_instance" "nat_dmz" { 
    ami    = "ami-67a54423" 
    availability_zone = "us-west-1a" 
    instance_type  = "m1.small" 
    key_name   = "${aws_key_pair.deployer.id}" 
    /* Notice we are assigning the security group here */ 
    security_groups = ["${aws_security_group.nat.id}"] 

    /* this puts the instance in your public subnet, but translate to the private one */ 
    subnet_id   = "${aws_subnet.dmz.id}" 

    /* this is really important for nat instance */ 
    source_dest_check = false 
    associate_public_ip_address = true 
} 

/* Give NAT EIP In DMZ */ 
resource "aws_eip" "nat_dmz" { 
    instance = "${aws_instance.nat_dmz.id}" 
    vpc  = true 
} 

/* Setup NAT in Web subnet */ 
resource "aws_instance" "nat_web" { 
    ami    = "ami-67a54423" 
    availability_zone = "us-west-1a" 
    instance_type  = "m1.small" 
    key_name   = "${aws_key_pair.deployer.id}" 
    /* Notice we are assigning the security group here */ 
    security_groups = ["${aws_security_group.nat.id}"] 

    /* this puts the instance in your public subnet, but translate to the private one */ 
    subnet_id   = "${aws_subnet.web.id}" 

    /* this is really important for nat instance */ 
    source_dest_check = false 
    associate_public_ip_address = true 
} 

/* Give NAT EIP In DMZ */ 
resource "aws_eip" "nat_web" { 
    instance = "${aws_instance.nat_web.id}" 
    vpc  = true 
} 

/* Install server in private subnet and jump host to it with terraform */ 
resource "aws_instance" "private_box" { 
    ami   = "ami-d1315fb1" 
    instance_type = "t2.large" 
    key_name  = "${aws_key_pair.deployer.id}" 
    subnet_id  = "${aws_subnet.api.id}" 
    associate_public_ip_address = false 

    /* this is what gives the box access to talk to the nat */ 
    security_groups = ["${aws_security_group.nat.id}"] 

    connection { 
    /* connect through the nat instance to reach this box */ 
    bastion_host = "${aws_eip.nat_dmz.public_ip}" 
    bastion_user = "ec2-user" 
    bastion_private_key = "${file("keys/terraform_rsa")}" 

    /* connect to box here */ 
    user = "ec2-user" 
    host = "${self.private_ip}" 
    private_key = "${file("~/.ssh/id_rsa")}" 
    } 
} 
3

その要塞ホストもNATとして動作している場合を除き(私は同じインスタンス上の役割を組み合わせることをアドバイスしません)その後、ウェブとアプリのサブネットTFが自動的にVPC用のローカルルーティングレコードを追加するので、アウトバウンドWebアクセスはありませんが、それ以外の場合は細かいルーティングが賢明に見えます。

VPC範囲をカバーするローカルルーティングレコードを持っている限り、ルーティングは問題ありません。あなたのTerraform設定ファイルを取得して、必要最小限のリソースを追加することで、3つのサブネットすべてで基本インスタンスを作成し、それらの間をルーティングすることで、セキュリティグループやNACLなどのものが不足している可能性があります。

+0

ありがとうございました。再び、私が持っている問題は、terraformは、その要塞を経由してプライベートサブネット上のインスタンスにSSHできないということです。私はそれがルーティングされていると考えましたが、私は深く掘り下げてここに更新を投稿します。 – n8gard

2

あなたはあなたの完全なテラフォームを与えられていないが、あなたは要塞IPたり、要塞ホストのCIDRブロックのいずれかからあなたの「アプリ」VPCのインスタンスにSSHを許可する必要がありますので、このような何か:

resource "aws_security_group" "allow_ssh" { 
    name = "allow_ssh" 
    description = "Allow inbound SSH traffic" 

    ingress { 
     from_port = 22 
     to_port = 22 
     protocol = "tcp" 
     cidr_blocks = ["${aws_instance.bastion.private_ip}/32"] 
    } 
} 

そして、あなたの「アプリ」インスタンスのリソースで、あなたはセキュリティグループを追加したいと思う:

... 
vpc_security_group_ids = ["${aws_security_group.allow_ssh.id}"] 
... 

https://www.terraform.io/docs/providers/aws/r/security_group_rule.html

+0

これは私にこれを表現する新しい方法を示してくれたので役に立ちましたが、残念ながらそれは修正ではありませんでした。他に何か欠けているはずです。 – n8gard

+0

特定のヘルプが必要な場合は、残りのテンプレートを用意してください。ここには十分な文脈がありません。 – Nathan

2

あなたはtcpdumpを持つネットワークの問題をチェックする必要がありますおよびその他のデバッグツールが含まれます。それを確認してください :

  1. IPSはreachebleとネットワーク設定されている(例えば、10.200.2正しいです。Xは、要塞ホストのIP)に達することができ
  2. iptablesの/別のファイアウォールは
  3. のsshサーバはあなたが右のセキュリティを持っているそれらのホストからのホストのIPに(SSH)
  4. を聴いているあなたのトラフィックをブロックしないことホストのためのグループ(あなたがEC2インスタンスのprotertiesでこれを見ることができます)
  5. はtcpdumpの
2

でトラフィックを盗聴してみてください私は、要塞ホストの理由が表示されません。

私はsaltstackを使って同様のことをしています。私はVPC内のマスターサーバーを使って残りを制御し、特定のセキュリティグループを割り当ててアクセスを許可します。

CIDR X/24 
subnetX.0/26- subnet for control server. <aster server ip EC2-subnet1/32 
subnetX.64/26 - private minions 
subentX.128/26 - public minions 
subnetX.192/26- private minions 

その後、分離 のあなたの愛のためのサブネットごとに1つのルートテーブルを作成するには、個々のサブネットに各ルートを取り付けます。例えば。

rt-1 - subnetX.0/26 
rt-2 - subnetX.64/26 
rt-3 - subnetX.128/26 
rt-4 - subnetX.192/26 

RT-1インスタンスはセキュリティグループinbound.e.g経由の接続を制限すると皆

destination: CIDR X/24 Target: local 

に接続するためのルートが可能ですので、あなたのルートテーブルはこのように何かを持っていることを確認してください。 EC2-subnet1/32からSSHを許可する

コントロールサーバーですべての作業が完了したら、私のパブリックサブネット内のCIDR X/24 Target:localと呼ばれる特定のルートを削除できます。ローカルのCIDRにトラフィックをルーティングします。

制御サーバーでルートを削除する権限をすでに与えているため、複雑な要塞を作成する理由はありません。

関連する問題