Wednesday, September 6, 2017

AWS Java SDK Example

AWS Java SDK Example

Connecting to AWS and printing all volumes



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class java-ec2-example{
    public static void main(String[] args){
        String myRegion = "us-east-1";
        String accessKey = "xxxx";
        String secretKey = "xxxx";
        String sessionTk = "xxxx";
        
        //For programmatic access user
        BasicAWSCredentials awsCreds =  new BasicAWSCredentials (accessKey, secretKey);
        
        //For temporary credential
        BasicSessionCredentials sesCreds = new BasicSessionCredentials (accessKey, secretKey, sessionTk);

        //For other credential (e.g. Instance IAM, Environment Variable, etc) leave this as null 
        AWSStaticCredentialsProvider myCredential = null;

        //For this example, we use session credential
        myCredential = new AWSStaticCredentialsProvider(sesCreds);

        AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard()
                     .withCredentials(myCredential)
                     .withRegion(myRegion)
                     .build();

        List<Volume> volumes = ec2.describeVolumes(new DescribeVolumesRequest()).getVolumes();
        for(int i=0;i<volumes.size();i++){
            System.out.println(volumes.get(i));
        }
    }
}

Lines 9-18 are various means to connect to AWS. If you want to rely on the environment variables, you can just comment out line 18 and leave myCredentials as null.

Line 20 is used to build client connector to EC2. Similiar builders are available for all AWS services.

Line 25 returns all the volumes in your account.

Line 26 loops and prints all the volumes.

AWS WAF log4j query

How to query AWS WAF log for log4j attacks 1. Setup your Athena table using this instruction https://docs.aws.amazon.com/athena/latest/ug/wa...