/** * Allocate an elastic IP address */ public DeferredResult<String> allocateElasticIPAddress() { AllocateAddressRequest req = new AllocateAddressRequest() .withDomain(DomainType.Vpc); String message = "Allocate AWS Elastic IP Address for use with instances in a VPC."; AWSDeferredResultAsyncHandler<AllocateAddressRequest, AllocateAddressResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message); this.client.allocateAddressAsync(req, handler); return handler.toDeferredResult() .thenApply(AllocateAddressResult::getAllocationId); }
public static void main(String[] args) { final String USAGE = "To run this example, supply an instance id\n" + "Ex: AllocateAddress <instance_id>\n"; if (args.length != 1) { System.out.println(USAGE); System.exit(1); } String instance_id = args[0]; final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); AllocateAddressRequest allocate_request = new AllocateAddressRequest() .withDomain(DomainType.Vpc); AllocateAddressResult allocate_response = ec2.allocateAddress(allocate_request); String allocation_id = allocate_response.getAllocationId(); AssociateAddressRequest associate_request = new AssociateAddressRequest() .withInstanceId(instance_id) .withAllocationId(allocation_id); AssociateAddressResult associate_response = ec2.associateAddress(associate_request); System.out.printf( "Successfully associated Elastic IP address %s " + "with instance %s", associate_response.getAssociationId(), instance_id); }
public static Address allocateAddress(AmazonEC2 ec2, DomainType domainType) { AllocateAddressRequest addressRequest = new AllocateAddressRequest().withDomain(domainType); AllocateAddressResult addressResult = ec2.allocateAddress(addressRequest); Address address = new Address().withAllocationId(addressResult.getAllocationId()) .withDomain(addressResult.getDomain()).withPublicIp(addressResult.getPublicIp()); return address; }
/** * TODO: メソッドコメント * * @param awsProcessClient * @return */ public AwsAddress createAddress(AwsProcessClient awsProcessClient) { // Elastic IPの確保 AllocateAddressRequest request = new AllocateAddressRequest(); if (BooleanUtils.isTrue(awsProcessClient.getPlatformAws().getVpc())) { request.withDomain(DomainType.Vpc); } String publicIp; try { AllocateAddressResult result = awsProcessClient.getEc2Client().allocateAddress(request); publicIp = result.getPublicIp(); } catch (AutoException e) { // Elastic IPの上限オーバーの場合 if (e.getCause() instanceof AmazonServiceException && "AddressLimitExceeded".equals(((AmazonServiceException) e.getCause()).getErrorCode())) { throw new AutoApplicationException("EPROCESS-000134"); } throw e; } // イベントログ出力 processLogger.debug(null, null, "AwsElasticIpAllocate", new Object[] { awsProcessClient.getPlatform().getPlatformName(), publicIp }); // AWSアドレス情報を作成 AwsAddress awsAddress = new AwsAddress(); awsAddress.setUserNo(awsProcessClient.getUserNo()); awsAddress.setPlatformNo(awsProcessClient.getPlatform().getPlatformNo()); awsAddress.setPublicIp(publicIp); awsAddress.setComment("Allocate at " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")); awsAddressDao.create(awsAddress); return awsAddress; }
/** * Allocate Elastic IPs as needed and assign it to the endpoints. If there are * any errors, roll back by releasing all allocated IPs if possible * * @param vpnEndpoints */ private void allocateElasticIPs(List<VPNEndpoint> vpnEndpoints) { for (VPNEndpoint vpnEndpoint : vpnEndpoints) { ec2Client.setEndpoint(vpnEndpoint.getRegion().getEndpoint()); AllocateAddressResult allocAddrResult = ec2Client.allocateAddress(new AllocateAddressRequest().withDomain(DomainType.Vpc)); String publicIp = allocAddrResult.getPublicIp(); vpnEndpoint.setElasticIPAddress(publicIp); vpnEndpoint.setElasticIPAllocationId(allocAddrResult.getAllocationId()); LOG.debug("Allocated elastic IP " + publicIp + " in " + vpnEndpoint.getRegion().getEndpoint()); } }
private int startByName(Ec2CommandOptions options) throws FileNotFoundException { String name = options.getName(); InputStream inputStream = new FileInputStream(new File(options.getCredentialsPath())); ConfigProvider.loadConfigure(inputStream); AmazonEC2 ec2 = AwsEc2Client.getEc2(); // Check Exists Instance Instance instance = AwsEc2Client.findInstanceByName(ec2, name); if (instance == null) { System.err.println("Not exists instance (name = " + name + ")."); return 2; } String instanceId = instance.getInstanceId(); System.out.println("Exists instance (id = " + instanceId + ")"); // Start Ec2 Instance InstanceStateChange stateChange = AwsEc2Client.startInstance(ec2, instanceId); AwsEc2Client.showStateChange(stateChange, "Starting Instance"); // Allocate Address DomainType domainType = (instance.getVpcId() == null) ? DomainType.Standard : DomainType.Vpc; Address address = AwsEc2Client.allocateAddress(ec2, domainType); String publicIp = address.getPublicIp(); System.out.println("Allocated Address(" + publicIp + ", " + address.getAllocationId() + ")"); if (address != null) { // TODO: Wait for Starting Instance. waitForStartingInstance(); try { // Associate Address String associateAddress = AwsEc2Client.associateAddress(ec2, address, instanceId); System.out.println("Associated Address(" + publicIp + ", " + associateAddress + ")"); String domain = options.getDomain(); if (domain != null) { // Attach Domain to EIP AmazonRoute53 route53 = AwsRoute53Client.getRoute53(); ChangeInfo attachedResult = AwsRoute53Client.attachDomainToEip(route53, publicIp, domain); if (attachedResult != null) { System.out.println("Attached domain(" + domain + ")"); } else { System.err.println("Not Found Available Hosted Zone for specified Domain(" + domain + ")"); } } } catch (AmazonServiceException e) { AwsEc2Client.releaseAddress(ec2, address); System.out.println("Released Address (" + publicIp + ")"); return 2; } } return 0; }