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

Add kubernetes-aws-java and container-aws-java templates #880

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions container-aws-java/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: ${PROJECT}
description: ${DESCRIPTION}
runtime: java
template:
description: A Java program to deploy a containerized service on AWS
config:
aws:region:
description: The AWS region to deploy into
default: us-west-2
containerPort:
description: The port to expose on the container
default: 80
cpu:
description: The amount of CPU to allocate for the container
default: 512
memory:
description: The amount of memory to allocate for the container
default: 128
1 change: 1 addition & 0 deletions container-aws-java/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM nginx
97 changes: 97 additions & 0 deletions container-aws-java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<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.pulumi</groupId>
<artifactId>${PROJECT}</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<encoding>UTF-8</encoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
<mainClass>myproject.App</mainClass>
<mainArgs/>
</properties>

<dependencies>
<dependency>
<groupId>com.pulumi</groupId>
<artifactId>pulumi</artifactId>
<version>[1.2,2.0)</version>
</dependency>
<dependency>
<groupId>com.pulumi</groupId>
<artifactId>aws</artifactId>
<version>[6.68,7.0)</version>
</dependency>
<dependency>
<groupId>com.pulumi</groupId>
<artifactId>awsx</artifactId>
<version>[2.21,3.0)</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>${mainClass}</mainClass>
<commandlineArgs>${mainArgs}</commandlineArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-wrapper-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<mavenVersion>3.8.5</mavenVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>
70 changes: 70 additions & 0 deletions container-aws-java/src/main/java/myproject/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package myproject;

import java.util.List;

import com.pulumi.Pulumi;
import com.pulumi.aws.ecs.Cluster;
import com.pulumi.awsx.ecr.Image;
import com.pulumi.awsx.ecr.ImageArgs;
import com.pulumi.awsx.ecr.Repository;
import com.pulumi.awsx.ecr.RepositoryArgs;
import com.pulumi.awsx.ecs.FargateService;
import com.pulumi.awsx.ecs.FargateServiceArgs;
import com.pulumi.awsx.ecs.inputs.FargateServiceTaskDefinitionArgs;
import com.pulumi.awsx.ecs.inputs.TaskDefinitionContainerDefinitionArgs;
import com.pulumi.awsx.ecs.inputs.TaskDefinitionPortMappingArgs;
import com.pulumi.awsx.lb.ApplicationLoadBalancer;

public class App {
public static void main(String[] args) {
Pulumi.run(ctx -> {
var config = ctx.config();
var containerPort = config.getInteger("containerPort").orElse(80);
var cpu = config.getInteger("cpu").orElse(512);
var memory = config.getInteger("memory").orElse(128);

// An ECS cluster to deploy into
var cluster = new Cluster("cluster");

// An ALB to serve the container endpoint to the internet
var loadbalancer = new ApplicationLoadBalancer("loadbalancer");

// An ECR repository to store our application's container image
var repo = new Repository("repo", RepositoryArgs.builder()
.forceDelete(true)
.build());

// Build and publish our application's container image from ./app to the ECR
// repository
var image = new Image("image", ImageArgs.builder()
.repositoryUrl(repo.url())
.context("./app")
.platform("linux/amd64")
.build());

// Deploy an ECS Service on Fargate to host the application container
var service = new FargateService("service", FargateServiceArgs.builder()
.cluster(cluster.arn())
.assignPublicIp(true)
.taskDefinitionArgs(FargateServiceTaskDefinitionArgs.builder()
.container(TaskDefinitionContainerDefinitionArgs.builder()
.name("app")
.image(image.imageUri())
.cpu(cpu)
.memory(memory)
.essential(true)
.portMappings(List.of(
TaskDefinitionPortMappingArgs.builder()
.containerPort(containerPort)
.targetGroup(loadbalancer.defaultTargetGroup())
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this is the output that causes the issue. iirc wrapping this in pulumi.output(…) (or the java equivalent) is a way to work around this. I can give it a try later

Copy link
Contributor

@flostadler flostadler Feb 9, 2025

Choose a reason for hiding this comment

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

This is working for me:

Suggested change
.targetGroup(loadbalancer.defaultTargetGroup())
.targetGroup(Output.of(loadbalancer.defaultTargetGroup()).apply(Function.identity()))

Note, this needs this import: import java.util.function.Function;

Copy link
Member Author

Choose a reason for hiding this comment

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

As Florian mentioned in Slack, the workaround isn't actually working. We need to fix Java to serialize output values: pulumi/pulumi-java#1662. Working on this now.

.build()))
.build())
.build())
.build());

// Export the URL at which the container's HTTP endpoint will be available
var dnsName = loadbalancer.loadBalancer().apply(lb -> lb.dnsName());
ctx.export("url", dnsName.applyValue(d -> "http://" + d));
});
}
}
24 changes: 24 additions & 0 deletions kubernetes-aws-java/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: ${PROJECT}
description: ${DESCRIPTION}
runtime: java
template:
description: A Java program to deploy a Kubernetes cluster on AWS
config:
aws:region:
description: The AWS region to deploy into
default: us-west-2
minClusterSize:
description: Minimum size (number of nodes) of cluster
default: 3
maxClusterSize:
description: Maximum size (number of nodes) of cluster
default: 6
desiredClusterSize:
description: Desired number of nodes in the cluster
default: 3
eksNodeInstanceType:
description: Instance type to use for worker nodes
default: t3.medium
vpcNetworkCidr:
description: Network CIDR to use for new VPC
default: 10.0.0.0/16
97 changes: 97 additions & 0 deletions kubernetes-aws-java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<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.pulumi</groupId>
<artifactId>${PROJECT}</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<encoding>UTF-8</encoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
<mainClass>myproject.App</mainClass>
<mainArgs/>
</properties>

