Skip to content

Commit b940710

Browse files
authored
feat(init-templates): app template comes with hint comments for 'env' (#13696)
How the `env` parameter is supposed to be used is still confusing to a lot of users. Add uncommentable lines to the init templates showing and describing the 3 alternatives. Fixes #12321. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 9d228e4 commit b940710

File tree

10 files changed

+205
-10
lines changed

10 files changed

+205
-10
lines changed

packages/aws-cdk/lib/init-templates/v1/app/csharp/src/%name.PascalCased%/Program.template.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,35 @@ sealed class Program
1010
public static void Main(string[] args)
1111
{
1212
var app = new App();
13-
new %name.PascalCased%Stack(app, "%name.PascalCased%Stack");
13+
new %name.PascalCased%Stack(app, "%name.PascalCased%Stack", new StackProps
14+
{
15+
// If you don't specify 'env', this stack will be environment-agnostic.
16+
// Account/Region-dependent features and context lookups will not work,
17+
// but a single synthesized template can be deployed anywhere.
18+
19+
// Uncomment the next block to specialize this stack for the AWS Account
20+
// and Region that are implied by the current CLI configuration.
21+
/*
22+
Env = new Amazon.CDK.Environment
23+
{
24+
Account = System.Environment.GetEnvironmentVariable("CDK_DEFAULT_ACCOUNT"),
25+
Region = System.Environment.GetEnvironmentVariable("CDK_DEFAULT_REGION"),
26+
}
27+
*/
28+
29+
// Uncomment the next block if you know exactly what Account and Region you
30+
// want to deploy the stack to.
31+
/*
32+
Env = new Amazon.CDK.Environment
33+
{
34+
Account = "123456789012",
35+
Region = "us-east-1",
36+
}
37+
*/
38+
39+
// For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html
40+
});
41+
1442
app.Synth();
1543
}
1644
}

packages/aws-cdk/lib/init-templates/v1/app/java/src/main/java/com/myorg/%name.PascalCased%App.template.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
package com.myorg;
22

33
import software.amazon.awscdk.core.App;
4+
import software.amazon.awscdk.core.Environment;
45

56
import java.util.Arrays;
67

