AWS CDK: Deploying a product in AWS Service Catalog

AWS CDK Service Catalog product golang

3 min read | by Jordi Prats

AWS Service Catalog allows organizations to create and manage catalogs of IT services that are going to get deployed in AWS using CloudFormation templates. To deploy an instance of a product we can use any IaC tool like Terraform, Pulumi, or the AWS CDK.

To deploy a product using the AWS CDK, we can use the CfnCloudFormationProvisionedProduct construct. This construct allows us to create a new instance of a product in the AWS Service Catalog. In this example, we are going to use Go, but the same can be done using Python, TypeScript, or Java.

First, we are going to create a new project using the AWS CDK CLI:

mkdir cdk-go-service-catalog
cd cdk-go-service-catalog
cdk init app --language go

We'll need to install the dependencies:

go get github.com/aws/aws-cdk-go/awscdk/v2
go get github.com/aws/constructs-go/constructs/v10
go get github.com/aws/jsii-runtime-go

Then we can proceed to update the main.go file, defining the stack and the product we want to deploy:

package main

import (
  "github.com/aws/aws-cdk-go/awscdk/v2"
  "github.com/aws/aws-cdk-go/awscdk/v2/awsservicecatalog"
  "github.com/aws/constructs-go/constructs/v10"
  "github.com/aws/jsii-runtime-go"
)

type CdkStackProps struct {
  awscdk.StackProps
}

func NewCdkStack(scope constructs.Construct, id string, props *CdkStackProps) awscdk.Stack {
  var sprops awscdk.StackProps
  if props != nil {
    sprops = props.StackProps
  }
  stack := awscdk.NewStack(scope, &id, &sprops)

  tags := []*awscdk.CfnTag{
    {Key: jsii.String("an"), Value: jsii.String("example")},
  }

  awsservicecatalog.NewCfnCloudFormationProvisionedProduct(
    stack,
    jsii.String("s3instance"),
    &awsservicecatalog.CfnCloudFormationProvisionedProductProps{
      ProductName:              jsii.String("S3Bucket"),
      ProvisioningArtifactName: jsii.String("1.2.3"),
      ProvisioningParameters: []awsservicecatalog.CfnCloudFormationProvisionedProduct_ProvisioningParameterProperty{
        {
          Key:   jsii.String("BucketName"),
          Value: jsii.String("demos3bucket"),
        },
      },
      Tags: &tags,
    })

  return stack
}

func main() {
  defer jsii.Close()

  app := awscdk.NewApp(nil)

  NewCdkStack(app, "CdkStack", &CdkStackProps{
    awscdk.StackProps{
      Env: env(),
    },
  })

  app.Synth(nil)
}

func env() *awscdk.Environment {
  return nil
}

This is what we are doing in the code:

  • CfnCloudFormationProvisionedProduct: This is the main construct responsible for provisioning a product in AWS Service Catalog. It refers to an already-existing product in Service Catalog.
  • ProductName: This is the name of the Service Catalog product that you want to provision. In this case, we are using an example product called S3BucketProduct.
  • ProvisioningArtifactName: Refers to the specific version of the product that we want to use, in this case, version 1.2.3.
  • ProvisioningParameters: These are the parameters that are passed to the product during provisioning. Each product will accept different parameters, for the example product we are just setting the BucketName to be demos3bucket.
  • Tags: These are the tags that are going to be applied to the product instance.

Once we have the code ready, we'll need to make sure all the dependencies are installed using go mod tidy:

go mod tidy

Finally, we can deploy the stack using the cdk deploy command:

cdk deploy

Posted on 03/09/2024

Categories