Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cdk-cli-lib to synthesize CDK application #170

Merged
merged 12 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/run-acceptance-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ jobs:
name: acceptance-test
concurrency:
group: acceptance-test-${{ matrix.index }} # TODO: concurrent tests across PRs can cause problems
cancel-in-progress: false
# TODO[pulumi/pulumi-cdk#152]: means that some resources in tests will have a static name
# which if the test does not complete, will not be cleaned up causing the next run to fail with resource already exists
cancel-in-progress: false
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,4 @@ test-reports/
lib/

**/cdk.out
**/cdk.context.json
159 changes: 104 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
# Pulumi CDK Adapter (preview)

The Pulumi CDK Adapter is a library that enables [Pulumi](https://github.com/pulumi/pulumi) programs to use [AWS CDK](https://github.com/aws/aws-cdk) constructs.
The Pulumi CDK Adapter is a library that enables
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change necessary? Looks like Emacs fil-paragraph got applied. I think we prefer to have the previous form.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, I kind of prefer the 80 line length limit. It makes looking/editing so much easier. It's also the default in most markdown linters, (e.g. markdownlint

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AH nice, one of those things that'd just nice to have a standard. We can adopt it in the project. I'll setup column width 80 in Emacs.

[Pulumi](https://github.com/pulumi/pulumi) programs to use [AWS
CDK](https://github.com/aws/aws-cdk) constructs.

The adapter allows writing AWS CDK code as part of an AWS CDK Stack inside a Pulumi program, and having the resulting AWS resources be deployed and managed via Pulumi. Outputs of resources defined in a Pulumi program can be passed into AWS CDK Constructs, and outputs from AWS CDK stacks can be used as inputs to other Pulumi resources.
The adapter allows writing AWS CDK code as part of an AWS CDK Stack inside a
Pulumi program, and having the resulting AWS resources be deployed and managed
via Pulumi. Outputs of resources defined in a Pulumi program can be passed
into AWS CDK Constructs, and outputs from AWS CDK stacks can be used as inputs
to other Pulumi resources.

> Note: Currently, the Pulumi CDK Adapter preview is available only for TypeScript/JavaScript users.
> Note: Currently, the Pulumi CDK Adapter preview is available only for
> TypeScript/JavaScript users.

For example, to construct an [AWS AppRunner `Service` resource](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-apprunner-alpha-readme.html) from within a Pulumi program, and export the resulting service's URL as as Pulumi Stack Output you write the following:
For example, to construct an [AWS AppRunner `Service`
resource](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-apprunner-alpha-readme.html)
from within a Pulumi program, and export the resulting service's URL as as
Pulumi Stack Output you write the following:

```ts
import * as pulumi from '@pulumi/pulumi';
Expand All @@ -27,18 +37,21 @@ class AppRunnerStack extends pulumicdk.Stack {
});

this.url = this.asOutput(service.serviceUrl);

this.synth();
}
}

const stack = new AppRunnerStack('teststack');
export const url = stack.url;
const app = new pulumicdk.App('app', (scope: pulumicdk.App): pulumicdk.AppOutputs => {
const stack = new AppRunnerStack('teststack');
return {
url: stack.url,
};
});
export const url = app.outputs['url'];
```

And then deploy with `pulumi update`:

```
```console
> pulumi up

Updating (dev)
Expand All @@ -60,7 +73,7 @@ Resources:

And curl the endpoint:

```
```console
> curl https://$(pulumi stack output url)

______ __ __ __ _ __
Expand All @@ -81,99 +94,135 @@ Try the workshop at https://apprunnerworkshop.com
Read the docs at https://docs.aws.amazon.com/apprunner
```

## Getting Started
## Bootstrapping

CDK has the concept of [bootstrapping](https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html)
which requires you to first bootstrap your account with certain AWS resources
that CDK needs to exist. With Pulumi CDK this is not required! Pulumi CDK will
automatically and dynamically create the bootstrap resources as needed.

### Bootstrapping
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now auto-bootstrap? Perhaps still worth a mention that it does this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some docs around bootstrapping and what resources we create for you.

### S3 Resources

AWS CDK requires that your AWS account and target region are "bootstrapped" for use with CDK, so in order to use CDK on Pulumi, you'll need to do that first. We recommend using the latest bootstrap template (v2, as of this writing). [See the AWS documentation](https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html) for details. (Note that resources deployed with Pulumi on CDK are deployed and managed with Pulumi, not CloudFormation; the bootstrapping step is required by CDK for additional runtime support.)
When any file assets are added to your application, CDK will automatically
create the following staging resources.

1. [aws.s3.BucketV2](https://www.pulumi.com/registry/packages/aws/api-docs/s3/bucketv2/)
- `forceDestroy`: true
2. [aws.s3.BucketServerSideEncryptionConfigurationV2](https://www.pulumi.com/registry/packages/aws/api-docs/s3/bucketserversideencryptionconfigurationv2/)
- `AES256`
3. [aws.s3.BucketVersioningV2](https://www.pulumi.com/registry/packages/aws/api-docs/s3/bucketversioningv2/)
- `Enabled`
4. [aws.s3.BucketLifecycleConfigurationV2](https://www.pulumi.com/registry/packages/aws/api-docs/s3/bucketlifecycleconfigurationv2/)
- Expire old versions > 365 days
- Expire deploy-time assets > 30 days
5. [aws.s3.BucketPolicy](https://www.pulumi.com/registry/packages/aws/api-docs/s3/bucketpolicy/)
- Require SSL

## API

### `Stack`
### `App`

A Construct that represents an AWS CDK stack deployed with Pulumi. In order to deploy a CDK stack with Pulumi, it must derive from this class. The `synth` method must be called after all CDK resources have been defined in order to deploy the stack (usually, this is done as the last line of the subclass's constructor).
A Pulumi CDK App component. This is the entrypoint to your Pulumi CDK application.
In order to deploy a CDK application with Pulumi, you must start with this class.

#### `constructor`

Create and register an AWS CDK stack deployed with Pulumi.
Create and register an AWS CDK app deployed with Pulumi.

```ts
constructor(name: string, options?: StackOptions)
constructor(id: string, createFunc: (scope: App) => void | AppOutputs, props?: AppResourceOptions)
```

Parameters:
* `name`: The _unique_ name of the resource.
* `options`: A bag of options that control this resource's behavior.

### `urn`

The URN of the underlying Pulumi component.
* `id`: The _unique_ name of the app
* `createFunc`: A callback function where all CDK Stacks must be created
* `options`: A bag of options that control the resource's behavior

#### `outputs`

The collection of outputs from the AWS CDK Stack represented as Pulumi Outputs. Each `CfnOutput` defined in the AWS CDK Stack will populate a value in the outputs.
The collection of outputs from the Pulumi CDK App represented as Pulumi Outputs.
Each `CfnOutput` defined in each AWS CDK Stack will populate a value in the
outputs.

```ts
outputs: { [outputId: string]: pulumi.Output<any> }
```

#### `synth`

Finalize the stack and deploy its resources.

```ts
protected synth()
```
#### `AppResourceOptions`

#### `asOutput`

Convert a CDK value to a Pulumi output.

Parameters:
* `v`: A CDK value.

```ts
asOutput<T>(v: T): pulumi.Output<T>
```

### `StackOptions`

Options specific to the Stack component.
Options specific to the App Component

```ts
interface StackOptions
interface AppResourceOptions
```

#### `remapCloudControlResource`

This optional method can be implemented to define a mapping to override and/or provide an implementation for a CloudFormation resource type that is not (yet) implemented in the AWS Cloud Control API (and thus not yet available in the Pulumi AWS Native provider). Pulumi code can override this method to provide a custom mapping of CloudFormation elements and their properties into Pulumi CustomResources, commonly by using the AWS Classic provider to implement the missing resource.
This optional method can be implemented to define a mapping to override and/or
provide an implementation for a CloudFormation resource type that is not (yet)
implemented in the AWS Cloud Control API (and thus not yet available in the
Pulumi AWS Native provider). Pulumi code can override this method to provide a
custom mapping of CloudFormation elements and their properties into Pulumi
CustomResources, commonly by using the AWS Classic provider to implement the
missing resource.

```ts
remapCloudControlResource(element: CfnElement, logicalId: string, typeName: string, props: any, options: pulumi.ResourceOptions): { [key: string]: pulumi.CustomResource } | undefined
remapCloudControlResource(logicalId: string, typeName: string, props: any, options: pulumi.ResourceOptions): { [key: string]: pulumi.CustomResource } | undefined
```

Parameters:
* `element`: The full CloudFormation element object being mapped.
* `logicalId`: The logical ID of the resource being mapped.
* `typeName`: The CloudFormation type name of the resource being mapped.
* `props`: The bag of input properties to the CloudFormation resource being mapped.
* `options`: The set of Pulumi ResourceOptions to apply to the resource being mapped.

Returns an object containing one or more logical IDs mapped to Pulumi resources that must be created to implement the mapped CloudFormation resource, or else undefined if no mapping is implemented.
Returns an object containing one or more logical IDs mapped to Pulumi resources
that must be created to implement the mapped CloudFormation resource, or else
undefined if no mapping is implemented.

#### `appId`

This is a unique identifier for the application. It will be used in the names of the
staging resources created for the application. This `appId` should be unique across apps.

### `Stack`

A Construct that represents an AWS CDK stack deployed with Pulumi. In order to
deploy a CDK stack with Pulumi, it must derive from this class.

#### `create`
#### `constructor`

Create and register an AWS CDK stack deployed with Pulumi.

```ts
create(name: string, ctor: typeof Stack, opts?: pulumi.CustomResourceOptions): StackComponent
constructor(app: App, name: string, options?: StackOptions)
```

Parameters:
* `app`: The Pulumi CDK App
* `name`: The _unique_ name of the resource.
flostadler marked this conversation as resolved.
Show resolved Hide resolved
* `stack`: The CDK Stack subclass to create.
* `parent`: The Pulumi CDKStackComponent parent resource.
* `opts`: A bag of options that control this resource's behavior.
* `options`: A bag of options that control this resource's behavior.


#### `asOutput`

Convert a CDK value to a Pulumi output.

Parameters:
* `v`: A CDK value.

```ts
asOutput<T>(v: T): pulumi.Output<T>
```

### `StackOptions`

Options specific to the Stack component.

```ts
interface StackOptions
```


### `asString`

Expand Down
Binary file added adr/assets/cdk_synth.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading