From 3f3836b9fc4edff417992b3da9da7150d5d73765 Mon Sep 17 00:00:00 2001 From: hacker65536 Date: Thu, 12 Sep 2019 05:52:43 +0000 Subject: [PATCH 1/4] Fix and add example code Context --- doc_source/context.md | 103 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 84 insertions(+), 19 deletions(-) diff --git a/doc_source/context.md b/doc_source/context.md index 4e56f2bd..db213d18 100644 --- a/doc_source/context.md +++ b/doc_source/context.md @@ -5,9 +5,10 @@ The AWS CDK uses context to retrieve information such as the Availability Zones ## Construct Context You can provide context values in three different ways: -+ Automatically by the AWS CDK CLI after required information, such as the list of Availability Zones, is looked up from the current AWS account\. -+ Provided by the user through the CLI using the \-\-context option, or in the `context` key of the `cdk.json` file\. -+ Set in code using the `construct.node.setContext` method\. + +- Automatically by the AWS CDK CLI after required information, such as the list of Availability Zones, is looked up from the current AWS account\. +- Provided by the user through the CLI using the \-\-context option, or in the `context` key of the `cdk.json` file\. +- Set in code using the `construct.node.setContext` method\. Context entries are scoped to the construct that created them: they are visible to children constructs, but not to siblings\. Context entries that are set by the CLI, either automatically or from the \-\-context option, are implicitly set on the `App` construct, and are visible to the app\. @@ -19,16 +20,16 @@ The AWS CDK supports several context methods that enable AWS CDK apps to get con The following are the context methods: -[HostedZone\.fromLookup](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-route53.HostedZone.html#static-from-lookupscope-id-query) +[HostedZone\.fromLookup](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-route53.HostedZone.html#static-from-wbr-lookupscope-id-query) Gets the hosted zones in your account\. -[stack\.availabilityZones](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Stack.html#availabilityzones) +[stack\.availabilityZones](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-route53.HostedZone.html#static-from-wbr-lookupscope-id-query) Gets the supported Availability Zones\. -StringParameter\.valueFromLookup +[StringParameter\.valueFromLookup](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ssm.StringParameter.html#static-value-wbr-from-wbr-lookupscope-parametername) Gets a value from the current Region's AWS Systems Manager Parameter Store\. -Vpc\.fromLookup +[Vpc\.fromLookup](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.Vpc.html#static-from-wbr-lookupscope-id-options) Gets the existing VPCs in your accounts\. If a given context information isn't available, the AWS CDK app notifies the AWS CDK CLI that the context information is missing\. The CLI then queries the current AWS account for the information, stores the resulting context information in the `cdk.context.json` file, and executes the AWS CDK app again with the context values\. @@ -41,14 +42,14 @@ Use the cdk context command to view and manage the information in your `cdk.cont ``` Context found in cdk.json: - -┌───────────────────────────────────────────────────────────────────────────| -│ # | Key │ Value | -├───────────────────────────────────────────────────────────────────────────| -│ 1 | availability-zones:account=123456789012:region=eu-central-1 │ [ "eu-central-1a", "eu-central-1b", "eu-central-1c" ] | -├───────────────────────────────────────────────────────────────────────────| -│ 2 | availability-zones:account=123456789012:region=eu-west-1 │ [ "eu-west-1a", "eu-west-1b", "eu-west-1c" ] | -└───────────────────────────────────────────────────────────────────────────| + +┌───┬─────────────────────────────────────────────────────────────┬──────────────────────────────────────────────────────────────┐ +│ # | Key │ Value │ +├───┼─────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────│ +│ 1 | availability-zones:account=123456789012:region=eu-central-1 │ [ "eu-central-1a", "eu-central-1b", "eu-central-1c" ] │ +├───┼─────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────│ +│ 2 | availability-zones:account=123456789012:region=eu-west-1 │ [ "eu-west-1a", "eu-west-1b", "eu-west-1c" ] │ +└───┴─────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────┘ Run cdk context --reset KEY_OR_NUMBER to remove a context key. It will be refreshed on the next CDK synthesis run. ``` @@ -71,12 +72,76 @@ Therefore, if you want to update to the latest version of the Amazon Linux AMI, cdk synth ``` +To clear all of the stored context values for your app, run cdk context \-\-clear, as follows\. + ``` -... +cdk context --clear ``` -To clear all of the stored context values for your app, run cdk context \-\-clear, as follows\. +The following is an example of importing an existing vpc using cdk context. ``` -cdk context --clear -``` \ No newline at end of file +import cdk = require('@aws-cdk/core'); +import ec2 = require('@aws-cdk/aws-ec2'); + +export class ExistsvpcStack extends cdk.Stack { + constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { + super(scope, id, props); + + const vpcid = this.node.tryGetContext('vpcid'); + + const vpc = ec2.Vpc.fromLookup(this, 'VPC', { + vpcId: vpcid, + }); + + const pubsubnets = vpc.selectSubnets({subnetType: ec2.SubnetType.PUBLIC}); + + new cdk.CfnOutput(this, 'publicsubnets', { + value: pubsubnets.subnetIds.toString(), + }); + } +} +``` + +``` +$ cdk diff -c vpcid=vpc-0cb9c31031d0d3e22 +Stack ExistsvpcStack +Outputs +[+] Output publicsubnets publicsubnets: {"Value":"subnet-06e0ea7dd302d3e8f,subnet-01fc0acfb58f3128f"} +``` + +``` +$ cdk context -j + +{ + "vpc-provider:account=123456789012:filter.vpc-id=vpc-0cb9c31031d0d3e22:region=us-east-1": { + "vpcId": "vpc-0cb9c31031d0d3e22", + "availabilityZones": [ + "us-east-1a", + "us-east-1b" + ], + "privateSubnetIds": [ + "subnet-03ecfc033225be285", + "subnet-0cded5da53180ebfa" + ], + "privateSubnetNames": [ + "Private" + ], + "privateSubnetRouteTableIds": [ + "rtb-0e955393ced0ada04", + "rtb-05602e7b9f310e5b0" + ], + "publicSubnetIds": [ + "subnet-06e0ea7dd302d3e8f", + "subnet-01fc0acfb58f3128f" + ], + "publicSubnetNames": [ + "Public" + ], + "publicSubnetRouteTableIds": [ + "rtb-00d1fdfd823c82289", + "rtb-04bb1969b42969bcb" + ] + } +} +``` From baabab0a71e96a217934c6d1c47ce9f0d5c4ee71 Mon Sep 17 00:00:00 2001 From: hacker65536 Date: Fri, 13 Sep 2019 00:10:55 +0000 Subject: [PATCH 2/4] Fix wrong fix link of availabilityZones --- doc_source/context.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc_source/context.md b/doc_source/context.md index db213d18..d5bacc5f 100644 --- a/doc_source/context.md +++ b/doc_source/context.md @@ -23,7 +23,7 @@ The following are the context methods: [HostedZone\.fromLookup](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-route53.HostedZone.html#static-from-wbr-lookupscope-id-query) Gets the hosted zones in your account\. -[stack\.availabilityZones](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-route53.HostedZone.html#static-from-wbr-lookupscope-id-query) +[stack\.availabilityZones](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Stack.html#availabilityzones) Gets the supported Availability Zones\. [StringParameter\.valueFromLookup](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ssm.StringParameter.html#static-value-wbr-from-wbr-lookupscope-parametername) From 2ba3e45bbba5f84e46a083b91b5a5082ad5ac0dc Mon Sep 17 00:00:00 2001 From: hacker65536 Date: Fri, 13 Sep 2019 04:31:07 +0000 Subject: [PATCH 3/4] Fix wrong fix new line --- doc_source/context.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc_source/context.md b/doc_source/context.md index d5bacc5f..069322f2 100644 --- a/doc_source/context.md +++ b/doc_source/context.md @@ -26,7 +26,7 @@ Gets the hosted zones in your account\. [stack\.availabilityZones](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Stack.html#availabilityzones) Gets the supported Availability Zones\. -[StringParameter\.valueFromLookup](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ssm.StringParameter.html#static-value-wbr-from-wbr-lookupscope-parametername) +[StringParameter\.valueFromLookup](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ssm.StringParameter.html#static-value-wbr-from-wbr-lookupscope-parametername) Gets a value from the current Region's AWS Systems Manager Parameter Store\. [Vpc\.fromLookup](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.Vpc.html#static-from-wbr-lookupscope-id-options) From 656c2f70dae8b41c926fad700ae14f418691cfd0 Mon Sep 17 00:00:00 2001 From: hacker65536 Date: Fri, 13 Sep 2019 04:36:01 +0000 Subject: [PATCH 4/4] Fix prompt string --- doc_source/context.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc_source/context.md b/doc_source/context.md index 069322f2..16c6a4cc 100644 --- a/doc_source/context.md +++ b/doc_source/context.md @@ -69,13 +69,13 @@ reset. It will be refreshed on the next SDK synthesis run. Therefore, if you want to update to the latest version of the Amazon Linux AMI, you can use the preceding example to do a controlled update of the context value and reset it, and then synthesize and deploy your app again\. ``` -cdk synth +$ cdk synth ``` To clear all of the stored context values for your app, run cdk context \-\-clear, as follows\. ``` -cdk context --clear +$ cdk context --clear ``` The following is an example of importing an existing vpc using cdk context.