简体   繁体   中英

Create and download an AWS ec2 keypair using jclouds with the help of access, secret key

I am able to create AWS-EC2 instance using jClouds API with the help of access key and secret key,

AWS_ACCESS_KEY = this.config.getString("aws_access_key");
AWS_SECRET_ACCESS_KEY = this.config.getString("aws_secret_access_key");

if (AWS_ACCESS_KEY == null || AWS_SECRET_ACCESS_KEY == null) {
    throw new InstantiationError("Invalid AWS Credentails");
}

ec2Api = ContextBuilder.newBuilder(AWS_EC2).credentials(AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY).buildApi(EC2Api.class);
instanceApi = ec2Api.getInstanceApi().get();

How to create a Key pair on AWS-EC2 when user is creating an instance for first time (jCloud API). Do we need to use existing access key and secret key for every new instance.

Can anyone please help me in advising how to create a new keypair for every new instance.

Template template = compute.templateBuilder()
            .hardwareId(InstanceType.T1_MICRO)
            .imageId("us-east-1/ami-ccf615a5")
            .osFamily(OsFamily.AMZN_LINUX)
            .locationId("us-east-1")
            .osVersionMatches("12.10")
            .build();
    Template template = templateBuilder.build();

    TemplateOptions templateOptions = template.getOptions();
    AWSEC2TemplateOptions awsTemplateOption= templateOptions.as(AWSEC2TemplateOptions.class);
    awsTemplateOption.securityGroups("securityGroupName");
    awsTemplateOption.keyPair("keyPairName");

try {
        Set<? extends NodeMetadata> nodes = compute.createNodesInGroup(groupName,1,template);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM