How to create a VPC using Java SDK?

0 votes
Can anyone help me with the code to create a VPC using Java SDK?
Feb 21 in AWS by Divyanshi
80 views

1 answer to this question.

0 votes

You can create a VPC using the following code:-

package com.ec2application.ec2application;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.CreateVpcRequest;
import com.amazonaws.services.ec2.model.CreateVpcResult;
import com.amazonaws.services.ec2.model.Vpc;

public class CreateVPC 
{
private static final AWSCredentials AWS_CREDENTIALS;
    static {
        // Your accesskey and secretkey
        AWS_CREDENTIALS = new BasicAWSCredentials(
                "Access Key",
                "Security Key"
        );
    }
public static void main(String[] args) {
        // Set up the amazon ec2 client
        AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(AWS_CREDENTIALS))
                .withRegion(Regions.US_EAST_1)
                .build();
        System.out.println("Creating a VPC");
        CreateVpcRequest newVPC = new CreateVpcRequest("In");
        newVPC.setCidrBlock("10.0.0.0/16");
        CreateVpcResult res = ec2Client.createVpc(newVPC);
        Vpc vp = res.getVpc();
        String vpcId = vp.getVpcId();
        System.out.println("Created VPC " + vpcId);
    }
}

This will generate an output like this:-

Creating a VPC
Created VPC vpc-0ba1336c724dc62cc

Now you can verify whether your VPC is created or not on AWS Console.

You can see that your VPC is created successfully.

answered Feb 21 by Priyaj
• 56,160 points

Related Questions In AWS

0 votes
1 answer

How to create a EMR Cluster using Java AWS SDK?

The Java code for creating an EMR ...READ MORE

answered Feb 27 in AWS by Priyaj
• 56,160 points
59 views
0 votes
1 answer

How to create subnets in a vpc using boto3?

Here is the simple way of implementing ...READ MORE

answered Dec 4, 2018 in AWS by Shuvodip Ghosh
338 views
0 votes
1 answer

How to create a VPC using AWS CLI?

To create an Amazon VPC using AWS ...READ MORE

answered Feb 21 in AWS by Priyaj
• 56,160 points
59 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+1 vote
1 answer