CDK Infrastructure
AWS CDK is a framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation.
The TypeScript infrastructure generator creates an AWS CDK infrastructure application written in TypeScript. The generated application includes security best practices through CFN Guard checks.
Usage
Generate an Infrastructure Project
You can generate a new infrastructure project in two ways:
- Install the Nx Console VSCode Plugin if you haven't already
- Open the Nx Console in VSCode
- Click
Generate (UI)
in the "Common Nx Commands" section - Search for
@aws/nx-plugin - ts#infra
- Fill in the required parameters
- Click
Generate
pnpm nx g @aws/nx-plugin:ts#infra
yarn nx g @aws/nx-plugin:ts#infra
npx nx g @aws/nx-plugin:ts#infra
bunx nx g @aws/nx-plugin:ts#infra
You can also perform a dry-run to see what files would be changed
pnpm nx g @aws/nx-plugin:ts#infra --dry-run
yarn nx g @aws/nx-plugin:ts#infra --dry-run
npx nx g @aws/nx-plugin:ts#infra --dry-run
bunx nx g @aws/nx-plugin:ts#infra --dry-run
Options
Parameter | Type | Default | Description |
---|---|---|---|
name required | string | - | The name of the application. |
ruleSet | string | aws_prototyping | Rule set to validate your AWS resources with. |
directory | string | packages | The directory of the new application. |
Generator Output
The generator will create the following project structure in the <directory>/<name>
directory:
Directorysrc
- main.ts Application entry point instantiating CDK stacks to deploy
Directorystacks CDK Stack definitions
- application-stack.ts Main application stack
- cdk.json CDK configuration
- project.json Project configuration and build targets
Implementing your CDK Infrastructure
You can start writing your CDK infrastructure inside src/stacks/application-stack.ts
, for example:
import * as cdk from 'aws-cdk-lib';import { Bucket } from 'aws-cdk-lib/aws-s3'import { Construct } from 'constructs';
export class ApplicationStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props);
// Declare your infrastructure here new Bucket(this, 'MyBucket'); }}
API Infrastructure
If you have used the tRPC API or FastAPI generators to create APIs, you will notice you already have some constructs available in packages/common/constructs
to deploy them.
If, for example, you created a tRPC API called my-api
, you can simply import and instantiate the construct to add all necessary infrastructure to deploy it:
import * as cdk from 'aws-cdk-lib';import { Construct } from 'constructs';import { MyApi } from ':my-scope/common-constructs';
export class ApplicationStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props);
// Add infrastructure for your API new MyApi(this, 'MyApi'); }}
Website Infrastructure
If you have used the CloudScape website generator, you will notice you already have a construct in packages/common/constructs
to deploy it. For example:
import * as cdk from 'aws-cdk-lib';import { Construct } from 'constructs';import { MyWebsite } from ':my-scope/common-constructs';
export class ApplicationStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props);
// Add infrastructure for your website new MyWebsite(this, 'MyWebsite'); }}
It is important to ensure that the website is declared after any API constructs in order for the website Runtime Config to include all API config.
Synthesizing your Infrastructure
As part of your build
target, as well as running the default compile, lint and test targets, your infrastructure project is synthesized to CloudFormation. This can also be executed in a standalone fashion, by running the synth
target:
pnpm nx run <my-infra>:synth
yarn nx run <my-infra>:synth
npx nx run <my-infra>:synth
bunx nx run <my-infra>:synth
You will find your synthesized cloud assembly in the root dist
folder, under dist/packages/<my-infra-project>/cdk.out
.
Bootstrapping your AWS Account(s)
If you are deploying a CDK application to an AWS Account for the first time, it will need to be bootstrapped first.
First, ensure that you have configured credentials for your AWS account.
Next, you can use the cdk bootstrap
command:
npx cdk bootstrap aws://<account-id>/<region>
For more details, please refer to the CDK documentation.
Deploying to AWS
After a build, you can deploy your infrastructure to AWS using the deploy
target.
First, ensure that you have configured credentials for your AWS account.
Next, run the deploy target:
pnpm nx run <my-infra>:deploy --all
yarn nx run <my-infra>:deploy --all
npx nx run <my-infra>:deploy --all
bunx nx run <my-infra>:deploy --all
More Information
For more information about CDK, please refer to the CDK Developer Guide and API Reference.