Skip to content

Go Examples for CDK Docs #456

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

Merged
merged 3 commits into from
Mar 20, 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
71 changes: 50 additions & 21 deletions v2/apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ The AWS Cloud Development Kit \(AWS CDK\) application is called an *app*\. A CDK
+ [Defining apps](#apps_construct)
+ [Working with apps](#apps-work)

## Defining apps<a name="apps_construct"></a>

You create an app by importing and using the [https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.App.html](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.App.html) class from the AWS Construct Library\. You then define your stacks within the app and define your constructs within stacks\. An app must contain at least one stack\.

With this structure in place, you can synthesize stacks before deployment\. Synthesizing stacks involves creating an AWS CloudFormation template per stack that is then used to deploy to AWS CloudFormation to provision your AWS resources\. To learn more about stacks, see [Stacks](stacks.md)\.

The `App` construct doesn't require any initialization arguments\. It is the only construct that can be used as a root for the construct tree\.

The following is an example of a new AWS CDK app that includes a stack named `MyFirstStack`\. The stack is then synthesized, producing an AWS CloudFormation template:
---

------
#### [ TypeScript ]

```
Expand All @@ -25,7 +22,8 @@ new MyFirstStack(app, 'hello-cdk');
app.synth();
```

------
---

#### [ JavaScript ]

```
Expand All @@ -34,7 +32,8 @@ new MyFirstStack(app, 'hello-cdk');
app.synth();
```

------
---

#### [ Python ]

```
Expand All @@ -43,7 +42,8 @@ MyFirstStack(app, "hello-cdk")
app.synth()
```

------
---

#### [ Java ]

```
Expand All @@ -52,7 +52,8 @@ new MyFirstStack(app, "hello-cdk");
app.synth();
```

------
---

#### [ C\# ]

```
Expand All @@ -61,7 +62,23 @@ new MyFirstStack(app, "hello-cdk");
app.Synth();
```

------
---

#### [ Go ]

```
app := awscdk.NewApp(nil)

MyFirstStack(app, "MyFirstStack", &MyFirstStackProps{
awscdk.StackProps{
Env: env(),
},
})

app.Synth(nil)
```

---

Stacks within a single app can easily refer to each other's resources and properties\. The AWS CDK infers dependencies between stacks so that they can be deployed in the correct order\. You can deploy any or all of the stacks within an app with a single `cdk deploy` command\.

Expand All @@ -71,7 +88,7 @@ Stacks within a single app can easily refer to each other's resources and proper

The following diagram shows the phases that the AWS CDK goes through when you call cdk deploy\. This command deploys the resources that your app defines\.

![\[Image NOT FOUND\]](http://docs.aws.amazon.com/cdk/v2/guide/images/Lifecycle.png)
![[Image NOT FOUND]](http://docs.aws.amazon.com/cdk/v2/guide/images/Lifecycle.png)

An AWS CDK app goes through the following phases in its lifecycle\.

Expand All @@ -91,8 +108,7 @@ This is the final stage of the execution of your AWS CDK app\. It's triggered by
In this phase, the AWS CDK Toolkit takes the deployment artifacts cloud assembly produced by the synthesis phase and deploys it to an AWS environment\. It uploads assets to Amazon S3 and Amazon ECR, or wherever they need to go\. Then, it starts an AWS CloudFormation deployment to deploy the application and create the resources\.

By the time the AWS CloudFormation deployment phase \(step 5\) starts, your AWS CDK app has already finished and exited\. This has the following implications:
+ The AWS CDK app can't respond to events that happen during deployment, such as a resource being created or the whole deployment finishing\. To run code during the deployment phase, you must inject it into the AWS CloudFormation template as a [custom resource](cfn_layer.md#develop-customize-custom)\. For more information about adding a custom resource to your app, see the [AWS CloudFormation module](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudformation-readme.html), or the [custom\-resource](https://github.com/aws-samples/aws-cdk-examples/tree/master/typescript/custom-resource/) example\.
+ The AWS CDK app might have to work with values that can't be known at the time it runs\. For example, if the AWS CDK app defines an Amazon S3 bucket with an automatically generated name, and you retrieve the `bucket.bucketName` \(Python: `bucket_name`\) attribute, that value is not the name of the deployed bucket\. Instead, you get a `Token` value\. To determine whether a particular value is available, call `cdk.isUnresolved(value)` \(Python: `is_unresolved`\)\. See [Tokens](tokens.md) for details\.


### Cloud assemblies<a name="apps_cloud_assembly"></a>

Expand All @@ -104,7 +120,8 @@ To interact with the cloud assembly that your AWS CDK app creates, you typically

The CDK Toolkit needs to know how to execute your AWS CDK app\. If you created the project from a template using the `cdk init` command, your app's `cdk.json` file includes an `app` key\. This key specifies the necessary command for the language that the app is written in\. If your language requires compilation, the command line performs this step before running the app, so you can't forget to do it\.

------
---

#### [ TypeScript ]

```
Expand All @@ -113,7 +130,8 @@ The CDK Toolkit needs to know how to execute your AWS CDK app\. If you created t
}
```

------
---

#### [ JavaScript ]

```
Expand All @@ -122,7 +140,8 @@ The CDK Toolkit needs to know how to execute your AWS CDK app\. If you created t
}
```

------
---

#### [ Python ]

```
Expand All @@ -131,7 +150,8 @@ The CDK Toolkit needs to know how to execute your AWS CDK app\. If you created t
}
```

------
---

#### [ Java ]

```
Expand All @@ -140,7 +160,8 @@ The CDK Toolkit needs to know how to execute your AWS CDK app\. If you created t
}
```

------
---

#### [ C\# ]

```
Expand All @@ -149,18 +170,26 @@ The CDK Toolkit needs to know how to execute your AWS CDK app\. If you created t
}
```

------
---

#### [ Go ]

```
{
"app": "go mod download && go run my-app.go"
}
```

---

If you didn't create your project using the CDK Toolkit, or if you want to override the command line given in `cdk.json`, you can use the \-\-app option when issuing the `cdk` command\.

```
$ cdk --app 'executable' cdk-command ...
```

The *executable* part of the command indicates the command that should be run to execute your CDK application\. Use quotation marks as shown, since such commands contain spaces\. The *cdk\-command* is a subcommand like synth or deploy that tells the CDK Toolkit what you want to do with your app\. Follow this with any additional options needed for that subcommand\.
The _executable_ part of the command indicates the command that should be run to execute your CDK application\. Use quotation marks as shown, since such commands contain spaces\. The _cdk\-command_ is a subcommand like synth or deploy that tells the CDK Toolkit what you want to do with your app\. Follow this with any additional options needed for that subcommand\.

The CLI can also interact directly with an already\-synthesized cloud assembly\. To do that, pass the directory in which the cloud assembly is stored in \-\-app\. The following example lists the stacks defined in the cloud assembly stored under `./my-cloud-assembly`\.

```
$ cdk --app ./my-cloud-assembly ls
```
69 changes: 51 additions & 18 deletions v2/aspects.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,85 @@ Aspects are a way to apply an operation to all constructs in a given scope\. The

To apply an aspect to a construct and all constructs in the same scope, call [https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.Aspects.html#static-ofscope](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.Aspects.html#static-ofscope)`.of(SCOPE).add()` with a new aspect, as shown in the following example\.

------
---

#### [ TypeScript ]

```
Aspects.of(myConstruct).add(new SomeAspect(...));
```

------
---

#### [ JavaScript ]

```
Aspects.of(myConstruct).add(new SomeAspect(...));
```

------
---

#### [ Python ]

```
Aspects.of(my_construct).add(SomeAspect(...))
```

------
---

#### [ Java ]

```
Aspects.of(myConstruct).add(new SomeAspect(...));
```

------
---

#### [ C\# ]

```
Aspects.Of(myConstruct).add(new SomeAspect(...));
```

------
---

#### [ Go ]

```
awscdk.Aspects_Of(stack).Add(awscdk.NewTag(...))
```

---

The AWS CDK uses aspects to [tag resources](tagging.md), but the framework can also be used for other purposes\. For example, you can use it to validate or change the AWS CloudFormation resources that are defined for you by higher\-level constructs\.

## Aspects in detail<a name="aspects_detail"></a>

Aspects employ the [visitor pattern](https://en.wikipedia.org/wiki/Visitor_pattern)\. An aspect is a class that implements the following interface\.

------
---

#### [ TypeScript ]

```
interface IAspect {
visit(node: IConstruct): void;}
```

------
---

#### [ JavaScript ]

JavaScript doesn't have interfaces as a language feature\. Therefore, an aspect is simply an instance of a class having a `visit` method that accepts the node to be operated on\.

------
---

#### [ Python ]

Python doesn't have interfaces as a language feature\. Therefore, an aspect is simply an instance of a class having a `visit` method that accepts the node to be operated on\.

------
---

#### [ Java ]

```
Expand All @@ -74,7 +91,8 @@ public interface IAspect {
}
```

------
---

#### [ C\# ]

```
Expand All @@ -84,7 +102,17 @@ public interface IAspect
}
```

------
---

#### [ Go ]

```
type IAspect interface {
Visit(node constructs.IConstruct)
}
```

---

When you call `Aspects.of(SCOPE).add(...)`, the construct adds the aspect to an internal list of aspects\. You can obtain the list with `Aspects.of(SCOPE)`\.

Expand All @@ -98,7 +126,8 @@ Aspects don't propagate across `Stage` construct boundaries, because `Stages` ar

The following example validates that all buckets created in the stack have versioning enabled\. The aspect adds an error annotation to the constructs that fail the validation\. This results in the synth operation failing and prevents deploying the resulting cloud assembly\.

------
---

#### [ TypeScript ]

```
Expand All @@ -122,7 +151,8 @@ class BucketVersioningChecker implements IAspect {
Aspects.of(stack).add(new BucketVersioningChecker());
```

------
---

#### [ JavaScript ]

```
Expand All @@ -146,7 +176,8 @@ class BucketVersioningChecker {
Aspects.of(stack).add(new BucketVersioningChecker());
```

------
---

#### [ Python ]

```
Expand All @@ -168,7 +199,8 @@ class BucketVersioningChecker:
Aspects.of(stack).add(BucketVersioningChecker())
```

------
---

#### [ Java ]

```
Expand All @@ -195,7 +227,8 @@ public class BucketVersioningChecker implements IAspect
Aspects.of(stack).add(new BucketVersioningChecker());
```

------
---

#### [ C\# ]

```
Expand All @@ -219,4 +252,4 @@ class BucketVersioningChecker : Amazon.Jsii.Runtime.Deputy.DeputyBase, IAspect
Aspects.Of(stack).add(new BucketVersioningChecker());
```

------
---
Loading