CodeGuru AWSSDK

  1. let’s check what kind of code review has doing in pull-request.(almost wait 5min.)

  2. Go to CodeCommit’s console: https://console.aws.amazon.com/codesuite/codecommit/repositories
        

  3. Select Repositories of Source on the left and click concurrencysample. pr1

  4. Select pr-concurrencytest for Pull requests of Repositories in the left source. maybe we need wait a few minite. pr2

  5. If CodeCommit and Repository are connected, the following screen will appear. Select Activity at the top. pr3

  6. The code review comment was attached to the code that I thought had no problems.

    pr4

  7. Temporary Security Credentials rather than long-term Security Credentials such as AccessKey and SceretKey. CodeGuru tells you how and how to use the AWS SDK secure. These are things that cannot be found in simple unit tests or experience!

  8. Let’s change SingletonRepo.java code. Delete the AWS SDK credential code. And you just need to give EC2 the IAM Role to access the necessary resources. let’s delete the AWS SDK credentials related code of SingleRepo.java.

    ...
    -   static AWSCredentials ac;
    ...
    
    public synchronized static SingletonRepo getInstance() {
        if (singletonrepo == null) {
            singletonrepo = new SingletonRepo();
    
    -        ac = new BasicAWSCredentials(ACCESS_KEY, SECERET_KEY);
    
            dynamodb = AmazonDynamoDBClientBuilder
                    .standard()
            .withRegion("us-west-2")
    -       .withCredentials(new AWSStaticCredentialsProvider(ac) )
            .build();
            System.out.println("singleton repo instance created");
        }
        return singletonrepo;
    }
    
  9. And… We have other problem. CodeGuru recommend that you use the Regions enum. not using string. Regions is an enumeration of all publicly available regions. To create a client with a region from the enum, use the following code.

    pr1

    ...
    +   import com.amazonaws.regions.Regions;
    ...
    
    public synchronized static SingletonRepo getInstance() {
        if (singletonrepo == null) {
            singletonrepo = new SingletonRepo();
    
            dynamodb = AmazonDynamoDBClientBuilder
                    .standard()
    -       .withRegion("us-west-2")
    +       .withRegion(Regions.US_WEST_2)
            .build();
            System.out.println("singleton repo instance created");
        }
        return singletonrepo;
    }
    
  10. SingletonRepo.java fixed code is here.

    package com.example.concurrencysample;
    
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.concurrent.*;
    
    import com.amazonaws.auth.AWSCredentials;
    import com.amazonaws.auth.AWSStaticCredentialsProvider;
    import com.amazonaws.auth.BasicAWSCredentials;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
    import com.amazonaws.services.dynamodbv2.document.DynamoDB;
    import com.amazonaws.services.dynamodbv2.document.Item;
    import com.amazonaws.services.dynamodbv2.document.Table;
    import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
    import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
    import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
    import com.amazonaws.services.dynamodbv2.model.KeyType;
    import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
    import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
    import com.amazonaws.regions.Regions;
    
    public class SingletonRepo {
        private static String ACCESS_KEY = "";
        private static String SECERET_KEY = "";
        private ConcurrentHashMap<Integer, Concurrency> map = new ConcurrentHashMap<>();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        private static SingletonRepo singletonrepo = null;
        static AmazonDynamoDB dynamodb;
    
        private SingletonRepo() {
        }
    
        public synchronized static SingletonRepo getInstance() {
            if (singletonrepo == null) {
                singletonrepo = new SingletonRepo();
    
    
                dynamodb = AmazonDynamoDBClientBuilder
                        .standard()
                .withRegion(Regions.US_WEST_2)
                .build();
                System.out.println("singleton repo instance created");
            }
            return singletonrepo;
        }
        public Concurrency get(int product_code){
            return map.get(product_code);
        }
    
        public Concurrency putKey(int product_code ) {           
            Date date = Util.getRandomDate();
            if (map.containsKey(product_code)) {
                return map.get(product_code);
            }else{
                return map.put(product_code, new Concurrency(product_code,"test" , date));
            }   
        }
        public void delKey(int product_code ) { 
            if (map.containsKey(product_code)) {
                map.remove(product_code);
            }
        }
        public int getMapCount() {
            return map.size();
        }
        public synchronized void clearHashMap() {
            map.clear();
            System.out.println("ClearMap!");
        }
        public Concurrency getKey(int product_number) {
            return map.get(product_number);  
        }
    }
    
  11. When developing AWS applications, a common approach is to use the shared configuration file (described in Using the Default Credential Provider Chain) to set the region for local development, and rely on the default region provider chain to determine the region when running on AWS infrastructure. This greatly simplifies client creation and keeps your application portable.

  12. You can evaluate whether CodeGuru’s suggestions are really useful through feedback. These can contribute to better CodeGuru comments. pr4

And.. let`s see more..