Thursday, March 14, 2024

OCI (Oracle Cloud Infrastructure) SDK to provision VCN, Subnet and establish VPN connectivity

 Oracle Cloud Infrastructure (OCI) Virtual Cloud Network (VCN) is the networking layer of the Oracle Cloud Infrastructure, equivalent to the Virtual Private Cloud (VPC) in other cloud providers. A VCN allows you to set up a customizable and private network in Oracle’s cloud. You can control the VCN’s IP address range, create subnets, and configure route tables and gateways to manage traffic within or outside the VCN.

  1. Private and Isolated Network: A VCN provides an isolated network within the Oracle Cloud Infrastructure.
  2. Customizable: You can set the IP CIDR block, create subnets, and use Network Security Groups or Security Lists to control inbound and outbound traffic.
  3. Route Tables: Define how the traffic is routed within your VCN or to the internet.
  4. Internet Gateway: Allows traffic to flow between your VCN and the internet.
  5. NAT Gateway: Allows instances in a private subnet to initiate outbound connections to the internet without exposing their IP addresses.
  6. VPN Gateway: For secure, encrypted communication between your on-premise network and your VCN.
  7. Load Balancer: Distributes incoming traffic across multiple targets to ensure high availability.
  8. Service Gateway: Provides a path for private traffic between your VCN and supported Oracle services.


This code snippet creates a VCN, subnet, security list, and VPN using the OCI Java SDK, utilizing the Identity service client and the respective create methods. Make sure to handle exceptions appropriately in your production code.


Make sure to replace "your_compartment_id", "YourVCN", "YourSubnet", "YourSecurityList", and "YourVPN" with appropriate values for your Oracle Cloud tenancy, Virtual Cloud Network (VCN), subnet, security list, and VPN display names respectively.

Ensure that your OCI configuration file (typically found at ~/.oci/config) is properly configured with your user credentials and the correct region.


import com.oracle.bmc.identity.IdentityClient;

import com.oracle.bmc.identity.model.CreateVpnDetails;

import com.oracle.bmc.identity.model.CreateSubnetDetails;

import com.oracle.bmc.identity.model.CreateSecurityListDetails;

import com.oracle.bmc.identity.model.CreateSecurityRuleDetails;

import com.oracle.bmc.identity.requests.CreateVpnRequest;

import com.oracle.bmc.identity.requests.CreateSubnetRequest;

import com.oracle.bmc.identity.requests.CreateSecurityListRequest;

import com.oracle.bmc.identity.responses.CreateVpnResponse;

import com.oracle.bmc.identity.responses.CreateSubnetResponse;

import com.oracle.bmc.identity.responses.CreateSecurityListResponse;

import com.oracle.bmc.Region;

import com.oracle.bmc.auth.AuthenticationDetailsProvider;

import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;

import com.oracle.bmc.model.BmcException;


import java.util.Collections;


public class InfrastructureProvisioning {

    public static void main(String[] args) {

        String compartmentId = "your_compartment_id";

        String vcnDisplayName = "YourVCN";

        String subnetDisplayName = "YourSubnet";

        String securityListDisplayName = "YourSecurityList";

        String vpnDisplayName = "YourVPN";


        // Path to your OCI configuration file

        String configurationFilePath = "~/.oci/config";


        // Get the authentication details from the OCI configuration file

        AuthenticationDetailsProvider provider =

                new ConfigFileAuthenticationDetailsProvider(configurationFilePath, "DEFAULT");


        IdentityClient identityClient = new IdentityClient(provider);

        identityClient.setRegion(Region.US_PHOENIX_1); // Change to appropriate region


        try {

            // Create VCN

            CreateVcnDetails createVcnDetails = CreateVcnDetails.builder()

                    .cidrBlock("10.0.0.0/16")

                    .compartmentId(compartmentId)

                    .displayName(vcnDisplayName)

                    .build();


            CreateVcnRequest createVcnRequest = CreateVcnRequest.builder()

                    .createVcnDetails(createVcnDetails)

                    .build();


            Vcn vcn = identityClient.createVcn(createVcnRequest).getVcn();


            // Create Subnet

            CreateSubnetDetails createSubnetDetails = CreateSubnetDetails.builder()

                    .cidrBlock("10.0.0.0/24")

                    .compartmentId(compartmentId)

                    .displayName(subnetDisplayName)

                    .vcnId(vcn.getId())

                    .build();


            CreateSubnetRequest createSubnetRequest = CreateSubnetRequest.builder()

                    .createSubnetDetails(createSubnetDetails)

                    .build();


            Subnet subnet = identityClient.createSubnet(createSubnetRequest).getSubnet();


            // Create Security List

            CreateSecurityRuleDetails createSecurityRuleDetails = CreateSecurityRuleDetails.builder()

                    .direction(CreateSecurityRuleDetails.Direction.Egress)

                    .destination("0.0.0.0/0")

                    .protocol("all")

                    .build();


            CreateSecurityListDetails createSecurityListDetails = CreateSecurityListDetails.builder()

                    .compartmentId(compartmentId)

                    .displayName(securityListDisplayName)

                    .egressSecurityRules(Collections.singletonList(createSecurityRuleDetails))

                    .ingressSecurityRules(Collections.singletonList(createSecurityRuleDetails))

                    .vcnId(vcn.getId())

                    .build();


            CreateSecurityListRequest createSecurityListRequest = CreateSecurityListRequest.builder()

                    .createSecurityListDetails(createSecurityListDetails)

                    .build();


            SecurityList securityList = identityClient.createSecurityList(createSecurityListRequest).getSecurityList();


            // Create VPN

            CreateVpnDetails createVpnDetails = CreateVpnDetails.builder()

                    .compartmentId(compartmentId)

                    .displayName(vpnDisplayName)

                    .vcnId(vcn.getId())

                    .build();


            CreateVpnRequest createVpnRequest = CreateVpnRequest.builder()

                    .createVpnDetails(createVpnDetails)

                    .build();


            Vpn vpn = identityClient.createVpn(createVpnRequest).getVpn();


            System.out.println("VPN Created: " + vpn.getId());

            System.out.println("Subnet Created: " + subnet.getId());

            System.out.println("Security List Created: " + securityList.getId());

        } catch (BmcException e) {

            System.out.println("Error: " + e.getMessage());

        } finally {

            identityClient.close();

        }

    }

}


OCI Knowledge Series: OCI Infrastructure components

  Oracle Cloud Infrastructure (OCI) provides a comprehensive set of infrastructure services that enable you to build and run a wide range of...