2017-01-24 70 views
0

因此,我們在我們的組織中使用了相當廣泛的terraform,並且我還有一些關於其他人如何進行VPC對等的問題。連接的初始創建非常簡單。我們從我們剛剛創建的VPC拉入,引用另一個VPC,然後填充路由表等。問題出在我們剛纔所看到的VPC上。我們現在必須手動轉到其他網絡堆棧並手動添加CIDR/PCX ID作爲變量。我寫了一個腳本,可以讓我們更輕鬆地處理這個腳本,但我想問問任何人是否正在動態地針對AWS對任何現有VPC執行查找並自動將現有PCX添加到該VPC的路由表中。Terraform和VPC對等

一個很有價值的例子是OPS VPC。我們有OPS,然後是dev,prod,qa,stg,uat,cte等等。所以當我們創建CTE vpc時,它會自動創建一個pcx並將其鏈接到ops和路由到ops。然而ops不知道這個新的pcx。所以我們必須手動添加它。我希望ops能夠對其自己進行資源查找,併爲其找到的任何新VPC/PCX提供自己的資源。

TLDR;雙向VPC對等的一種方式是更具動態性

回答

0

我們最終只是圍繞這個寫了一個包裝腳本。每當我們添加一個新的VPC時,我們會前往操作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 
0

假設你正在使用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,督促,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}" 
} 

一旦你用短暫的棧完成,你可以放心地摧毀它甚至會在操作堆棧中清理它的路線。

希望這是你以後的樣子!