Main.java
package com.example;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.route53.Route53Client;
import software.amazon.awssdk.services.route53.model.Change;
import software.amazon.awssdk.services.route53.model.ChangeAction;
import software.amazon.awssdk.services.route53.model.ChangeBatch;
import software.amazon.awssdk.services.route53.model.ChangeResourceRecordSetsRequest;
import software.amazon.awssdk.services.route53.model.ChangeResourceRecordSetsResponse;
import software.amazon.awssdk.services.route53.model.RRType;
import software.amazon.awssdk.services.route53.model.ResourceRecord;
import software.amazon.awssdk.services.route53.model.ResourceRecordSet;
public class Main {
public static void main(String[] args) throws IOException {
Region region = Region.AWS_GLOBAL;
Route53Client route53Client = Route53Client.builder()
.region(region)
.build();
upsertArecord("XXXXXXXXXXXXXXXXXXXX", "18.177.121.151", "test.example.com", route53Client);
}
public static void upsertArecord(String hostedZoneId, String ipAddress, String host, Route53Client route53Client) {
ResourceRecord resourceRecord = ResourceRecord.builder().value(ipAddress).build();
List<ResourceRecord> resourceRecords = new ArrayList<ResourceRecord>();
resourceRecords.add(resourceRecord);
// https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/route53/model/ResourceRecordSet.html
// weightを入れるとSimpleタイプにならないので、weightは入れない。
// ホスト名は、testではなく、test.example.com のような文字列にする必要がある。
ResourceRecordSet resourceRecordSet = ResourceRecordSet.builder()
.name(host)
.type(RRType.A)
.ttl(new Long(300))
.resourceRecords(resourceRecords)
.build();
// https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/route53/model/Change.html
// UPSERTにすることで、新規も更新もできる。
Change change = Change.builder()
.action(ChangeAction.UPSERT)
.resourceRecordSet(resourceRecordSet).build();
List<Change> changes = new ArrayList<Change>();
changes.add(change);
ChangeBatch changeBatch = ChangeBatch.builder()
.changes(changes)
.build();
// https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/route53/model/ChangeResourceRecordSetsRequest.html
ChangeResourceRecordSetsRequest request = ChangeResourceRecordSetsRequest.builder()
.hostedZoneId(hostedZoneId)
.changeBatch(changeBatch)
.build();
// 例外がthrowされなければ、要求の送信は完了(戻り値の状態値はPENDINGとなるが、それでOK)
ChangeResourceRecordSetsResponse response = route53Client.changeResourceRecordSets(request);
// Print the result
// response.toString();
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>aws-sdk-java-route53</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- https://github.com/aws/aws-sdk-java-v2/#using-the-sdk -->
<!-- https://search.maven.org/search?q=g:software.amazon.awssdk%20AND%20a:bom -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.15.32</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- https://docs.aws.amazon.com/code-samples/latest/catalog/javav2-route53-pom.xml.html -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>route53</artifactId>
</dependency>
<!-- slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<!-- http://www.slf4j.org/manual.html -->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
</dependency>
<!-- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies>
<build>
<finalName>my-route53</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>