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 aws eks hello world example #399

Merged
merged 7 commits into from
Apr 2, 2024
Merged
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
105 changes: 69 additions & 36 deletions examples/aws-eks/Main.scala
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import besom.*
import besom.api.aws
import besom.api.eks
import besom.api.kubernetes as k8s
import besom.api.{awsx, eks, kubernetes as k8s}

@main def main = Pulumi.run {
// Get the default VPC and select the default subnet
val vpc = aws.ec2.getVpc(aws.ec2.GetVpcArgs(default = true))
val subnet = vpc.flatMap(vpc =>
aws.ec2.getSubnet(
aws.ec2.GetSubnetArgs(
vpcId = vpc.id,
defaultForAz = true
)
)
)
val appName = "hello-world"
val appLabels = Map("appClass" -> appName)
val appPort = 80

// Get the default VPC
val vpc = awsx.ec2.Vpc("my-vpc", awsx.ec2.VpcArgs(cidrBlock = "10.0.0.0/16"))

// Create an EKS cluster using the default VPC and subnet
val cluster = eks.Cluster(
"my-cluster",
appName,
eks.ClusterArgs(
vpcId = vpc.id,
subnetIds = List(subnet.id),
vpcId = vpc.vpcId,
subnetIds = vpc.publicSubnetIds,
instanceType = "t2.medium",
desiredCapacity = 2,
minSize = 1,
Expand All @@ -29,32 +23,71 @@ import besom.api.kubernetes as k8s
)
)

val k8sProvider = k8s.Provider(
"k8s-provider",
k8s.ProviderArgs(
kubeconfig = cluster.kubeconfigJson
)
val defaultProvider = cluster.core.provider

// Create a kubernetes namespace
val namespace = k8s.core.v1.Namespace(
name = appName,
opts = opts(provider = defaultProvider)
)
val namespaceName = namespace.metadata.name

// Create default metadata
val defaultMetadata = k8s.meta.v1.inputs.ObjectMetaArgs(
namespace = namespaceName,
labels = appLabels
)

val pod = k8s.core.v1.Pod(
"mypod",
k8s.core.v1.PodArgs(
spec = k8s.core.v1.inputs.PodSpecArgs(
containers = List(
k8s.core.v1.inputs.ContainerArgs(
name = "echo",
image = "k8s.gcr.io/echoserver:1.4"
// Define the "Hello World" deployment.
val deployment = k8s.apps.v1.Deployment(
name = appName,
k8s.apps.v1.DeploymentArgs(
metadata = defaultMetadata,
spec = k8s.apps.v1.inputs.DeploymentSpecArgs(
selector = k8s.meta.v1.inputs.LabelSelectorArgs(matchLabels = appLabels),
replicas = 1,
template = k8s.core.v1.inputs.PodTemplateSpecArgs(
metadata = k8s.meta.v1.inputs.ObjectMetaArgs(labels = appLabels),
spec = k8s.core.v1.inputs.PodSpecArgs(
containers = k8s.core.v1.inputs.ContainerArgs(
name = appName,
image = "nginx:latest",
ports = List(
k8s.core.v1.inputs.ContainerPortArgs(containerPort = 80)
)
) :: Nil
)
)
)
),
opts = opts(
provider = k8sProvider,
dependsOn = cluster,
deletedWith = cluster // skip deletion to save time, since it will be deleted with the cluster
)
opts = opts(provider = defaultProvider)
)

// Define the "Hello World" service.
val service = k8s.core.v1.Service(
name = appName,
k8s.core.v1.ServiceArgs(
spec = k8s.core.v1.inputs.ServiceSpecArgs(
selector = appLabels,
`type` = k8s.core.v1.enums.ServiceSpecType.LoadBalancer,
ports = List(
k8s.core.v1.inputs.ServicePortArgs(
port = appPort,
targetPort = 80
)
)
),
metadata = defaultMetadata
),
opts = opts(provider = defaultProvider, dependsOn = deployment)
)

// Export the cluster's kubeconfig
Stack(cluster, pod).exports(kubeconfig = cluster.kubeconfig)
val serviceHostname =
service.status.loadBalancer.ingress.map(_.flatMap(_.head.hostname).get)

// Export the cluster's kubeconfig and url
Stack(cluster, service).exports(
kubeconfig = cluster.kubeconfig,
url = p"http://$serviceHostname:$appPort"
)
}
18 changes: 13 additions & 5 deletions examples/aws-eks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ These values are indicated with `***`.
pulumi config set aws:region us-west-2
```

We recommend using `us-west-2` to host your EKS cluster as other regions (notably `us-east-1`) may have capacity issues that prevent EKS
We recommend using `us-west-2` to host your EKS cluster as other regions (notably `us-east-1`) may have capacity
issues that prevent EKS
clusters from creating.

We are tracking enabling the creation of VPCs limited to specific AZs to unblock this in `us-east-1`: pulumi/pulumi-awsx#32

We are tracking enabling the creation of VPCs limited to specific AZs to unblock this in `us-east-1`:
pulumi/pulumi-awsx#32

3. Stand up the EKS cluster:

Expand All @@ -41,13 +43,19 @@ These values are indicated with `***`.
pulumi stack output kubeconfig --show-secrets > kubeconfig.json
```

Once you have this file in hand, you can interact with your new cluster as usual via `kubectl`:
Once you have this file in hand, you can interact with your new cluster as usual via `kubectl`:

```bash
kubectl --kubeconfig=./kubeconfig.json get pods --all-namespaces
```

5. To clean up resources, destroy your stack and remove it:
5. And finally - open the application in your browser to see the running application.

```bash
curl $(pulumi stack output url)
```

6. To clean up resources, destroy your stack and remove it:

```bash
pulumi destroy
Expand Down
2 changes: 2 additions & 0 deletions examples/aws-eks/project.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//> using scala "3.3.1"
//> using options -Werror -Wunused:all -Wvalue-discard -Wnonunit-statement
//> using plugin "org.virtuslab::besom-compiler-plugin:0.2.3-SNAPSHOT"

//> using dep "org.virtuslab::besom-core:0.2.3-SNAPSHOT"
//> using dep "org.virtuslab::besom-awsx:2.5.0-core.0.2"
//> using dep "org.virtuslab::besom-eks:2.2.1-core.0.2"

//> using repository sonatype:snapshots
Loading