Cx as Code with TypeScript and CDK

Hi all,

I'm trying to use CX as Code with CDK for Terraform and typescript but it is not generating the output with the resources.

I've tested with other providers (aws) and I can generate the resources without any issue, also I have generated the resources for Genesys when not using CDK.

Anyone is using the same setup and is achiving a successful execution?

Does anyone know if there is any limitation with the genesys provider?

Thank you

Hi Miquel,

Could you be more specific? Are you having problems executing CX as Code with the CDK. I know when it first came out we were trying it out and had no problems with it. Are you getting any kind of errors that might help provide guidance?

Thanks,
John Carnell
Manager, Developer Engagement

Hi John,

What I've tested is the following code:

// Copyright (c) HashiCorp, Inc
// SPDX-License-Identifier: MPL-2.0
import { Construct } from "constructs";
import { App, TerraformStack } from "cdktf";
import { TfExport} from "./.gen/providers/genesyscloud/tf-export";
import { GenesyscloudProvider} from "./.gen/providers/genesyscloud/provider";

class MyStack extends TerraformStack {
  constructor(scope: Construct, id: string) {
    super(scope, id);
    // define resources here
    new GenesyscloudProvider(scope,'Provider',{
      awsRegion: 'XXXXX',
      oauthclientId: 'XXXXXXX',
      oauthclientSecret: 'XXXXXXX'
    });
    new TfExport(scope,'Export',{
      exportAsHcl: true,
      includeStateFile: true,
      directory: './fullExport'
    });
  }
}

const app = new App();
new MyStack(app, "genesys-cx-as-code");
app.synth();

What I get is the following terraform.tfstate file:

{
    "version": 3,
    "serial": 1,
    "lineage": "9a206d32-095c-438d-86e3-607791f8e167",
    "backend": {
        "type": "local",
        "config": {
            "path": "/XXXXXX/terraform.genesys-cx-as-code.tfstate",
            "workspace_dir": null
        },
        "hash": 1721479173
    },
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {},
            "resources": {},
            "depends_on": []
        }
    ]
}

If instead of using CDK I execute the following main.tf:

terraform {
  required_providers {
    genesyscloud = {
      source  = "mypurecloud/genesyscloud"
      version = "~> 1.0.0"
    }
  }



}
resource "genesyscloud_tf_export" "export" {
  directory          = "./genesyscloud"
  include_state_file = true
}
provider "genesyscloud" {
      oauthclient_id = "XXXXXXXX"
      oauthclient_secret = "XXXXXXXXX"
      aws_region = "XXXXXX"
}

Then what I get is this tfstate file:

{
  "version": 4,
  "terraform_version": "1.3.7",
  "serial": 4,
  "lineage": "cfc976a9-fda0-24f9-c081-a08ab8386421",
  "outputs": {},
  "resources": [
    {
      "mode": "managed",
      "type": "genesyscloud_tf_export",
      "name": "export",
      "provider": "provider[\"registry.terraform.io/mypurecloud/genesyscloud\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "directory": "./genesyscloud",
            "exclude_attributes": null,
            "id": "genesyscloud/genesyscloud.tf.json",
            "include_state_file": true,
            "resource_types": null
          },
          "sensitive_attributes": [],
          "private": "bnVsbA=="
        }
      ]
    }
  ],
  "check_results": null
}

So this time resources are correctly produced.

I get no error on console or terminal, it just produce the files so I have no clue why when generating using the cdk resources are not produced.

Any clue?

Hi Miguel,

Thanks for posting this. I am going to play around with this either over the weekend or on Tuesday. Our Genesys Cloud offices are closed in the US for the Martin Luther King holiday.

We have only played around with the CDK briefly and I used it to create some queues. I have never tried the export functionality with it, so it will be interesting to see what I can do with it.

Just out of curiosity were any actual resources exported in the non-TF state module.

Thanks for posting and I hope to have something for you next week.

Thanks,
John Carnell
Manager. Developer Engagement

Hi John,

Probably I'm doing something wrong but I cannot generate anything, even a queue. Can you share a basic project that just create a queue so I can test on my end?

This are the versions I'm using:

Node: 16.13.0
cdktf: 0.14.3
go: 1.19.5
terraform: 1.3.7
genesys cloud provider: 1.10.0
O.S.: MacOS 13.1

Thank you

Hi Miguel,

Thanks for providing the information. So here is what I have been able to do today.

  1. I was able to re-install my environment. So I am running with Node v18.0.0.
  2. I was able to install the CDKTF (latest version) running Terraform v1.3.0.
  3. Created a Typescript script that would create a queue and perform an export of all queues:
  constructor(scope: Construct, id: string) {
    super(scope, id);

    // define resources here
    new GenesyscloudProvider(this,"genesyscloudprovider", {})

    new TfExport(this, "myexport", {
      directory: "./genesyscloud/export",
      resourceTypes: ["genesyscloud_routing_queue"],
      includeStateFile: true,
      exportAsHcl: true
    })

    new RoutingQueue(this, "mySimpleCDKQueue",{
      name: "MySimpleCDKQueue", 
      description: "Example Queue built by CDK"
    }) 
  }
}



const app = new App();

new MyStack(app, "cdktf");
app.synth();

I ran cdktf deploy and everything worked as expected. So a couple of things:

  1. Make sure you do a cdktf deploy and not just a cdktf synth. I know that sounds really basic but I forgot to do that multiple times. :slight_smile:
  2. Remember you are doing an export as a relative path your export files are going to be written out to the cdktf.out\stacks\fullExport directory.
  3. The TFExport object is a resource of the CX as Code provider. This means that when you run your typescript application it will generate a tfstate file that maintains the state for your run. The actual exported resources and a state file will be written to your export directory.

I hope this helps. I am going to record a DevDrop that shows how to set up and use the Terraform CDK with CX as Code and Typescript. Keep an eye on the videos section of the Developer Center and it should be there sometime this week.

Thanks,
John Carnell
Manager, Developer Engagement

Thank you John!!

Can you share your output file when you execute cdktf synth? can you also upload this dummy project so I can execute same code on my workstation? I still don't see what I'm doing wrong but no resource is generated on tfstate file when executing the cdksynth.

Hi Miguel,

I threw the project out here in my personal account since I do not plan to turn this into a formal blueprint. I hope this helps.

If you blow away the state files and then set the Genesys client credentials via environment variables you should be able to run this exact app. Also you can turn up the TF_LOG=debug and set the SDK flag on the provider to get logs from the SDK calls and the TF_LOG.

Thanks,
John Carnell
Manager, Developer Engagement

Thank you very much John :star_struck:!!!

I will download the project, test on my laptop and let you know.

Hi John,

Finally I've found out the error, when creating the resources I was passing "scope" as parameter but I have to pass "this":

wrong -> new TfExport(scope, "myexport", {

correct -> new TfExport(this, "myexport", {

Thank you very much for your help :smiling_face_with_three_hearts:!!!

Hi Miquel,

No worries. Glad you found it and I have been there debugging these issues. They can be slog.

Thanks,
John Carnell
Manager, Developer Engagement

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.