class AwsClientFactory {

    @Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME)
    public AmazonEC2 getEc2Client(AwsParamsDto awsParamsDto)
    {
        // Get client configuration.
        ClientConfiguration clientConfiguration = awsHelper.getClientConfiguration(awsParamsDto);

        // If specified, use the AWS credentials passed in.
        if (StringUtils.isNotBlank(awsParamsDto.getAwsAccessKeyId()))
        {
            return AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(
                new BasicSessionCredentials(awsParamsDto.getAwsAccessKeyId(), awsParamsDto.getAwsSecretKey(), awsParamsDto.getSessionToken())))
                .withClientConfiguration(clientConfiguration).withRegion(awsParamsDto.getAwsRegionName()).build();
        }
        // Otherwise, use the default AWS credentials provider chain.
        else
        {
            return AmazonEC2ClientBuilder.standard().withClientConfiguration(clientConfiguration).withRegion(awsParamsDto.getAwsRegionName()).build();
        }
    }

}