Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix aws-java-eks-minimal example #1281

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.aws.ec2.Ec2Functions;
import com.pulumi.aws.ec2.inputs.GetSubnetIdsArgs;
import com.pulumi.aws.ec2.inputs.GetSubnetsArgs;
import com.pulumi.aws.ec2.inputs.GetSubnetsFilterArgs;
import com.pulumi.aws.ec2.inputs.GetVpcArgs;
import com.pulumi.aws.ec2.outputs.GetVpcResult;
import com.pulumi.core.Output;
import com.pulumi.eks.Cluster;
import com.pulumi.eks.ClusterArgs;

import java.util.List;
import java.util.stream.Collectors;

public class App {
Expand All @@ -18,32 +19,61 @@ public static void main(String[] args) {
}

private static void stack(Context ctx) {
var vpcIdOutput = Ec2Functions.getVpc(

// Find the default VPC identifier.
final var defaultVpcId = Ec2Functions.getVpc(
GetVpcArgs.builder().default_(true).build()
).applyValue(GetVpcResult::id);
ctx.export("vpcIdOutput", vpcIdOutput);

var subnetIdsOutput = vpcIdOutput
.apply(vpcId -> Ec2Functions.getSubnetIds(GetSubnetIdsArgs.builder()
.vpcId(vpcId)
.build()))
.applyValue(getSubnetIdsResult ->
getSubnetIdsResult.ids()
.stream()
.sorted()
.limit(2)
.collect(Collectors.toList()));

ctx.export("subnetIdsOutput", subnetIdsOutput.applyValue(vs -> String.join(",", vs)));

var cluster = new Cluster("my-cluster", ClusterArgs.builder()
.vpcId(vpcIdOutput)
.subnetIds(subnetIdsOutput)

// Export it for visibility via `pulumi stack output`.
ctx.export("defaultVpcId", defaultVpcId);

// Find all the subnets in the default VPC.
final var allSubnetIds = Ec2Functions.getSubnets(GetSubnetsArgs.builder()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should provision the subnets instead of finding them in the default VPC, to make the example more self-contained.

.filters(GetSubnetsFilterArgs.builder()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getSubnetIds returned deprecation warnings. https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets is recommended to be used instead. Its not obvious from the types but this is how it's to be used.

.name("vpc-id")
.values(defaultVpcId.applyValue(List::of))
.build())
.build())
.applyValue(result -> result.ids()
.stream()
.sorted()
.limit(2)
.collect(Collectors.toList()));

// Fail if there are not enough subnets available (the example requires at least 2).
ctx.export("totalSubnetCount", allSubnetIds.applyValue(ids -> {
final var n = ids.size();
if (n < 2) {
throw new RuntimeException(String.format(
"This example requires at least 2 subnets in the default VPC, but found only %s", n));
}
return n;
}));

// Select 2 subnets only to speed up the example.
final var selectedSubnetIds = allSubnetIds
.applyValue(ids -> ids.stream().limit(2).collect(Collectors.toList()));

// Export it for visibility via `pulumi stack output`.
ctx.export("selectedSubnetIds", selectedSubnetIds.applyValue(ids -> String.join(",", ids)));

// Provision an EKS cluster in the given VPC and selected subnets.
final var cluster = new Cluster("my-cluster", ClusterArgs.builder()
.vpcId(defaultVpcId)
.subnetIds(selectedSubnetIds)
.instanceType("t2.micro")
.minSize(1)
.maxSize(2)
.build());

// Export `kubeconfig` to enable interacting with the cluster from the command line:
//
// $ pulumi stack output kubeconfig --show-secrets > kubeconfig
// $ export KUBECONFIG=$PWD/kubeconfig
// $ kubectl version
// $ kubectl cluster-info
// $ kubectl get nodes
ctx.export("kubeconfig", cluster.kubeconfig());
}
}