[英]AWS VPC Peering Automatically using Lambda and SNS Topic
有没有一种方法可以使 AWS VPC 对等互连自动化?
使用下面的 Lambda Python 代码配置您的 VPC,应该是接受者 VPC,然后为此 lambda 配置 SNS 触发器,而此 select 是源允许的 SNS 的最佳选项。 Rest lambda 将处理您在大部分手动工作中需要的内容。
import boto3
client = boto3.client('ec2')
resource = boto3.resource('ec2')
vpc_peering_connection = client.describe_vpc_peering_connections(Filters=[{
'Name':'status-code',
'Values':['pending-acceptance','failed'] # Refer to BOTO3 documentation for filters of your choice
}])['VpcPeeringConnections']
route_tables = client.describe_route_tables()
def lambda_handler(event,context):
try:
list_of_vpcs_in_route_tables = []
for peering_con_vpc_info in vpc_peering_connection: # Gathering Peering VPC connection information
peering_id = peering_con_vpc_info['VpcPeeringConnectionId']
client.accept_vpc_peering_connection( VpcPeeringConnectionId = peering_id )
# for values in peering_con_vpc_info['RequesterVpcInfo']:
print('VPC Peering ID request accepted \t ' + peering_id) # Print PEERING CONNECTION ID
accpeter_vpc_id = peering_con_vpc_info['AccepterVpcInfo']['VpcId'] # Filtering ACCEPTERS VPID information
#Getting list of VPC found with ROUTE TABLES
for rt_tables_list in route_tables['RouteTables']: # Getting ALL THE ROUTE TABLES IN AN ACCOUNT
if accpeter_vpc_id in rt_tables_list['VpcId']: # Filtering with matching ACCEPTER VPCID with EXISTING ROUTE TABLE LIST OF VPCID's
print("===================================================================================================")
print("For the Accepter VPC\t" + accpeter_vpc_id +"\twith Routable ID\t" + rt_tables_list['RouteTableId'])
print("===================================================================================================")
DestinationBlock = peering_con_vpc_info['RequesterVpcInfo']['CidrBlock']
RoutablesID = rt_tables_list['RouteTableId']
VpcPeeringId = peering_con_vpc_info['VpcPeeringConnectionId']
client.create_route(DestinationCidrBlock = DestinationBlock,
RouteTableId = RoutablesID,
VpcPeeringConnectionId = VpcPeeringId)
print("Routes added successfully")
elif peering_con_vpc_info['Status']['Code'] == 'failed':
print('Peering has failed look for the exception errors')
except Exception as e:
print(e.args[-1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.