Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if running in AWS before initializing #28

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions src/main/java/com/meltmedia/jgroups/aws/AWS_PING.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
*/
package com.meltmedia.jgroups.aws;

import com.amazonaws.regions.RegionUtils;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
import com.amazonaws.services.ec2.model.Filter;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.util.EC2MetadataUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.jgroups.Address;
Expand Down Expand Up @@ -174,27 +177,32 @@ public class AWS_PING extends Discovery {
public void init() throws Exception {
super.init();

//get the instance identity
try (CloseableHttpClient client = HttpClients.createDefault()) {
this.instanceIdentity = InstanceIdentity.getIdentity(client);
}
if(Regions.getCurrentRegion() != null) {
//get the instance identity
try (CloseableHttpClient client = HttpClients.createDefault()) {
this.instanceIdentity = InstanceIdentity.getIdentity(client);
}

//setup ec2 client
this.ec2 = EC2Factory.create(
instanceIdentity,
access_key,
secret_key,
credentials_provider_class,
new CredentialsProviderFactory(),
log_aws_error_messages);
//setup ec2 client
this.ec2 = EC2Factory.create(
instanceIdentity,
access_key,
secret_key,
credentials_provider_class,
new CredentialsProviderFactory(),
log_aws_error_messages);

this.ipAddressUtils = new IPAddressUtils(port_number, port_range);
this.tagUtils = new TagsUtils(ec2, instanceIdentity, tags).validateTags();
this.filterUtils = new FilterUtils(filters, tagUtils);
this.ipAddressUtils = new IPAddressUtils(port_number, port_range);
this.tagUtils = new TagsUtils(ec2, instanceIdentity, tags).validateTags();
this.filterUtils = new FilterUtils(filters, tagUtils);

log.info("Configured for instance: " + instanceIdentity.instanceId);
filterUtils.getAwsFilters().ifPresent(f -> log.info("Configured with filters [%s]", f));
tagUtils.getAwsTagNames().ifPresent(t -> log.info("Configured with tags [%s]", t));
log.info("Configured for instance: " + instanceIdentity.instanceId);
filterUtils.getAwsFilters().ifPresent(f -> log.info("Configured with filters [%s]", f));
tagUtils.getAwsTagNames().ifPresent(t -> log.info("Configured with tags [%s]", t));
}
else {
log.debug("Could not determine AWS Region. Assuming instance is not running in AWS...");
}
}

/**
Expand Down