{"id":21193792,"url":"https://github.com/danielmschmidt/cdktf-aws-networking-demo","last_synced_at":"2025-03-14T21:18:05.758Z","repository":{"id":46734928,"uuid":"506644131","full_name":"DanielMSchmidt/cdktf-aws-networking-demo","owner":"DanielMSchmidt","description":null,"archived":false,"fork":false,"pushed_at":"2022-07-19T13:52:00.000Z","size":99,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-01-21T13:49:29.456Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DanielMSchmidt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-23T13:06:52.000Z","updated_at":"2022-06-23T13:07:01.000Z","dependencies_parsed_at":"2022-09-22T23:01:48.258Z","dependency_job_id":null,"html_url":"https://github.com/DanielMSchmidt/cdktf-aws-networking-demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielMSchmidt%2Fcdktf-aws-networking-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielMSchmidt%2Fcdktf-aws-networking-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielMSchmidt%2Fcdktf-aws-networking-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielMSchmidt%2Fcdktf-aws-networking-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DanielMSchmidt","download_url":"https://codeload.github.com/DanielMSchmidt/cdktf-aws-networking-demo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243646699,"owners_count":20324587,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T19:15:46.074Z","updated_at":"2025-03-14T21:18:05.717Z","avatar_url":"https://github.com/DanielMSchmidt.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CDKTF Networking Demo for AWS\n\nThis is a port of parts 1 and 2 from [hashicorp/microservices-architecture-on-aws](https://github.com/hashicorp/microservices-architecture-on-aws/tree/part-2), adapted for CDKTF.\n\n## Summary\n\n- [Part 1: Port of the Terraform Code](https://github.com/DanielMSchmidt/cdktf-aws-networking-demo/tree/part-1)\n- [Part 2: Refactoring into Constructs](https://github.com/DanielMSchmidt/cdktf-aws-networking-demo/tree/part-2)\n\n## The Architecture\n\nWe implement roughly this architecture:\n\n![Architecture](https://github.com/hashicorp/microservices-architecture-on-aws/raw/main/images/aws-consul-ecs-finalized-architecture.png)\n\nWe create a VPC with private and public subnets and use a gateway to expose traffic from our ECS cluster to the internet.\n\n## Getting started\n\nTo run this demo, you need to have the following:\n\n- Terraform \u003e= 1.0.0\n- Node.js \u003e= 12\n- cdktf-cli 0.11.2 (can be installed via `npm install -g cdktf-cli@0.11.2`)\n- [Credentials to AWS set up in your environment](https://github.com/hashicorp/microservices-architecture-on-aws#prerequisites)\n\nRun the following command to get started:\n\n- `npm install` to install all dependencies\n- `cdktf deploy cdktf-aws-networking-demo` to deploy the demo\n\n## Walkthrough\n\nThis walkthrough should give you a high level overview of CDKTF, especially coming from the AWS CDK or Terraform. For more in-depth information, please refer to the [Terraform CDK documentation](https://www.terraform.io/cdktf).\n\n### Coming from Terraform\n\nCDKTF does not come with it's own language but works with a few imperative programming languages instead: Typescript, Python, Java, C#, Go. Instead of variable / output / resource / data source blocks a CDKTF program consists of Constructs. [Constructs](https://github.com/aws/constructs) is a framework to build a graph that builds the desired state. CDKTF takes this graph and builds Terraform code from it. The Constructs you can use in CDKTF programs come from the CDKTF core library, third party packages, or are generated bindings based on the Terraform Provider schema. You can use Constructs [to build logical components, see part 2](https://github.com/DanielMSchmidt/cdktf-aws-networking-demo/tree/part-2). To instanciate a Construct you have to pass the scope (which is the parent), a unique identifier within this scope, and whatever properties this specific Construct requires.\n\nA special construct is the `TerraformStack`, which maps roughly to a Terraform workspace. CDKTF can handle multiple stacks and even build connections between them automatically; they are resolved through `RemoteStateDataSources` and we call them [Cross Stack References](https://www.terraform.io/cdktf/concepts/stacks#cross-stack-references). The CDKTF CLI can be used to deploy / destroy multiple in parallel.\n\n### Coming from AWS CDK\n\nThe concept of AWS CDK and CDKTF is very similar. The main difference is that CDKTF synthesizes the code into Terraform instead of CloudFormation, meaning CDKTF can be used to build infrastructure on any platform with a Terraform provider. Since we have a much broader spectrum of things to support we have only few L2 / L3 constructs in place. Instead we focus much more on making the generated L1 constructs great and provide a good UX that way. We also work on an [AWS CDK Adapter](https://www.terraform.io/cdktf/create-and-deploy/aws-adapter) that lets you use AWS CDK Constructs within your CDKTF program seemlessly, it's currently in technical preview.\n\n### Provider bindings\n\nIn line 2 you can see we are importing the AWS provider. We are using a pre-built provider here, these are a set of providers we generate and publish bindings for. [You can use any provider you want with CDKTF](https://www.terraform.io/cdktf/concepts/providers-and-resources#add-provider-to-cdktf-json), if you can we recommend using pre-built ones since it saves you some time. You can use the new `cdktf provider add` command to add the provider of your choice, it will find it either as a pre-built one or it will be added to your `cdktf.json` file.\n\nThe generated bindings are what is often referred to as L1 Constructs, so basic generated abstractions. We recommend wrapping your L1 constructs in a L2 construct, we will cover this in the [second part](https://github.com/danielmschmidt/cdktf-aws-networking-demo/tree/part-2).\n\nYou can also use the [AWS Adapter (technical preview)](https://github.com/hashicorp/cdktf-aws-cdk) to use the AWS CDK within CDKTF, we will go through this in the [third part](https://github.com/danielmschmidt/cdktf-aws-networking-demo/tree/part-3).\n\nYou will notice the generated bindings follow a naming convention, for example the `aws_ecs_cluster` binding is named `ecsCluster`. There are a few exceptions when the name would collide with a value in a programming language we support, but in general it's quite predictable. We represent blocks as objects if they can only appear once, as lists of objects if there can be more.\n\n### Note on the AWS provider namespacing\n\nSince AWS provides so many services the provider is quite big, this is why it's the only provider we currently namespace. The namespaces are determined by the [sidebar group the resource is in](https://registry.terraform.io/providers/hashicorp/aws/latest/docs).\n\n### Behind the scenes\n\n[The Deep Dive into CDK for Terraform](https://www.youtube.com/watch?v=nNr8JrN-9HE\u0026t=2s) is a good talk to watch to understand the inner workings of CDKTF.\nHere is the workflow when you run `cdktf deploy cdktf-aws-networking-demo` as a quick TL;DR overview:\n\n1. Synth: We run the app command from your `cdktf.json` file, this will generate the `cdktf.out` directory with the synthesized code. The code consists of a `manifest.json` that has all the stack metadata in it, e.g. dependencies between stacks. The `cdk.tf.json` file is the actual Terraform code that is being synthesized per stack. This phase is the synth phase, everything else is part of the execution phase.\n2. `terraform init` is run in the selected stack to download the actual providers\n3. `terraform plan` is run in the selected stack to generate the plan, it is shown to you through the CLI output\n4. `terraform apply` is run in the selected stack to apply the plan\n\n### Converting from Terraform to CDKTF\n\nTo help people convert documentation or their code base over to CDKTF, we have the [`cdktf convert`](https://www.terraform.io/cdktf/cli-reference/commands#convert) command. It will convert your Terraform code to CDKTF code. There might be some differences in the generated code, but most of the time it should be pretty close.\n\n### Refactoring into Constructs\n\nThe Terraform CDK does not come with a lot of built-in constructs, so we need to create our own.\nUsers can create abstractions for their own use-cases using `Constructs` and be creative in their structure. The can use methods / closures / constructors to create their own constructs and configure behaviour.\n\nConstructs can be used internally through file references, but they can also be distributed. [You can use the CDKTF projen construct](https://github.com/projen/projen/blob/main/src/cdktf/cdktf-construct.ts#L28) to generate a GitHub project for distributing your own CDKTF constructs. If you want to share your code both as constructs and as Terraform Modules you should take a look at [`https://github.com/DanielMSchmidt/projen-cdktf-hybrid-construct/`](https://github.com/DanielMSchmidt/projen-cdktf-hybrid-construct/), there is also a [CDK Day talk about it](https://www.youtube.com/watch?v=s8tO-ymVQPg\u0026t=10267s).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielmschmidt%2Fcdktf-aws-networking-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielmschmidt%2Fcdktf-aws-networking-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielmschmidt%2Fcdktf-aws-networking-demo/lists"}