4.1 Creating a New Project

Step 1 — Open the Project

Go to the folder that contains the deploying_applications_to_eks lab.

cd eks-demo-apps

The code you have in that project should look like this:

import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

const pulumiConfig = new pulumi.Config();

// Existing Pulumi stack reference in the format:
// <organization>/<project>/<stack> e.g. "myUser/myProject/dev"
const clusterStackRef = new pulumi.StackReference(pulumiConfig.require("clusterStackRef"));

// Get the kubeconfig from the cluster stack output.
const kubeconfig = clusterStackRef.getOutput("kubeconfig");

// Create the k8s provider with the kubeconfig.
const provider = new k8s.Provider("k8sProvider", { kubeconfig });

const ns = new k8s.core.v1.Namespace("eks-demo-apps-ns", {
    metadata: { name: "eks-demo-apps" },
}, { provider });

const appLabels = { app: "eks-demo-apps" };
const deployment = new k8s.apps.v1.Deployment("eks-demo-apps-dep", {
    metadata: { namespace: ns.metadata.name },
    spec: {
        selector: { matchLabels: appLabels },
        replicas: 3,
        template: {
            metadata: { labels: appLabels },
            spec: {
                containers: [{
                    name: "eks-demo-apps",
                    image: "jocatalin/kubernetes-bootcamp:v2",
                }],
            },
        },
    },
}, { provider });

const service = new k8s.core.v1.Service("eks-demo-apps-svc", {
    metadata: { namespace: ns.metadata.name },
    spec: {
        selector: appLabels,
        ports: [{ port: 80, targetPort: 8080 }],
        type: "LoadBalancer",
    },
}, { provider });

const address = service.status.loadBalancer.ingress[0].hostname;
const port = service.spec.ports[0].port;
export const url = pulumi.interpolate`http://${address}:${port}`;

Note that there is quite a bit of boilerplate here. In fact, there are really only 4 things that are being customized for this particular workload:

  • The image to use
  • The number of replicas
  • The port mappings
  • The namespace to create the Service and Deployment inside

In this lab, we’ll create a component that only requires specifying these four things, abstracting away all the boilerplate and complexity behind a simple interface for this common pattern.