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, 2019 in AWS by Divyanshi
2,716 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.

Hope this helps!

Want to learn more about Java SDK? Enroll for the Java course online here!

Thanks!

answered Feb 21, 2019 by Priyaj
• 58,100 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 AWS ...READ MORE

answered Feb 27, 2019 in AWS by Priyaj
• 58,100 points
1,550 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 5, 2018 in AWS by Shuvodip Ghosh
4,529 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, 2019 in AWS by Priyaj
• 58,100 points
1,129 views
0 votes
1 answer

How to create a subnet inside a VPC using AWS CLI?

To create a subnet inside a VPC ...READ MORE

answered Feb 21, 2019 in AWS by Priyaj
• 58,100 points
689 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to create a DynamoDB table using Java SDK?

The code to create a table in ...READ MORE

answered Feb 23, 2019 in AWS by Priyaj
• 58,100 points
2,524 views
+1 vote
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP