To create a site-to-site VPN the usage of the Boto3 library in Python, you’ll make the most of the boto3.shopper('ec2')
shopper to engage with the AWS EC2 provider. Right hereâs an instance code snippet to create a site-to-site VPN:
import boto3
ec2_client = boto3.shopper('ec2')
# Create VPN Gateway
vpn_gateway_response = ec2_client.create_vpn_gateway(Sort='ipsec.1', TagSpecifications=[{
'ResourceType': 'vpn-gateway',
'Tags': [{'Key': 'Name', 'Value': 'SiteToSiteVPN'}]
}])
vpn_gateway_id = vpn_gateway_response['VpnGateway']['VpnGatewayId']
# Create VPN Connection
vpn_connection_response = ec2_client.create_vpn_connection(
Sort='ipsec.1',
CustomerGatewayId='<CUSTOMER_GATEWAY_ID>',
VpnGatewayId=vpn_gateway_id,
Choices={
'StaticRoutesOnly': True
},
TagSpecifications=[{
'ResourceType': 'vpn-connection',
'Tags': [{'Key': 'Name', 'Value': 'SiteToSiteVPNConnection'}]
}]
)
vpn_connection_id = vpn_connection_response['VpnConnection']['VpnConnectionId']
# Create VPN Connection Course
ec2_client.create_vpn_connection_route(
DestinationCidrBlock='<DESTINATION_CIDR_BLOCK>',
VpnConnectionId=vpn_connection_id
)
Within the above code, you wish to have to interchange <CUSTOMER_GATEWAY_ID>
with the ID of the buyer gateway representing the far off website, and <DESTINATION_CIDR_BLOCK>
with the CIDR block of the far off community you need to connect with.
The code snippet creates a VPN gateway the usage of the create_vpn_gateway
manner, passing the required parameters corresponding to the kind of VPN (Sort
) and tags (TagSpecifications
). It then retrieves the VPN gateway ID from the reaction.
Subsequent, the code creates a VPN connection the usage of the create_vpn_connection
manner, offering the buyer gateway ID, VPN gateway ID, choices (on this case, StaticRoutesOnly
), and tags.
In spite of everything, the code creates a VPN connection path the usage of the create_vpn_connection_route
manner, specifying the vacation spot CIDR block and the VPN connection ID.
You’ll run this code the usage of Python and the Boto3 library to create the site-to-site VPN sources in AWS EC2.