Example usage for com.amazonaws.services.ec2 AmazonEC2 createRoute

List of usage examples for com.amazonaws.services.ec2 AmazonEC2 createRoute

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2 AmazonEC2 createRoute.

Prototype

CreateRouteResult createRoute(CreateRouteRequest createRouteRequest);

Source Link

Document

Creates a route in a route table within a VPC.

Usage

From source file:com.urbancode.terraform.tasks.aws.helpers.AWSHelper.java

License:Apache License

/**
 * Creates a route with given info. Attach Id can be either an instance (as a NAT) or an
 *  gateway. I f the attachId is an instance (starts with "i-") then the src/destination check
 *  on the instance will be disabled to allow it to act as a NAT.
 *
 * @param routeTableId the id of the route table to add this route to
 * @param destCidr// w  w w .  ja va2s.  com
 * @param attachId of the instance or internet gateway
 * @param ec2Client AmazonEC2 connection
 */
public void createRoute(String routeTableId, String destCidr, String attachId, AmazonEC2 ec2Client) {
    try {
        CreateRouteRequest request = new CreateRouteRequest().withDestinationCidrBlock(destCidr)
                .withRouteTableId(routeTableId);
        if (attachId.startsWith("i-")) {
            request = request.withInstanceId(attachId);
            // disable src/dest check to allow instance to run as NAT
            setSrcDestCheck(false, attachId, ec2Client);
        } else if (attachId.startsWith("igw-")) {
            request = request.withGatewayId(attachId);
        }
        ec2Client.createRoute(request);
    } catch (AmazonServiceException e) {
        log.error("Failed to create Route in Route Table " + routeTableId, e);
        if (!"InvalidRouteTableID.NotFound".equalsIgnoreCase(e.getErrorCode())) {
            // we're only going to swallow the expcetion if the gateway id was not found
            throw e;
        }
    }
}