<dependencies>
<dependency>
<groupId>com.pulumi</groupId>
<artifactId>pulumi</artifactId>
<version>[1.2,2.0)</version>
</dependency>
<dependency>
<groupId>com.pulumi</groupId>
<artifactId>awsx</artifactId>
<version>[2.21,3.0)</version>
</dependency>
<dependency>
<groupId>com.pulumi</groupId>
<artifactId>eks</artifactId>
<version>[3.8.1,4.0)</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>${mainClass}</mainClass>
<commandlineArgs>${mainArgs}</commandlineArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-wrapper-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<mavenVersion>3.8.5</mavenVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>
54 changes: 54 additions & 0 deletions kubernetes-aws-java/src/main/java/myproject/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package myproject;

import com.pulumi.Pulumi;
import com.pulumi.awsx.ec2.Vpc;
import com.pulumi.awsx.ec2.VpcArgs;
import com.pulumi.eks.Cluster;
import com.pulumi.eks.ClusterArgs;
import com.pulumi.eks.enums.AuthenticationMode;

public class App {
public static void main(String[] args) {
Pulumi.run(ctx -> {
// Grab some values from the Pulumi configuration (or use default values)
var config = ctx.config();
var minClusterSize = config.getInteger("minClusterSize").orElse(3);
var maxClusterSize = config.getInteger("maxClusterSize").orElse(6);
var desiredClusterSize = config.getInteger("desiredClusterSize").orElse(3);
var eksNodeInstanceType = config.get("eksNodeInstanceType").orElse("t3.medium");
var vpcNetworkCidr = config.get("vpcNetworkCidr").orElse("10.0.0.0/16");

// Create a VPC for the EKS cluster
var eksVpc = new Vpc("eks-vpc", VpcArgs.builder()
.enableDnsHostnames(true)
.cidrBlock(vpcNetworkCidr)
.build());

// Create the EKS cluster
var eksCluster = new Cluster("eks-cluster", ClusterArgs.builder()
// Put the cluster in the new VPC created earlier
.vpcId(eksVpc.vpcId())
// Use the "API" authentication mode to support access entries
.authenticationMode(AuthenticationMode.Api)
// Public subnets will be used for load balancers
.publicSubnetIds(eksVpc.publicSubnetIds())
// Private subnets will be used for cluster nodes
.privateSubnetIds(eksVpc.privateSubnetIds())
// Change configuration values to change any of the following settings
.instanceType(eksNodeInstanceType)
.desiredCapacity(desiredClusterSize)
.minSize(minClusterSize)
.maxSize(maxClusterSize)
// Do not give the worker nodes public IP addresses
.nodeAssociatePublicIpAddress(false)
// Change these values for a private cluster (VPN access required)
.endpointPrivateAccess(false)
.endpointPublicAccess(true)
.build());

// Export some values for use elsewhere
ctx.export("kubeconfig", eksCluster.kubeconfig());
ctx.export("vpcId", eksVpc.vpcId());
});
}
}
Loading