简体   繁体   English

无法使用Java SDK在AWS中创建VPC

[英]Not able to create a VPC in AWS using Java SDK

Below is my code using the API to create VPC in AWS, but I am getting an error. 以下是我使用API​​在AWS中创建VPC的代码,但出现错误。

Code: 码:

CreateAccessKeyRequest key = new CreateAccessKeyRequest();
BasicAWSCredentials cred = new BasicAWSCredentials("", "");
key.setRequestCredentials(cred);
AmazonEC2 ec2 = new AmazonEC2Client();
System.out.println("Creating VPC.....\n");
CreateVpcRequest newVPC = new CreateVpcRequest("In");
newVPC.setRequestCredentials(key.getRequestCredentials());
String cidrBlock = "192.168.1.70/28";
newVPC.setCidrBlock(cidrBlock);
newVPC.setInstanceTenancy(Tenancy.Default);
AmazonIdentityManagementClient client = new AmazonIdentityManagementClient(cred);
CreateVpcResult res = ec2.createVpc(newVPC);
Vpc vp = res.getVpc();
vp.setIsDefault(true);
String vpcId = vp.getVpcId();
System.out.println("Created VPC" + vpcId);

Error: 错误:

Exception in thread "main" com.amazonaws.AmazonClientException: Unable to load AWS credentials from any provider in the chain
    at com.amazonaws.auth.AWSCredentialsProviderChain.getCredentials(AWSCredentialsProviderChain.java:117)
    at com.amazonaws.services.ec2.AmazonEC2Client.invoke(AmazonEC2Client.java:10540)
    at com.amazonaws.services.ec2.AmazonEC2Client.createVpc(AmazonEC2Client.java:5709)
    at CreateUserRequest.main(CreateUserRequest.java:29)

There are several issues with the code snippet above. 上面的代码段有几个问题。

First, it is a bad idea to hardcode an access key / secret key. 首先,对访问密钥/秘密密钥进行硬编码是一个坏主意。 These should be stored in an external configuration file or an environment variables. 这些应存储在外部配置文件或环境变量中。 If this code is meant to run from an EC2 instance, you should use "Roles" and Instance Profiles instead. 如果该代码旨在从EC2实例运行,则应改用“ Roles”和“ Instance Profiles”。 This is clearly explained at http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/credentials.html http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/credentials.html上对此有清楚的说明。

That being said, if this is just for a quick test, let's assume hard coding access key and secret key is indeed a possibility, there are other problems at code level : 话虽如此,如果这只是一个快速测试,我们假设硬编码访问密钥和秘密密钥确实有可能,在代码级别还有其他问题:

  • you do not need to use CreateAccessKeyRequest as this is the API call to generate a new Access key / Secret Key 您不需要使用CreateAccessKeyRequest因为这是用于生成新的访问密钥/秘密密钥的API调用

  • your AmazonEC2Client has no reference to your credentials provider 您的AmazonEC2Client没有参考您的凭证提供商

  • there is no need to set the credentials provider for the VPC Request itself 无需为VPC请求本身设置凭据提供程序

  • setInstanceTenancy is not required, this is the default setInstanceTenancy不是必需的,这是默认设置

  • vp.setIsDefault(true); is useless. 是没用的。 If you want to create a Default VPC, make a request to our Support team, they will flag your VPC as the default one (see https://aws.amazon.com/premiumsupport/knowledge-center/deleted-default-vpc/ ) 如果要创建默认VPC,请向我们的支持团队提出请求,他们会将您的VPC标记为默认VPC(请参阅https://aws.amazon.com/premiumsupport/knowledge-center/deleted-default-vpc/

Here is a modified code sample that create a VPC 这是创建VPC的修改后的代码示例

package com.stormacq;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.*;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.CreateVpcRequest;
import com.amazonaws.services.ec2.model.CreateVpcResult;
import com.amazonaws.services.ec2.model.Vpc;

public class Main {

    public static void main(String[] args) {
        BasicAWSCredentials cred = new BasicAWSCredentials("AK...OQ", "gH...tp");
        AmazonEC2 ec2 = new AmazonEC2Client(cred);
        ec2.setRegion(Region.getRegion(Regions.US_EAST_1));

        System.out.println("Creating VPC...");
        CreateVpcRequest newVPC = new CreateVpcRequest("In");

        newVPC.setCidrBlock("192.168.1.70/28");
        CreateVpcResult res = ec2.createVpc(newVPC);
        Vpc vp = res.getVpc();

        String vpcId = vp.getVpcId();
        System.out.println("Created VPC " + vpcId);
    }
}

Thanks Sébastien Stormacq for supporting me with your answer. 感谢SébastienStormacq支持我的回答。 We have found the solution for this issue. 我们已经找到解决该问题的方法。 Actually my windows desktop time was 6 minutes late to the actual time. 实际上,我的Windows桌面时间比实际时间晚6分钟。 So there its getting the conflict to validate the AWS credentials. 因此,在验证AWS凭证时会遇到冲突。 We synced the time manually with exact time and now its working like a charm. 我们手动将时间与准确的时间同步,现在它的运行就像是一种魅力。 Please once check the Windows time when one is getting the same issue. 请一次检查Windows出现相同问题的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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