Example usage for com.amazonaws.auth PropertiesCredentials PropertiesCredentials

List of usage examples for com.amazonaws.auth PropertiesCredentials PropertiesCredentials

Introduction

In this page you can find the example usage for com.amazonaws.auth PropertiesCredentials PropertiesCredentials.

Prototype

public PropertiesCredentials(InputStream inputStream) throws IOException 

Source Link

Document

Reads the specified input stream as a stream of Java properties file content and extracts the AWS access key ID and secret access key from the properties.

Usage

From source file:fr.xebia.demo.amazon.aws.AmazonAwsIamAccountCreator.java

License:Apache License

public AmazonAwsIamAccountCreator() throws IOException {
    InputStream credentialsAsStream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("AwsCredentials.properties");
    Preconditions.checkNotNull(credentialsAsStream,
            "File 'AwsCredentials.properties' NOT found in the classpath");
    AWSCredentials awsCredentials = new PropertiesCredentials(credentialsAsStream);
    iam = new AmazonIdentityManagementClient(awsCredentials);

    ses = new AmazonSimpleEmailServiceClient(awsCredentials);
}

From source file:fr.xebia.demo.amazon.aws.AmazonAwsIamAccountCreatorV2.java

License:Apache License

public AmazonAwsIamAccountCreatorV2() {
    try {//  ww  w  .  j a  va  2s .c  om
        InputStream credentialsAsStream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("AwsCredentials.properties");
        Preconditions.checkNotNull(credentialsAsStream,
                "File 'AwsCredentials.properties' NOT found in the classpath");
        AWSCredentials awsCredentials = new PropertiesCredentials(credentialsAsStream);
        iam = new AmazonIdentityManagementClient(awsCredentials);

        ses = new AmazonSimpleEmailServiceClient(awsCredentials);

        ec2 = new AmazonEC2Client(awsCredentials);
        ec2.setEndpoint("ec2.eu-west-1.amazonaws.com");

        keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
        keyPairGenerator.initialize(1024, new SecureRandom());
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

From source file:fr.xebia.demo.amazon.aws.AmazonAwsInfrastructureMaker.java

License:Apache License

public AmazonAwsInfrastructureMaker() {
    try {//from  www  .ja va  2 s.c o  m
        InputStream credentialsAsStream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("AwsCredentials.properties");
        Preconditions.checkNotNull(credentialsAsStream,
                "File 'AwsCredentials.properties' NOT found in the classpath");
        AWSCredentials credentials = new PropertiesCredentials(credentialsAsStream);
        ec2 = new AmazonEC2Client(credentials);
        ec2.setEndpoint("ec2.eu-west-1.amazonaws.com");
        rds = new AmazonRDSClient(credentials);
        rds.setEndpoint("rds.eu-west-1.amazonaws.com");
        elb = new AmazonElasticLoadBalancingClient(credentials);
        elb.setEndpoint("elasticloadbalancing.eu-west-1.amazonaws.com");
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:fr.xebia.demo.amazon.aws.AmazonAwsSesEmailVerifier.java

License:Apache License

public static void main(String[] args) throws Exception {
    InputStream credentialsAsStream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("AwsCredentials.properties");
    Preconditions.checkNotNull(credentialsAsStream,
            "File 'AwsCredentials.properties' NOT found in the classpath");
    AWSCredentials awsCredentials = new PropertiesCredentials(credentialsAsStream);

    AmazonSimpleEmailService ses = new AmazonSimpleEmailServiceClient(awsCredentials);

    URL emailsToVerifyURL = Thread.currentThread().getContextClassLoader().getResource("emails-to-verify.txt");
    List<String> emailsToVerify = Resources.readLines(emailsToVerifyURL, Charsets.ISO_8859_1);

    for (String emailToVerify : emailsToVerify) {
        System.out.println(emailToVerify);
        Thread.sleep(10 * 1000);//from w  ww .  j  av a 2s  .c om
        ses.verifyEmailAddress(new VerifyEmailAddressRequest().withEmailAddress(emailToVerify));
    }
}

From source file:fr.xebia.demo.amazon.aws.AmazonAwsShutdowner.java

License:Apache License

public AmazonAwsShutdowner() throws IOException {
    InputStream credentialsAsStream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("AwsCredentials.properties");
    Preconditions.checkNotNull(credentialsAsStream,
            "File 'AwsCredentials.properties' NOT found in the classpath");
    AWSCredentials credentials = new PropertiesCredentials(credentialsAsStream);
    ec2 = new AmazonEC2Client(credentials);
    ec2.setEndpoint("ec2.eu-west-1.amazonaws.com");
    rds = new AmazonRDSClient(credentials);
    rds.setEndpoint("rds.eu-west-1.amazonaws.com");
    elb = new AmazonElasticLoadBalancingClient(credentials);
    elb.setEndpoint("elasticloadbalancing.eu-west-1.amazonaws.com");
}

From source file:fr.xebia.demo.amazon.aws.PetclinicInfrastructureEnforcer.java

License:Apache License

public PetclinicInfrastructureEnforcer() {
    try {//from   w  ww. j ava2  s .c om
        InputStream credentialsAsStream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("AwsCredentials.properties");
        Preconditions.checkNotNull(credentialsAsStream,
                "File 'AwsCredentials.properties' NOT found in the classpath");
        AWSCredentials credentials = new PropertiesCredentials(credentialsAsStream);
        ec2 = new AmazonEC2Client(credentials);
        ec2.setEndpoint("ec2.eu-west-1.amazonaws.com");
        rds = new AmazonRDSClient(credentials);
        rds.setEndpoint("rds.eu-west-1.amazonaws.com");
        elb = new AmazonElasticLoadBalancingClient(credentials);
        elb.setEndpoint("elasticloadbalancing.eu-west-1.amazonaws.com");
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:fr.xebia.training.troubleshooting.TroubleshootingTrainingInfrastructureCreator.java

License:Apache License

public TroubleshootingTrainingInfrastructureCreator() {
    try {/* w  w w .  j  ava 2  s.  co m*/
        InputStream credentialsAsStream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("AwsCredentials.properties");
        Preconditions.checkNotNull(credentialsAsStream,
                "File 'AwsCredentials.properties' NOT found in the classpath");
        AWSCredentials credentials = new PropertiesCredentials(credentialsAsStream);
        ec2 = new AmazonEC2Client(credentials);
        ec2.setEndpoint("ec2.eu-west-1.amazonaws.com");
        rds = new AmazonRDSClient(credentials);
        rds.setEndpoint("rds.eu-west-1.amazonaws.com");
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:fr.xebia.workshop.infrastructureascode.AmazonAwsPetclinicInfrastructureEnforcer.java

License:Apache License

public AmazonAwsPetclinicInfrastructureEnforcer() {
    try {//from   w  ww.  j  ava  2  s .co  m
        InputStream credentialsAsStream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("AwsCredentials.properties");
        Preconditions.checkNotNull(credentialsAsStream,
                "File 'AwsCredentials.properties' NOT found in the classpath");
        AWSCredentials credentials = new PropertiesCredentials(credentialsAsStream);
        ec2 = new AmazonEC2Client(credentials);
        ec2.setEndpoint("ec2.eu-west-1.amazonaws.com");
        rds = new AmazonRDSClient(credentials);
        rds.setEndpoint("rds.eu-west-1.amazonaws.com");
        elb = new AmazonElasticLoadBalancingClient(credentials);
        elb.setEndpoint("elasticloadbalancing.eu-west-1.amazonaws.com");
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:getting_started.CreateSecurityGroupApp.java

License:Open Source License

/**
 * @param args//from www.  ja va  2s  .c om
 */
public static void main(String[] args) {
    // Retrieves the credentials from an AWSCredentials.properties file.
    AWSCredentials credentials = null;
    try {
        credentials = new PropertiesCredentials(
                InlineGettingStartedCodeSampleApp.class.getResourceAsStream("AwsCredentials.properties"));
    } catch (IOException e1) {
        System.out.println("Credentials were not properly entered into AwsCredentials.properties.");
        System.out.println(e1.getMessage());
        System.exit(-1);
    }

    // Create the AmazonEC2Client object so we can call various APIs.
    AmazonEC2 ec2 = new AmazonEC2Client(credentials);

    // Create a new security group.
    try {
        CreateSecurityGroupRequest securityGroupRequest = new CreateSecurityGroupRequest("GettingStartedGroup",
                "Getting Started Security Group");
        ec2.createSecurityGroup(securityGroupRequest);
    } catch (AmazonServiceException ase) {
        // Likely this means that the group is already created, so ignore.
        System.out.println(ase.getMessage());
    }

    String ipAddr = "0.0.0.0/0";

    // Get the IP of the current host, so that we can limit the Security Group
    // by default to the ip range associated with your subnet.
    try {
        InetAddress addr = InetAddress.getLocalHost();

        // Get IP Address
        ipAddr = addr.getHostAddress() + "/10";
    } catch (UnknownHostException e) {
    }

    //System.exit(-1);
    // Create a range that you would like to populate.
    ArrayList<String> ipRanges = new ArrayList<String>();
    ipRanges.add(ipAddr);

    // Open up port 23 for TCP traffic to the associated IP from above (e.g. ssh traffic).
    ArrayList<IpPermission> ipPermissions = new ArrayList<IpPermission>();
    IpPermission ipPermission = new IpPermission();
    ipPermission.setIpProtocol("tcp");
    ipPermission.setFromPort(new Integer(22));
    ipPermission.setToPort(new Integer(22));
    ipPermission.setIpRanges(ipRanges);
    ipPermissions.add(ipPermission);

    try {
        // Authorize the ports to the used.
        AuthorizeSecurityGroupIngressRequest ingressRequest = new AuthorizeSecurityGroupIngressRequest(
                "GettingStartedGroup", ipPermissions);
        ec2.authorizeSecurityGroupIngress(ingressRequest);
    } catch (AmazonServiceException ase) {
        // Ignore because this likely means the zone has already been authorized.
        System.out.println(ase.getMessage());
    }
}

From source file:getting_started.InlineGettingStartedCodeSampleApp.java

License:Open Source License

/**
 * @param args/*from   ww  w  .j  a va2 s  .c  o m*/
 */
public static void main(String[] args) {
    //============================================================================================//
    //=============================== Submitting a Request =======================================// 
    //============================================================================================//

    // Retrieves the credentials from an AWSCredentials.properties file.
    AWSCredentials credentials = null;
    try {
        credentials = new PropertiesCredentials(
                InlineGettingStartedCodeSampleApp.class.getResourceAsStream("AwsCredentials.properties"));
    } catch (IOException e1) {
        System.out.println("Credentials were not properly entered into AwsCredentials.properties.");
        System.out.println(e1.getMessage());
        System.exit(-1);
    }

    // Create the AmazonEC2Client object so we can call various APIs.
    AmazonEC2 ec2 = new AmazonEC2Client(credentials);

    // Initializes a Spot Instance Request
    RequestSpotInstancesRequest requestRequest = new RequestSpotInstancesRequest();

    // Request 1 x t1.micro instance with a bid price of $0.03. 
    requestRequest.setSpotPrice("0.03");
    requestRequest.setInstanceCount(Integer.valueOf(1));

    // Setup the specifications of the launch. This includes the instance type (e.g. t1.micro)
    // and the latest Amazon Linux AMI id available. Note, you should always use the latest 
    // Amazon Linux AMI id or another of your choosing.
    LaunchSpecification launchSpecification = new LaunchSpecification();
    launchSpecification.setImageId("ami-8c1fece5");
    launchSpecification.setInstanceType("t1.micro");

    // Add the security group to the request.
    ArrayList<String> securityGroups = new ArrayList<String>();
    securityGroups.add("GettingStartedGroup");
    launchSpecification.setSecurityGroups(securityGroups);

    // Add the launch specifications to the request.
    requestRequest.setLaunchSpecification(launchSpecification);

    //============================================================================================//
    //=========================== Getting the Request ID from the Request ========================// 
    //============================================================================================//

    // Call the RequestSpotInstance API. 
    RequestSpotInstancesResult requestResult = ec2.requestSpotInstances(requestRequest);
    List<SpotInstanceRequest> requestResponses = requestResult.getSpotInstanceRequests();

    // Setup an arraylist to collect all of the request ids we want to watch hit the running
    // state.
    ArrayList<String> spotInstanceRequestIds = new ArrayList<String>();

    // Add all of the request ids to the hashset, so we can determine when they hit the 
    // active state.
    for (SpotInstanceRequest requestResponse : requestResponses) {
        System.out.println("Created Spot Request: " + requestResponse.getSpotInstanceRequestId());
        spotInstanceRequestIds.add(requestResponse.getSpotInstanceRequestId());
    }

    //============================================================================================//
    //=========================== Determining the State of the Spot Request ======================// 
    //============================================================================================//

    // Create a variable that will track whether there are any requests still in the open state.
    boolean anyOpen;

    // Initialize variables.
    ArrayList<String> instanceIds = new ArrayList<String>();

    do {
        // Create the describeRequest with tall of the request id to monitor (e.g. that we started).
        DescribeSpotInstanceRequestsRequest describeRequest = new DescribeSpotInstanceRequestsRequest();
        describeRequest.setSpotInstanceRequestIds(spotInstanceRequestIds);

        // Initialize the anyOpen variable to false  which assumes there are no requests open unless
        // we find one that is still open.
        anyOpen = false;

        try {
            // Retrieve all of the requests we want to monitor. 
            DescribeSpotInstanceRequestsResult describeResult = ec2
                    .describeSpotInstanceRequests(describeRequest);
            List<SpotInstanceRequest> describeResponses = describeResult.getSpotInstanceRequests();

            // Look through each request and determine if they are all in the active state.
            for (SpotInstanceRequest describeResponse : describeResponses) {
                // If the state is open, it hasn't changed since we attempted to request it.
                // There is the potential for it to transition almost immediately to closed or
                // cancelled so we compare against open instead of active.
                if (describeResponse.getState().equals("open")) {
                    anyOpen = true;
                    break;
                }

                // Add the instance id to the list we will eventually terminate.
                instanceIds.add(describeResponse.getInstanceId());
            }
        } catch (AmazonServiceException e) {
            // If we have an exception, ensure we don't break out of the loop.
            // This prevents the scenario where there was blip on the wire.
            anyOpen = true;
        }

        try {
            // Sleep for 60 seconds.
            Thread.sleep(60 * 1000);
        } catch (Exception e) {
            // Do nothing because it woke up early.
        }
    } while (anyOpen);

    //============================================================================================//
    //====================================== Canceling the Request ==============================// 
    //============================================================================================//

    try {
        // Cancel requests.
        CancelSpotInstanceRequestsRequest cancelRequest = new CancelSpotInstanceRequestsRequest(
                spotInstanceRequestIds);
        ec2.cancelSpotInstanceRequests(cancelRequest);
    } catch (AmazonServiceException e) {
        // Write out any exceptions that may have occurred.
        System.out.println("Error cancelling instances");
        System.out.println("Caught Exception: " + e.getMessage());
        System.out.println("Reponse Status Code: " + e.getStatusCode());
        System.out.println("Error Code: " + e.getErrorCode());
        System.out.println("Request ID: " + e.getRequestId());
    }

    //============================================================================================//
    //=================================== Terminating any Instances ==============================// 
    //============================================================================================//
    try {
        // Terminate instances.
        TerminateInstancesRequest terminateRequest = new TerminateInstancesRequest(instanceIds);
        ec2.terminateInstances(terminateRequest);
    } catch (AmazonServiceException e) {
        // Write out any exceptions that may have occurred.
        System.out.println("Error terminating instances");
        System.out.println("Caught Exception: " + e.getMessage());
        System.out.println("Reponse Status Code: " + e.getStatusCode());
        System.out.println("Error Code: " + e.getErrorCode());
        System.out.println("Request ID: " + e.getRequestId());
    }

}