下面是我使用 API 在 AWS 中创建 VPC 的代码,但出现错误。
代码:
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);
错误:
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)