78
public class %name.PascalCased%App {
89
public static void main(final String[] args) {
910
App app = new App();
1011

11-
new %name.PascalCased%Stack(app, "%name.PascalCased%Stack");
12+
%name.PascalCased%Stack.Builder.create(app, "%name.PascalCased%Stack")
13+
// If you don't specify 'env', this stack will be environment-agnostic.
14+
// Account/Region-dependent features and context lookups will not work,
15+
// but a single synthesized template can be deployed anywhere.
16+
17+
// Uncomment the next block to specialize this stack for the AWS Account
18+
// and Region that are implied by the current CLI configuration.
19+
/*
20+
.env(Environment.builder()
21+
.account(System.getenv("CDK_DEFAULT_ACCOUNT"))
22+
.region(System.getenv("CDK_DEFAULT_REGION"))
23+
.build())
24+
*/
25+
26+
// Uncomment the next block if you know exactly what Account and Region you
27+
// want to deploy the stack to.
28+
/*
29+
.env(Environment.builder()
30+
.account("123456789012")
31+
.region("us-east-1")
32+
.build())
33+
*/
34+
35+
// For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html
36+
.build();
1237

1338
app.synth();
1439
}

packages/aws-cdk/lib/init-templates/v1/app/javascript/bin/%name%.template.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,18 @@ const cdk = require('@aws-cdk/core');
44
const { %name.PascalCased%Stack } = require('../lib/%name%-stack');
55

66
const app = new cdk.App();
7-
new %name.PascalCased%Stack(app, '%name.PascalCased%Stack');
7+
new %name.PascalCased%Stack(app, '%name.PascalCased%Stack', {
8+
/* If you don't specify 'env', this stack will be environment-agnostic.
9+
* Account/Region-dependent features and context lookups will not work,
10+
* but a single synthesized template can be deployed anywhere. */
11+
12+
/* Uncomment the next line to specialize this stack for the AWS Account
13+
* and Region that are implied by the current CLI configuration. */
14+
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
15+
16+
/* Uncomment the next line if you know exactly what Account and Region you
17+
* want to deploy the stack to. */
18+
// env: { account: '123456789012', region: 'us-east-1' },
19+
20+
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
21+
});

packages/aws-cdk/lib/init-templates/v1/app/python/app.template.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python3
2+
import os
23

34
from aws_cdk import core as cdk
45

@@ -12,6 +13,22 @@
1213

1314

1415
app = core.App()
15-
%name.PascalCased%Stack(app, "%name.PascalCased%Stack")
16+
%name.PascalCased%Stack(app, "%name.PascalCased%Stack",
17+
# If you don't specify 'env', this stack will be environment-agnostic.
18+
# Account/Region-dependent features and context lookups will not work,
19+
# but a single synthesized template can be deployed anywhere.
20+
21+
# Uncomment the next line to specialize this stack for the AWS Account
22+
# and Region that are implied by the current CLI configuration.
23+
24+
#env=core.Environment(account=os.getenv('CDK_DEFAULT_ACCOUNT'), region=os.getenv('CDK_DEFAULT_REGION')),
25+
26+
# Uncomment the next line if you know exactly what Account and Region you
27+
# want to deploy the stack to. */
28+
29+
#env=core.Environment(account='123456789012', region='us-east-1'),
30+
31+
# For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html
32+
)
1633

1734
app.synth()

packages/aws-cdk/lib/init-templates/v1/app/typescript/bin/%name%.template.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,18 @@ import * as cdk from '@aws-cdk/core';
44
import { %name.PascalCased%Stack } from '../lib/%name%-stack';
55

66
const app = new cdk.App();
7-
new %name.PascalCased%Stack(app, '%name.PascalCased%Stack');
7+
new %name.PascalCased%Stack(app, '%name.PascalCased%Stack', {
8+
/* If you don't specify 'env', this stack will be environment-agnostic.
9+
* Account/Region-dependent features and context lookups will not work,
10+
* but a single synthesized template can be deployed anywhere. */
11+
12+
/* Uncomment the next line to specialize this stack for the AWS Account
13+
* and Region that are implied by the current CLI configuration. */
14+
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
15+
16+
/* Uncomment the next line if you know exactly what Account and Region you
17+
* want to deploy the stack to. */
18+
// env: { account: '123456789012', region: 'us-east-1' },
19+
20+
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
21+
});

packages/aws-cdk/lib/init-templates/v2/app/csharp/src/%name.PascalCased%/Program.template.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,34 @@ sealed class Program
1010
public static void Main(string[] args)
1111
{
1212
var app = new App();
13-
new %name.PascalCased%Stack(app, "%name.PascalCased%Stack");
13+
new %name.PascalCased%Stack(app, "%name.PascalCased%Stack", new StackProps
14+
{
15+
// If you don't specify 'env', this stack will be environment-agnostic.
16+
// Account/Region-dependent features and context lookups will not work,
17+
// but a single synthesized template can be deployed anywhere.
18+
19+
// Uncomment the next block to specialize this stack for the AWS Account
20+
// and Region that are implied by the current CLI configuration.
21+
/*
22+
Env = new Amazon.CDK.Environment
23+
{
24+
Account = System.Environment.GetEnvironmentVariable("CDK_DEFAULT_ACCOUNT"),
25+
Region = System.Environment.GetEnvironmentVariable("CDK_DEFAULT_REGION"),
26+
}
27+
*/
28+
29+
// Uncomment the next block if you know exactly what Account and Region you
30+
// want to deploy the stack to.
31+
/*
32+
Env = new Amazon.CDK.Environment
33+
{
34+
Account = "123456789012",
35+
Region = "us-east-1",
36+
}
37+
*/
38+
39+
// For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html
40+
});
1441
app.Synth();
1542
}
1643
}

packages/aws-cdk/lib/init-templates/v2/app/java/src/main/java/com/myorg/%name.PascalCased%App.template.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
package com.myorg;
22

33
import software.amazon.awscdk.lib.App;
4+
import software.amazon.awscdk.lib.Environment;
45

56
import java.util.Arrays;
67

