Example #1
0
  public static void main(String[] args) {
    System.out.println("Hello, World!");
    AmazonEC2Client ec2 = new AmazonEC2Client();
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    ec2.setRegion(usWest2);
    RunInstancesRequest req = new RunInstancesRequest();
    req.setImageId("ami-5189a661");
    req.setInstanceType("t2.micro");
    req.setKeyName("aws_pt-user");

    req.setMinCount(1);
    req.setMaxCount(1);

    RunInstancesResult result = ec2.runInstances(req);
    System.out.println(result.toString());
  }
Example #2
0
  private AmazonEC2Client getClientForAccount(final String accountId, final Region region) {
    AWSSecurityTokenServiceClient stsClient =
        new AWSSecurityTokenServiceClient(new ProfileCredentialsProvider());

    AssumeRoleRequest assumeRequest =
        new AssumeRoleRequest()
            .withRoleArn("arn:aws:iam::ACCOUNT_ID:role/fullstop-role")
            .withDurationSeconds(3600)
            .withRoleSessionName("fullstop-role");

    AssumeRoleResult assumeResult = stsClient.assumeRole(assumeRequest);

    BasicSessionCredentials temporaryCredentials =
        new BasicSessionCredentials(
            assumeResult.getCredentials().getAccessKeyId(),
            assumeResult.getCredentials().getSecretAccessKey(),
            assumeResult.getCredentials().getSessionToken());

    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(temporaryCredentials);
    amazonEC2Client.setRegion(region);

    return amazonEC2Client;
  }
 private AmazonEC2 initClient() {
   AwsPluginExtension aws = project.getExtensions().getByType(AwsPluginExtension.class);
   AmazonEC2Client client = aws.createClient(AmazonEC2Client.class, profileName);
   client.setRegion(aws.getActiveRegion(region));
   return client;
 }