Polish Pipeline

Get Endpoints

Stepping back, we can see a problem now that our app is being deployed by our pipeline. There is no easy way to find the endpoints of our application (the TableViewer and APIGateway endpoints), so we can’t call it! Let’s add a little bit of code to expose these more obviously.

First edit CdkWorkshop/CdkWorkshopStack.cs to get these values and expose them as properties of our stack:

using Amazon.CDK;
using Amazon.CDK.AWS.APIGateway;
using Amazon.CDK.AWS.Lambda;
using Eladb.DynamoTableViewer;

namespace CdkWorkshop
{
    public class CdkWorkshopStack : Stack
    {
        public readonly CfnOutput HCViewerUrl;
        public readonly CfnOutput HCEndpoint;

        // Defines a new lambda resource
        public CdkWorkshopStack(Construct parent, string id, IStackProps props = null) : base(parent, id, props)
        {
            var hello = new Function(this, "HelloHandler", new FunctionProps
            {
                Runtime = Runtime.NODEJS_10_X,
                Code = Code.FromAsset("lambda"),
                Handler = "hello.handler"
            });

            var helloWithCounter = new HitCounter(this, "HelloHitCounter", new HitCounterProps
            {
                Downstream = hello
            });

            var gateway = new LambdaRestApi(this, "Endpoint", new LambdaRestApiProps
            {
                Handler = helloWithCounter.Handler
            });

            var tv = new TableViewer(this, "ViewerHitCount", new TableViewerProps
            {
                Title = "Hello Hits",
                Table = helloWithCounter.MyTable
            });

            this.HCViewerUrl = new CfnOutput(this, "TableViewerUrl", new CfnOutputProps
            {
                Value = gateway.Url
            });

            this.HCEndpoint = new CfnOutput(this, "GatewayUrl", new CfnOutputProps
            {
                Value = gateway.Url
            });
        }
    }
}

By adding outputs HCViewerUrl and HCEnpoint, we expose the necessary endpoints to our HitCounter application. We are using the core construct CfnOutput to declare these as Cloudformation stack outputs (we will get to this in a minute).

Let’s commit these changes to our repo (git commit -am "MESSAGE" && git push), and navigate to the Cloudformation console. You can see there are three stacks.

  • CDKToolkit: The first is the integrated CDK stack (you should always see this on bootstrapped accounts). You can ignore this.
  • WorkshopPipelineStack: This is the stack that declares our pipeline. It isn’t the one we need right now.
  • Deploy-WebService: Here is our application! Select this, and under details, select the Outputs tab. Here you should see four endpoints (two pairs of duplicate values). Two of them, EndpointXXXXXX and ViewerHitCounterViewerEndpointXXXXXXX, are defaluts generated by Cloudformation, and the other two are the outputs we declared ourselves.

If you click the TableViewerUrl value, you should see our pretty hitcounter table that we created in the initial workshop.

Add Validation Test

Now we have our application deplyed, but no CD pipeline is complete without tests!

Let’s start with a simple test to ping our endpoints to see if they are alive. Return to CdkWorkshop/PipelineStack.cs and add the following:

using Amazon.CDK;
using Amazon.CDK.AWS.CodeCommit;
using Amazon.CDK.AWS.CodePipeline;
using Amazon.CDK.AWS.CodePipeline.Actions;
using Amazon.CDK.Pipelines;
using System.Collections.Generic;

namespace CdkWorkshop
{
    public class WorkshopPipelineStack : Stack
    {
        public WorkshopPipelineStack(Construct parent, string id, IStackProps props = null) : base(parent, id, props)
        {
            // PIPELINE CODE HERE...

            var deploy = new WorkshopPipelineStage(this, "Deploy");
            var deployStage = pipeline.AddApplicationStage(deploy);
            deployStage.AddActions(new ShellScriptAction(new ShellScriptActionProps
            {
                ActionName = "TestViewerEndpoint",
                UseOutputs = new Dictionary<string, StackOutput> {
                    { "ENDPOINT_URL", /* TBD */ }
                },
                Commands = new string[] {"curl -Ssf $ENDPOINT_URL"}
            }));
            deployStage.AddActions(new ShellScriptAction(new ShellScriptActionProps
            {
                ActionName = "TestAPIGatewayEndpoint",
                UseOutputs = new Dictionary<string, StackOutput> {
                    { "ENDPOINT_URL", /* TBD */ }
                },
                Commands = new string[] {
                    "curl -Ssf $ENDPOINT_URL/",
                    "curl -Ssf $ENDPOINT_URL/hello",
                    "curl -Ssf $ENDPOINT_URL/test"
                }
            }));
        }
    }
}

First we import ShellScriptAction from CDK Pipelines. This is a construct that simply executes one or more shell script commands. Then we add two actions to our deployment stage that test our TableViewer endpoint and our APIGateway endpoint respectively.

Note: We submit several curl requests to the APIGateway endpoint so that when we look at our tableviewer, there are several values already populated.

You may notice that we have not yet set the URLs of these endpoints. This is because they are not yet exposed to this stack!

With a slight modification to CdkWorkshop/PipelineStage.cs we can expose them:

using Amazon.CDK;
using Amazon.CDK.Pipelines;

namespace CdkWorkshop
{
    public class WorkshopPipelineStage : Stage
    {
        public readonly CfnOutput HCViewerUrl;
        public readonly CfnOutput HCEndpoint;

        public WorkshopPipelineStage(Construct scope, string id, StageProps props = null)
            : base(scope, id, props)
        {
            var service = new CdkWorkshopStack(this, "WebService");

            this.HCEndpoint = service.HCEndpoint;
            this.HCViewerUrl = service.HCViewerUrl;
        }
    }
}

Now we can add those values to our actions in CdkWorkshop/PipelineStack.cs by getting the StackOutput of our pipeline stack:

// CODE HERE...

deployStage.AddActions(new ShellScriptAction(new ShellScriptActionProps
{
    ActionName = "TestViewerEndpoint",
    UseOutputs = new Dictionary<string, StackOutput> {
        { "ENDPOINT_URL", pipeline.StackOutput(deploy.HCViewerUrl) }
    },
    Commands = new string[] {"curl -Ssf $ENDPOINT_URL"}
}));
deployStage.AddActions(new ShellScriptAction(new ShellScriptActionProps
{
    ActionName = "TestAPIGatewayEndpoint",
    UseOutputs = new Dictionary<string, StackOutput> {
        { "ENDPOINT_URL", pipeline.StackOutput(deploy.HCEndpoint) }
    },
    Commands = new string[] {
        "curl -Ssf $ENDPOINT_URL/",
        "curl -Ssf $ENDPOINT_URL/hello",
        "curl -Ssf $ENDPOINT_URL/test"
    }
}));

Commit and View!

Commit those changes, wait for the pipeline to re-deploy the app, and navigate back to the CodePipeline Console and you can now see that there are two test actions contained within the Deploy stage!

Conratulations! You have successfully created a CD pipeline for your application complete with tests and all! Feel free to explore the console to see the details of the stack created, or check out the API Reference section on CDK Pipelines and build one for your application.