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:
S3BucketProduct.1.2.3.BucketName to be demos3bucket.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