javaamazon-web-servicesaws-sdkamazon-cloudfrontaws-sdk-java

Can't add new alternative domain name to CloudFront resource using AWS SDK for Java 2.x


I'm having difficulties trying to add a new Alternative Domain Name (CNAMEs) to an existing CloudFront resource using the AWS SDK for Java v2.x

This is the code snippet I'm using so far:

// First I get the actual resource from AWS
GetDistributionResponse distributionInformation = cloudFrontclient
        .getDistribution(GetDistributionRequest.builder().id(input.getDistributionId())
        .build());

// Then I extract the part I want to edit
DistributionConfig config = distributionInformation.distribution().distributionConfig();

// so far so good, I'm able to see my data as intended 

// The next thing is to try adding the new alias, and of course I can't as that array is Unmodifiable! 
// Meaning that I'm  always getting an: java.lang.UnsupportedOperationException
config.aliases().items().add(input.getAlternativeDomain()); 

// If the previous line worked or I find an alternative solution I'm planning to make the following update request
UpdateDistributionRequest updateDistributionRequest = UpdateDistributionRequest
                .builder()
                .distributionConfig(config)
                .build();

cloudFrontclient.updateDistribution(updateDistributionRequest);

I'm kind of lost here, I'm not exactly sure how this is supposed to work.

I'll appreciate any help I can get

Thanks in advance


Solution

  • I confirmed that the methods that belong to DistributionConfig - even comment - seem to be read-only when you use the object returned from distributionConfig

        Distribution disObject = response.distribution();
        DistributionConfig config = disObject.distributionConfig();
    

    The solution is to create a new DistributionConfig object by using the builder method (see below). Add the new values and then also read in the values that do not change. Otherwise a Java exception is thrown.

    Here I add a new comment as an example of modifying a Distribution.

    public static void main(String[] args) {
    
            CloudFrontClient cloudFrontClient = CloudFrontClient.builder()
                    .region(Region.AWS_GLOBAL)
                    .build();
    
            try {
    
                // Lets get the Distribution to modify
                GetDistributionRequest disRequest = GetDistributionRequest.builder()
                        .id("E90U7J6Pxxxxx")
                        .build();
    
                GetDistributionResponse response = cloudFrontClient.getDistribution(disRequest);
                Distribution disObject = response.distribution();
                DistributionConfig config = disObject.distributionConfig();
    
                // Create a new  DistributionConfig object and add new values to comment and aliases
                DistributionConfig config1 = DistributionConfig.builder()
                        .aliases(config.aliases()) // You can pass in new values here
                        .comment("New Comment")
                        .cacheBehaviors(config.cacheBehaviors())
                        .priceClass(config.priceClass())
                        .defaultCacheBehavior(config.defaultCacheBehavior())
                        .enabled(config.enabled())
                        .callerReference(config.callerReference())
                        .logging(config.logging())
                        .originGroups(config.originGroups())
                        .origins(config.origins())
                        .restrictions(config.restrictions())
                        .defaultRootObject(config.defaultRootObject())
                        .webACLId(config.webACLId())
                        .httpVersion(config.httpVersion())
                        .viewerCertificate(config.viewerCertificate())
                        .customErrorResponses(config.customErrorResponses())
                        .build();
    
    
                    UpdateDistributionRequest updateDistributionRequest = UpdateDistributionRequest.builder()
                        .distributionConfig(config1)
                        .id(disObject.id())
                        .ifMatch(response.eTag())
                         .build();
    
                cloudFrontClient.updateDistribution(updateDistributionRequest);
    
    
            } catch (CloudFrontException e){
                System.err.println(e.awsErrorDetails().errorMessage());
                System.exit(1);
            }
        }
    }
    

    This worked and you can see the new comment:

    enter image description here