Create BGP route policies

This guide describes how to create BGP route policies in Cloud Router.

You can create BGP route policies for Cloud Router using any of the following:

The examples on this page create a BGP route policy to set the BGP multiple exit discriminator (MED) attribute value to 12345 for BGP routes that aren't included in 192.168.10.0/24 and matches the set of communities 65000:1 and 65000:2.

Build a BGP route policy

<pre class="devsite-click-to-copy">
gcloud beta compute routers add-route-policy <var>ROUTER_NAME</var> \
    --policy-name=<var>BGP_ROUTE_POLICY_NAME</var> \
    --policy-type=<var>POLICY_TYPE</var>
</pre>

Replace the following:

*   `ROUTER_NAME`: the name of the Cloud Router
*   `BGP_ROUTE_POLICY_NAME`: a name for the BGP route policy
*   `POLICY_TYPE`: the type of policy to add, either
    `import` for inbound routes, or `export` for outbound routes.
  1. Add a BGP route policy term by running the following command:

    gcloud beta compute routers add-route-policy-term ROUTER_NAME \
        --policy-name=BGP_ROUTE_POLICY_NAME \
        --priority=2 \
        --match='destination != 192.168.10.0/24 && communities.matchesEvery(['65000:1', '65000:2])' \
        --actions='med.set(12345)'
    

Upload a BGP route policy

  1. Create the BGP route policy in your preferred text editor. You can use JSON or YAML formatting to create your BGP route policy. The following example uses a YAML file:

    # Set the MED value for BGP routes that aren't 192.168.10.0/24 and
    # communities that include (65000:1, 65000:2)
    name: BGP_ROUTE_POLICY_NAME
    type: ROUTE_POLICY_TYPE 
    terms:
    - priority: 1
      match:
        expression: >
          destination != '192.168.10.0/24' && communities.matchesEvery(['65000:1', '65000:2'])
      actions:
      - expression: med.set(12345)
    

    Replace the following:

    • BGP_ROUTE_POLICY_NAME: a name for the BGP route policy.
    • ROUTE_POLICY_TYPE: the type of BGP route policy you're creating. The type can be one of the following:

      • ROUTE_POLICY_TYPE_IMPORT: BGP route policies for inbound routes.
      • ROUTE_POLICY_TYPE_EXPORT: BGP route policies for outbound routes.
  2. Upload the BGP route policy:

    gcloud beta compute routers upload-route-policy ROUTER_NAME \
        --region=REGION \
        --policy-name=BGP_ROUTE_POLICY_NAME \
        --file-name=FILE_NAME \
        --file-format=yaml
    

    Replace the following:

    • ROUTER_NAME: the name of the Cloud Router
    • REGION: the region the Cloud Router is in
    • FILENAME: the filename that you're uploading
  3. Apply the BGP route policies to BGP peers:

    gcloud beta compute routers update-bgp-peer ROUTER_NAME \
        --region=REGION \
        --peer-name=PEER_NAME \
        --import-policies='IMPORT_POLICIES'
    

    Replace the following:

    • PEER_NAME: the BGP peer's name to apply the BGP route policy to.
    • IMPORT_POLICIES: a comma-separated list of import policies. Passing an empty string removes all import policies.

What's next