Terraform和VPC对等

时间:2017-01-24 00:47:31

标签: terraform

所以我们在我们的组织中广泛使用terraform,并且我有一些关于其他人如何进行VPC对等的问题。最初创建连接很容易。我们从刚刚创建的VPC中引入并引用另一个VPC,然后填充路由表等。问题出在我们刚刚看到的VPC上。我们现在必须手动转到其他网络堆栈并手动添加CIDR / PCX ID作为变量。我编写了一个脚本,可以更轻松地为我们处理这个问题,但我想问一下是否有人动态执行针对任何现有VPC的AWS查找,并自动将现有PCX添加到该VPC的路由表中。

这将是有价值的一个例子是在OPS VPC中。我们有OPS,然后是dev,prod,qa,stg,uat,cte等。所以当我们创建CTE vpc时,它会自动创建一个pcx并将其链接到ops并路由到ops。然而,ops不知道这个新的pcx。所以我们必须手动添加它。我希望ops能够自己进行资源查找,并为它找到的任何新的VPC / PCX提供自己的资源。

TLDR;双向VPC对等的方式更加动态

3 个答案:

答案 0 :(得分:2)

假设您正在使用remote state backend,您可以将OPS网络堆栈作为remote state data source引入,然后从您希望它能够的任何短暂堆栈中更改其路由表路由到。

将尝试并做一个最小的例子(显然缺少大量的锅炉板):

# my_ops_stack.tf

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

module "ops_stack" {
  source = "/my/modules/ops_stack"
  cidr   = "10.1.0.0/16"
  // other vars probably
}

// the outputs which will be accessible 
// via the remote state data source:
output "routing_table_id" { 
  value = "${module.ops_stack.routing_table_id}"
}
output "vpc_id" {
  value = "${module.ops_stack.vpc_id}"
}
output "vpc_cidr" {
  value = "10.1.0.0/16"
}

我现在configure使用terraform cli(this will soon be possible in config)进行此堆栈的远程状态后端:

# Run in the same folder as my_ops_stack.tf
terraform remote config \
  -backend=s3 \
  -backend-config="bucket=my-state-bucket" \
  -backend-config="key=ops-stack/terraform.tfstate" \
  -backend-config="region=eu-west-1"

现在配置了状态后端,您应用于堆栈的任何更改都将与该后端同步:

terraform apply
# the usual stuff... but now synced with s3!

现在,在你的新短暂堆栈的模板中(dev,prod,qa,stg,uat,cte等):

# my_dev_stack.tf

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

// Pull in your ops stack from the remote backend:
data "terraform_remote_state" "ops_stack" {
    backend = "s3"
    config {
        bucket = "my-state-bucket"
        key = "ops-stack/terraform.tfstate"
        region = "eu-west-1"
    }
}

// Create your dev stack
module "dev_stack" {
  source            = "/my/modules/dev_stack"
  cidr              = "10.2.0.0/16"
  // The ops_stack vpc id for creating the peering connection:
  ops_vpc_id        = "${data.terraform_remote_state.ops_stack.vpc_id}"
  // Maybe some security group rules you wanna setup
  allow_access_from = "${data.terraform_remote_state.ops_stack.vpc_cidr}"
  // other vars probably
}

// And use its outputs to add a route to the 
// ops vpc routing table from the dev stack!
resource "aws_route" "ops_to_dev" {
    route_table_id = "${data.terraform_remote_state.ops_stack.routing_table_id}" 
    destination_cidr_block = "10.2.0.0/16" // dev_stack's cidr
    vpc_peering_connection_id = "${module.dev_stack.vpcx_id}"
}

一旦你完成了短暂的堆栈,你就可以安全地销毁它,它甚至会在ops堆栈中清理它的路径。

希望这就是你所追求的!

答案 1 :(得分:0)

我们最后只围绕此编写了一个包装脚本。每当我们添加一个新的VPC时,我们都会进入ops VPC目录并执行这个脚本,它将使用所有必要的变量动态填充variables.tf文件,以设置OPS vpc对等连接/路由。

示例脚本:

#!/bin/bash
region=$(find . -name "*vars.tf"|cut -d/ -f2|cut -d- -f1-3)
profile=$(find . -name "*vars.tf" -exec grep 'variable "profile"' {} \; |awk '{print $6}'|tr -d '"')
account=$(pwd|cut -d/ -f5|cut -d- -f1)

getData(){
    for id in ${ids[@]}; do
        output=$(aws ec2 describe-vpc-peering-connections --region $region --profile $account --vpc-peering-connection-ids $id)
        cidr=$(echo "$output"|jq '.VpcPeeringConnections[].RequesterVpcInfo.CidrBlock'|tr -d '"')
        if [[ $1 == cidr ]]; then
            echo $cidr
        elif [[ $1 == id ]]; then
            echo $id
        fi
    done
}
checkOps() {
    pwd|grep 'ops' &>/dev/null
}
populateRoutes() {
    if ! checkOps; then
        echo "Must be run from the ops directory"
        exit 1
    fi
    ids=($(aws ec2 describe-vpc-peering-connections --region $region --profile $account --filters "Name=status-code,Values=active"|jq '.VpcPeeringConnections[].VpcPeeringConnectionId'|tr -d '"'))
    if (( ${#ids[@]} == 0 )); then
        echo "No update necessary"
        exit 0
    fi

    cidr_list=($(getData cidr))
    cidr_format=$(echo "${cidr_list[@]}"|tr ' ' ',')
    echo $cidr_format

    id_list=($(getData id))
    id_format=$(echo "${id_list[@]}"|tr ' ' ',')
    echo $id_format

    if (( ${#cidr_list[@]} != ${#id_list[@]} )); then
        echo "CIDR List and ID List do not match"
        exit 1
    fi

    sed -i "/pcx_count/c\variable\ \"pcx_count\"\ \{\ default \=\ \"${#ids[@]}\" \}" ./variables.tf
    sed -i "/ops_cidrs/c\variable\ \"ops_cidrs\"\ \{\ default\ \=\ \"$cidr_format\"\ \}" ./variables.tf
    sed -i "/pcx_ids/c\variable\ \"pcx_ids\"\ \{\ default\ \=\ \"$id_format\"\ \}" ./variables.tf
}

populateRoutes

答案 2 :(得分:0)

我也有兴趣执行此自动化部分,其中当我们提供一些条件时,terraform会自动接受对等请求(主要关注帐户间VPC对等)。 我相信为待处理的vpc对等请求创建查找资源将相当容易。使用aws cli,可以通过类似类似aws ec2 describe-vpc-peering-connections的过滤器之类的--filter Name=status-code,Values=pending-acceptance来实现,但是不幸的是,我看不到terraform提供了这种资源。 我最初的想法是对未决的对等请求执行查找,并接受具有特定条件的请求:requester_account_id,requester_vpc_id,accepter_vpc_id。 到目前为止,唯一的方法是要么手动接受对等请求,要么手动向aws_vpc_peering_connection_accepter

提供对等请求ID