AWS CDK Python

,

Is it me or the hole AWS CDK python documentation is on typescript, even on the Pypi the samples seems to be from typescript

  const secret = SecretValue.secretsManager('secretId', {
  jsonField: 'password' // optional: key of a JSON field to retrieve (defaults to all content),
  versionId: 'id'       // optional: id of the version (default AWSCURRENT)
  versionStage: 'stage' // optional: version stage name (default AWSCURRENT)
});

Related discussions:

Not just that, even the cdk workshop which has step by step tutorials, had some Python examples mistakenly mention Typescript the last time I checked.

It’s only just GA, yeh there are big holes in the documentation, makes it interesting to work with ;p

Yeah, it’s irritating, I’m assuming it will change in the future. In the meantime check out the CDK python examples.. I’ve been writing a CDK app in Python these past couple days and the way I was able to start making progress was by cross-referencing these examples against the typescript docs, which in turn gave me a pretty solid idea of how typescript examples can be converted to Python.

Thx for the reference.

I’m curious, why CDK vs Troposphere or even Terraform?

In previous announcements, they have acknowledged that the docs & examples for Python are still rough. They’re actively looking for PR’s to improve that, https://github.com/aws/aws-cdk

I’m in a meeting with the CDK team right now and made them aware of this thread. The plan is to convert all of the TypeScript to Python to make very clear that Python is a first-class CDK citizen.

You should also read AWS CDK in Other Languages to learn how to translate from TypeScript to other languages.

Neat, thanks for the Feedback.

CDK is written in TypeScript.

The Python package (and other languages supported by CDK) are just thin shims around the TypeScript library. AWS has a tool called jsii that “allows code in any language to naturally interact with JavaScript classes”.

The Python package is just auto-generated Classes with jsii metaclass info. These classes act as proxy classes and the Ptyhon calls are marshaled in and out to a Node.js VM.

The jsii Python package has a jsii._kernel.providers.process module which spins up a Node JS process and shuttles data between Python and Node so that the “real” work is done with JavaScript.

The Python and Ruby are still listed as a “Work in Progress” on the JSII page … it’s a fair bit of magic that’s going on there. Obviously some performance hits, although IaC isn’t really a place where that kind of hit would matter. BUt it might take some time for them to get the jsii really robust though.

Is the plan to convert all the TypeScript docs to Python and continue using JSII under the hood, or is the plan to remove JSII completely and have full native libraries for all of the supported languages?

There are layers of abstraction in IaC projects:

- lowest level: create and manage resources only using API calls. Generally no one is really doing this anymore, although with tools like Ansible people have gone pretty far down this road without using any higher level abstractions …

- CloudFormation: create resources in CloudFormation templates and have state and dependencies managed for you. Or alternatively, Terraform can manage it’s own state and provide a similar level of abstraction. Troposphere fits in here, as it’s just a tool to let you write CloudFormation in Python.

- Terraform - or Stacker/Sceptre: tooling to handle configuration for passing parameters to stacks in different environments, to create/update the stacks in your AWS accounts. This is “terraform plan” or “stacker build” which can let you know things like what resources an update might touch.

- CDK is “a high-level object-oriented abstraction to define AWS resources imperatively using the power of modern programming languages. Using the CDK’s library of infrastructure constructs, you can easily encapsulate AWS best practices in your infrastructure definition and share it without worrying about boilerplate logic.”. CDK is essentially getting you one level of abstraction above declaring everything directly in CloudFormation templates. Terraform has a similar abstraction with Terraform modules.

As CDK is controlled entirely from a programming language, it’s up to you to write some tooling/commands to allow you to orchestrate provisioning and handle your per-environment configuration. It is roughly similar to what stacker_blueprints provides for Stacker, a way to abstract and re-use common CloudFormation template generation patterns.

If you are using CDK, you can do quite a bit fairly quickly. However, you would still need to layer on additional tooling if you were using it for large production environments. For example, Terragrunt and Terratest are tools for Terraform that allow you to do functional testing and DRY configuration. You’d need to create your own analogues for those in CDK (caveat, I see CDK has some built-in test tooling … I haven’t looked at it yet though). In CloudFormation land, people are using Runway to also manage multi-region, multi-account deployments, so you’d need to write some wrapper around your CDK to allow it to work with such a tool.

Finally tools likeCDK, Punchcard and AIM are attempting to take IaC projects to a layer of abstraction above what exists with any of the current tooling today. CDK has the concept of high-level Constructs, so that if you have a Lambda subscribe to an SNS Topic, CDK will auto generate an IAM Policy to allow the topic to invoke the Lambda.

Punchcard by allow you to both define a Lambda and then directly have the Lambda code in the function that defined the Lambda. Essentially letting you freely mix IaC code freely with App code(!).

AIM (which is a tool I’m contributing to) attempts to abstract as much a way as possible. It lets you describe everything in declarative YAML, then feeds that YAML describing your infrastructure and applications into a CDK-like engine, which can do similar time saving features such as IAM policy auto-generation, but also adds on other time saving smart features, such as if you declare you want an ASG to have swap metrics, it will not only auto-generate an IAM Policy to allow the instance to collect metrics, but also configure and install the CloudWatch Agent on those instances.

Straight from my colleague:

The plan is to convert all the TypeScript docs to Python and continue using JSII under the hood. A big part of the value the CDK provides is the high-level abstractions contained in the AWS Construct Library, it’s a key differentiator from solutions like Troposphere, which is still very low level. The JSII allows us to accelerate the rate at which we provide this value to customers who use different programming languages. We can author new constructs in TypeScript and then vend them to all the languages supported by JSII, automatically, rather than hand coding the same construct 4 times for 4 languages. We are sensitive about making customers wait for new features in the CDK and JSII allows us to shorten this wait as much as possible.

While I wasn’t privy to the decision-making process, we had; good experience with CloudFormation via Ansible, TypeScript devs, a direct line to the CDK guys in Amazon and a project that would be completing around when it went GA. I also suspect it was the latest shiny thing the Data Service Head had been playing with. ;p

I was pretty painful as we started dev when it was 0.14.0 so we had to deal with a lot of breaking changes during version bumps and reimplementing CFN level code as they have been brought into CDK.

Definitely worth it now as we have a pretty complex infrastructure implemented across multiple environments and can move away from our legacy data centre setup!

Has anyone seen documentation on how to create constructs in the underlying js and auto-build the higher level language interfaces for jsii? I’m interested in creating custom constructs and then building/ publishing them to all languages so that consumers can use cdk language of choice.