2017-11-25 186 views
0

我正在嘗試使用AWS JAVA API創建新的EC2 instance。但是我得到消息,但以下情況除外使用EC2創建新的EC2實例時創建標記時出錯Java API

異常線程「main」 com.amazonaws.services.ec2.model.AmazonEC2Exception:標籤規範 資源類型必須有一個值(服務:AmazonEC2;狀態代碼: 400; 錯誤代碼:InvalidParameterValue;請求ID:XXXXXX)

public static void main(String[] args) { 
    Ec2Utilities utils = new Ec2Utilities(); 
    Map<String, String> tagMap = new HashMap<String, String>(); 
    tagMap.put("name", "newinstance"); 
    tagMap.put("category", "cat1"); 
    utils.createInstanceFromAmi("t2.micro", "ami-sdfsds", 1, "sg-sdfsd", "nammme", tagMap); 
} 

public void createInstanceFromAmi(String instanceType, String amiId, int count, String securityGroup, 
     String keyName, Map<String, String> tagMap) { 
    AmazonEC2 ec2Client = Auth.getCredentails(); 
    TagSpecification tagSpecs = new TagSpecification(); 
    tagSpecs.setTags(buildTags(tagMap)); 
    RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withTagSpecifications(tagSpecs) 
                     .withInstanceType(instanceType) 
                     .withImageId(amiId) 
                     .withMinCount(count) 
                     .withMaxCount(2) 
                     .withSecurityGroupIds(securityGroup) 
                     .withKeyName(keyName); 

    RunInstancesResult runInstances = ec2Client.runInstances(runInstancesRequest); 

} 

private List<Tag> buildTags(Map<String, String> tagMap) { 
    List<Tag> tagList = new ArrayList<Tag>(); 
    tagMap.forEach((k, v) -> { 
     tagList.add(new Tag(k, v)); 
    }); 
    System.out.println(tagList); 
    return tagList; 
} 

回答

0

我固定它自己。我加入這個

List<TagSpecification> tagSpecifications = new ArrayList<>(); 
    tagSpecifications.add(new TagSpecification().withTags(buildTags(tagMap)) 

buildTags(tagMap)返回`榜」