78
public class %name.PascalCased%App {
89
public static void main(final String[] args) {
910
App app = new App();
1011

11-
new %name.PascalCased%Stack(app, "%name.PascalCased%Stack");
12+
%name.PascalCased%Stack.Builder.create(app, "%name.PascalCased%Stack")
13+
// If you don't specify 'env', this stack will be environment-agnostic.
14+
// Account/Region-dependent features and context lookups will not work,
15+
// but a single synthesized template can be deployed anywhere.
16+
17+
// Uncomment the next block to specialize this stack for the AWS Account
18+
// and Region that are implied by the current CLI configuration.
19+
/*
20+
.env(Environment.builder()
21+
.account(System.getenv("CDK_DEFAULT_ACCOUNT"))
22+
.region(System.getenv("CDK_DEFAULT_REGION"))
23+
.build())
24+
*/
25+
26+
// Uncomment the next block if you know exactly what Account and Region you
27+
// want to deploy the stack to.
28+
/*
29+
.env(Environment.builder()
30+
.account("123456789012")
31+
.region("us-east-1")
32+
.build())
33+
*/
34+
35+
// For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html
36+
.build();
1237

1338
app.synth();
1439
}

packages/aws-cdk/lib/init-templates/v2/app/javascript/bin/%name%.template.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,18 @@ const cdk = require('aws-cdk-lib');
44
const { %name.PascalCased%Stack } = require('../lib/%name%-stack');
55

66
const app = new cdk.App();
7-
new %name.PascalCased%Stack(app, '%name.PascalCased%Stack');
7+
new %name.PascalCased%Stack(app, '%name.PascalCased%Stack', {
8+
/* If you don't specify 'env', this stack will be environment-agnostic.
9+
* Account/Region-dependent features and context lookups will not work,
10+
* but a single synthesized template can be deployed anywhere. */
11+
12+
/* Uncomment the next line to specialize this stack for the AWS Account
13+
* and Region that are implied by the current CLI configuration. */
14+
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
15+
16+
/* Uncomment the next line if you know exactly what Account and Region you
17+
* want to deploy the stack to. */
18+
// env: { account: '123456789012', region: 'us-east-1' },
19+
20+
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
21+
});
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
#!/usr/bin/env python3
2+
import os
23

34
import aws_cdk_lib as core
45

56
from %name.PythonModule%.%name.PythonModule%_stack import %name.PascalCased%Stack
67

78

89
app = core.App()
9-
%name.PascalCased%Stack(app, "%name.PascalCased%Stack")
10+
%name.PascalCased%Stack(app, "%name.PascalCased%Stack",
11+
# If you don't specify 'env', this stack will be environment-agnostic.
12+
# Account/Region-dependent features and context lookups will not work,
13+
# but a single synthesized template can be deployed anywhere.
14+
15+
# Uncomment the next line to specialize this stack for the AWS Account
16+
# and Region that are implied by the current CLI configuration.
17+
18+
#env=core.Environment(account=os.getenv('CDK_DEFAULT_ACCOUNT'), region=os.getenv('CDK_DEFAULT_REGION')),
19+
20+
# Uncomment the next line if you know exactly what Account and Region you
21+
# want to deploy the stack to. */
22+
23+
#env=core.Environment(account='123456789012', region='us-east-1'),
24+
25+
# For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html
26+
)
1027

1128
app.synth()

packages/aws-cdk/lib/init-templates/v2/app/typescript/bin/%name%.template.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,18 @@ import * as cdk from 'aws-cdk-lib';
44
import { %name.PascalCased%Stack } from '../lib/%name%-stack';
55

66
const app = new cdk.App();
7-
new %name.PascalCased%Stack(app, '%name.PascalCased%Stack');
7+
new %name.PascalCased%Stack(app, '%name.PascalCased%Stack', {
8+
/* If you don't specify 'env', this stack will be environment-agnostic.
9+
* Account/Region-dependent features and context lookups will not work,
10+
* but a single synthesized template can be deployed anywhere. */
11+
12+
/* Uncomment the next line to specialize this stack for the AWS Account
13+
* and Region that are implied by the current CLI configuration. */
14+
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
15+
16+
/* Uncomment the next line if you know exactly what Account and Region you
17+
* want to deploy the stack to. */
18+
// env: { account: '123456789012', region: 'us-east-1' },
19+
20+
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
21+
});

0 commit comments

Comments
 (0)