From fdd157d2f20303cd6352a47cab872846f7957181 Mon Sep 17 00:00:00 2001 From: KIDANI Akito Date: Tue, 12 Oct 2021 04:53:58 +0900 Subject: [PATCH 1/6] docs(apigwv2): fix typo (#16887) resoruce -> resource --- packages/@aws-cdk/aws-apigatewayv2/lib/websocket/api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-apigatewayv2/lib/websocket/api.ts b/packages/@aws-cdk/aws-apigatewayv2/lib/websocket/api.ts index f2f2653c94ee6..3d7d627ab4fef 100644 --- a/packages/@aws-cdk/aws-apigatewayv2/lib/websocket/api.ts +++ b/packages/@aws-cdk/aws-apigatewayv2/lib/websocket/api.ts @@ -21,7 +21,7 @@ export interface IWebSocketApi extends IApi { */ export interface WebSocketApiProps { /** - * Name for the WebSocket API resoruce + * Name for the WebSocket API resource * @default - id of the WebSocketApi construct. */ readonly apiName?: string; From 0499641d441c88b76e556df7b879f5f0348c8bb7 Mon Sep 17 00:00:00 2001 From: Nick Lynch Date: Tue, 12 Oct 2021 10:36:38 +0100 Subject: [PATCH 2/6] chore(ubergen): create missing L1 READMEs and strip stability banners (#16692) This change bundles together two changes for how ubergen handles package READMEs. The first is to generate a base README for experimental/alpha modules where only the L1s are included. The second is to strip out the stability banners from *all* READMEs; this is because, by definition, everything included in aws-cdk-lib is stable, so the banners are now redundant/misleading. fixes #16567 --- .../build-tools/create-missing-libraries.ts | 83 +++++-------------- packages/@aws-cdk/cfnspec/lib/index.ts | 1 + .../@aws-cdk/cfnspec/lib/library-creation.ts | 83 +++++++++++++++++++ packages/@aws-cdk/cfnspec/package.json | 2 +- .../cfnspec/test/libary-creation.test.ts | 59 +++++++++++++ tools/@aws-cdk/ubergen/bin/ubergen.ts | 11 ++- tools/@aws-cdk/ubergen/package.json | 1 + 7 files changed, 176 insertions(+), 64 deletions(-) create mode 100644 packages/@aws-cdk/cfnspec/lib/library-creation.ts create mode 100644 packages/@aws-cdk/cfnspec/test/libary-creation.test.ts diff --git a/packages/@aws-cdk/cfnspec/build-tools/create-missing-libraries.ts b/packages/@aws-cdk/cfnspec/build-tools/create-missing-libraries.ts index a8900329a3917..0f28a38a86f87 100644 --- a/packages/@aws-cdk/cfnspec/build-tools/create-missing-libraries.ts +++ b/packages/@aws-cdk/cfnspec/build-tools/create-missing-libraries.ts @@ -24,13 +24,9 @@ async function main() { // iterate over all cloudformation namespaces for (const namespace of cfnspec.namespaces()) { - const [moduleFamily, moduleBaseName] = (namespace === 'AWS::Serverless' ? 'AWS::SAM' : namespace).split('::'); - - const moduleName = `${moduleFamily}-${moduleBaseName.replace(/V\d+$/, '')}`.toLocaleLowerCase(); - const packagePath = path.join(root, moduleName); - - const lowcaseModuleName = moduleBaseName.toLocaleLowerCase(); - const packageName = `@aws-cdk/${moduleName}`; + const module = cfnspec.createModuleDefinitionFromCfnNamespace(namespace); + const lowcaseModuleName = module.moduleName.toLocaleLowerCase(); + const packagePath = path.join(root, module.moduleName); // we already have a module for this namesapce, move on. if (await fs.pathExists(packagePath)) { @@ -42,12 +38,12 @@ async function main() { if (scopes.indexOf(namespace) !== -1) { // V2-style module is already modeled in the root package, nothing to be done! continue; - } else if (await fs.pathExists(path.join(root, `${moduleFamily}-${moduleBaseName}`.toLocaleLowerCase()))) { + } else if (await fs.pathExists(path.join(root, `${module.moduleFamily}-${module.moduleBaseName}`.toLocaleLowerCase()))) { // V2-style package already has it's own package (legacy behavior), nothing to be done! continue; } else { // V2-style package needs to be added to it's "V1" package... Get down to business! - console.error(`Adding ${namespace} to ${packageName}`); + console.error(`Adding ${namespace} to ${module.packageName}`); scopes.push(namespace); packageJson['cdk-build'].cloudformation = scopes; await fs.writeJson(packageJsonPath, packageJson, { encoding: 'utf-8', spaces: 2 }); @@ -62,22 +58,6 @@ async function main() { } } - // dotnet names - const dotnetPackage = `Amazon.CDK.${moduleFamily}.${moduleBaseName}`; - - // java names - const javaGroupId = 'software.amazon.awscdk'; - const javaPackage = moduleFamily === 'AWS' - ? `services.${lowcaseModuleName}` - : `${moduleFamily.toLocaleLowerCase()}.${lowcaseModuleName}`; - const javaArtifactId = moduleFamily === 'AWS' - ? lowcaseModuleName - : `${moduleFamily.toLocaleLowerCase()}-${lowcaseModuleName}`; - - // python names - const pythonDistName = `aws-cdk.${moduleName}`; - const pythonModuleName = pythonDistName.replace(/-/g, '_'); - async function write(relativePath: string, contents: string[] | string | object) { const fullPath = path.join(packagePath, relativePath); const dir = path.dirname(fullPath); @@ -97,10 +77,10 @@ async function main() { await fs.writeFile(fullPath, data + '\n'); } - console.log(`generating module for ${packageName}...`); + console.log(`generating module for ${module.packageName}...`); await write('package.json', { - name: packageName, + name: module.packageName, version, description: `The CDK Construct Library for ${namespace}`, main: 'lib/index.js', @@ -110,17 +90,17 @@ async function main() { projectReferences: true, targets: { dotnet: { - namespace: dotnetPackage, - packageId: dotnetPackage, + namespace: module.dotnetPackage, + packageId: module.dotnetPackage, signAssembly: true, assemblyOriginatorKeyFile: '../../key.snk', iconUrl: 'https://raw.githubusercontent.com/aws/aws-cdk/master/logo/default-256-dark.png', }, java: { - package: `${javaGroupId}.${javaPackage}`, + package: `${module.javaGroupId}.${module.javaPackage}`, maven: { - groupId: javaGroupId, - artifactId: javaArtifactId, + groupId: module.javaGroupId, + artifactId: module.javaArtifactId, }, }, python: { @@ -128,15 +108,15 @@ async function main() { 'Framework :: AWS CDK', 'Framework :: AWS CDK :: 1', ], - distName: pythonDistName, - module: pythonModuleName, + distName: module.pythonDistName, + module: module.pythonModuleName, }, }, }, repository: { type: 'git', url: 'https://github.com/aws/aws-cdk.git', - directory: `packages/${packageName}`, + directory: `packages/${module.packageName}`, }, homepage: 'https://github.com/aws/aws-cdk', scripts: { @@ -169,7 +149,7 @@ async function main() { 'cdk', 'constructs', namespace, - moduleName, + module.moduleName, ], author: { name: 'Amazon Web Services', @@ -271,28 +251,7 @@ async function main() { '});', ]); - await write('README.md', [ - `# ${namespace} Construct Library`, - '', - '', - '---', - '', - '![cfn-resources: Stable](https://img.shields.io/badge/cfn--resources-stable-success.svg?style=for-the-badge)', - '', - '> All classes with the `Cfn` prefix in this module ([CFN Resources]) are always stable and safe to use.', - '>', - '> [CFN Resources]: https://docs.aws.amazon.com/cdk/latest/guide/constructs.html#constructs_lib', - '', - '---', - '', - '', - '', - 'This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aws-cdk) project.', - '', - '```ts', - `import ${lowcaseModuleName} = require('${packageName}');`, - '```', - ]); + await cfnspec.createLibraryReadme(namespace, path.join(packagePath, 'README.md')); await write('.eslintrc.js', [ "const baseConfig = require('@aws-cdk/cdk-build-tools/config/eslintrc');", @@ -310,10 +269,10 @@ async function main() { await fs.copy(path.join(templateDir, file), path.join(packagePath, file)); } - await addDependencyToMegaPackage(path.join('@aws-cdk', 'cloudformation-include'), packageName, version, ['dependencies', 'peerDependencies']); - await addDependencyToMegaPackage('aws-cdk-lib', packageName, version, ['devDependencies']); - await addDependencyToMegaPackage('monocdk', packageName, version, ['devDependencies']); - await addDependencyToMegaPackage('decdk', packageName, version, ['dependencies']); + await addDependencyToMegaPackage(path.join('@aws-cdk', 'cloudformation-include'), module.packageName, version, ['dependencies', 'peerDependencies']); + await addDependencyToMegaPackage('aws-cdk-lib', module.packageName, version, ['devDependencies']); + await addDependencyToMegaPackage('monocdk', module.packageName, version, ['devDependencies']); + await addDependencyToMegaPackage('decdk', module.packageName, version, ['dependencies']); } } diff --git a/packages/@aws-cdk/cfnspec/lib/index.ts b/packages/@aws-cdk/cfnspec/lib/index.ts index 6ab020d9580cc..17ef1bde49c36 100644 --- a/packages/@aws-cdk/cfnspec/lib/index.ts +++ b/packages/@aws-cdk/cfnspec/lib/index.ts @@ -3,6 +3,7 @@ import { CfnLintFileSchema } from './_private_schema/cfn-lint'; import * as schema from './schema'; export { schema }; export * from './canned-metrics'; +export * from './library-creation'; /** * The complete AWS CloudFormation Resource specification, having any CDK patches and enhancements included in it. diff --git a/packages/@aws-cdk/cfnspec/lib/library-creation.ts b/packages/@aws-cdk/cfnspec/lib/library-creation.ts new file mode 100644 index 0000000000000..85292a89d2749 --- /dev/null +++ b/packages/@aws-cdk/cfnspec/lib/library-creation.ts @@ -0,0 +1,83 @@ +import * as fs from 'fs-extra'; + +export interface ModuleDefinition { + readonly namespace: string; + readonly moduleName: string; + readonly moduleFamily: string; + readonly moduleBaseName: string; + readonly packageName: string; + + readonly dotnetPackage: string; + readonly javaGroupId: string; + readonly javaPackage: string; + readonly javaArtifactId: string; + + readonly pythonDistName: string; + readonly pythonModuleName: string; +} + +export function createModuleDefinitionFromCfnNamespace(namespace: string): ModuleDefinition { + const [moduleFamily, moduleBaseName] = (namespace === 'AWS::Serverless' ? 'AWS::SAM' : namespace).split('::'); + const moduleName = `${moduleFamily}-${moduleBaseName.replace(/V\d+$/, '')}`.toLocaleLowerCase(); + + const lowcaseModuleName = moduleBaseName.toLocaleLowerCase(); + const packageName = `@aws-cdk/${moduleName}`; + + // dotnet names + const dotnetPackage = `Amazon.CDK.${moduleFamily}.${moduleBaseName}`; + + // java names + const javaGroupId = 'software.amazon.awscdk'; + const javaPackage = moduleFamily === 'AWS' + ? `services.${lowcaseModuleName}` + : `${moduleFamily.toLocaleLowerCase()}.${lowcaseModuleName}`; + const javaArtifactId = moduleFamily === 'AWS' + ? lowcaseModuleName + : `${moduleFamily.toLocaleLowerCase()}-${lowcaseModuleName}`; + + // python names + const pythonDistName = `aws-cdk.${moduleName}`; + const pythonModuleName = pythonDistName.replace(/-/g, '_'); + + return { + namespace, + moduleName, + moduleFamily, + moduleBaseName, + packageName, + dotnetPackage, + javaGroupId, + javaPackage, + javaArtifactId, + pythonDistName, + pythonModuleName, + }; +} + +export async function createLibraryReadme(namespace: string, readmePath: string) { + const module = createModuleDefinitionFromCfnNamespace(namespace); + + await fs.writeFile(readmePath, [ + `# ${namespace} Construct Library`, + '', + '', + '---', + '', + '![cfn-resources: Stable](https://img.shields.io/badge/cfn--resources-stable-success.svg?style=for-the-badge)', + '', + '> All classes with the `Cfn` prefix in this module ([CFN Resources]) are always stable and safe to use.', + '>', + '> [CFN Resources]: https://docs.aws.amazon.com/cdk/latest/guide/constructs.html#constructs_lib', + '', + '---', + '', + '', + '', + 'This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aws-cdk) project.', + '', + '```ts', + `import ${module.moduleName.toLocaleLowerCase()} = require('${module.packageName}');`, + '```', + '', + ].join('\n'), 'utf8'); +} diff --git a/packages/@aws-cdk/cfnspec/package.json b/packages/@aws-cdk/cfnspec/package.json index 1109498bfe092..28df618bf6231 100644 --- a/packages/@aws-cdk/cfnspec/package.json +++ b/packages/@aws-cdk/cfnspec/package.json @@ -35,12 +35,12 @@ "@types/jest": "^26.0.24", "@types/md5": "^2.3.1", "fast-json-patch": "^2.2.1", - "fs-extra": "^9.1.0", "jest": "^26.6.3", "json-diff": "^0.5.4", "sort-json": "^2.0.0" }, "dependencies": { + "fs-extra": "^9.1.0", "md5": "^2.3.0" }, "repository": { diff --git a/packages/@aws-cdk/cfnspec/test/libary-creation.test.ts b/packages/@aws-cdk/cfnspec/test/libary-creation.test.ts new file mode 100644 index 0000000000000..354952325f314 --- /dev/null +++ b/packages/@aws-cdk/cfnspec/test/libary-creation.test.ts @@ -0,0 +1,59 @@ +import { createModuleDefinitionFromCfnNamespace } from '../lib'; + +describe('createModuleDefinitionFromCfnNamespace', () => { + + test('base case', () => { + const module = createModuleDefinitionFromCfnNamespace('AWS::EC2'); + + expect(module).toEqual({ + namespace: 'AWS::EC2', + moduleName: 'aws-ec2', + moduleFamily: 'AWS', + moduleBaseName: 'EC2', + packageName: '@aws-cdk/aws-ec2', + dotnetPackage: 'Amazon.CDK.AWS.EC2', + javaGroupId: 'software.amazon.awscdk', + javaPackage: 'services.ec2', + javaArtifactId: 'ec2', + pythonDistName: 'aws-cdk.aws-ec2', + pythonModuleName: 'aws_cdk.aws_ec2', + }); + }); + + test('Serverless is special-cased to SAM', () => { + const module = createModuleDefinitionFromCfnNamespace('AWS::Serverless'); + + expect(module).toEqual({ + namespace: 'AWS::Serverless', + moduleName: 'aws-sam', + moduleFamily: 'AWS', + moduleBaseName: 'SAM', + packageName: '@aws-cdk/aws-sam', + dotnetPackage: 'Amazon.CDK.AWS.SAM', + javaGroupId: 'software.amazon.awscdk', + javaPackage: 'services.sam', + javaArtifactId: 'sam', + pythonDistName: 'aws-cdk.aws-sam', + pythonModuleName: 'aws_cdk.aws_sam', + }); + }); + + test('Java artifacts use different package/artifact when module family is not AWS', () => { + const module = createModuleDefinitionFromCfnNamespace('Alexa::ASK'); + + expect(module).toEqual({ + namespace: 'Alexa::ASK', + moduleName: 'alexa-ask', + moduleFamily: 'Alexa', + moduleBaseName: 'ASK', + packageName: '@aws-cdk/alexa-ask', + dotnetPackage: 'Amazon.CDK.Alexa.ASK', + javaGroupId: 'software.amazon.awscdk', + javaPackage: 'alexa.ask', + javaArtifactId: 'alexa-ask', + pythonDistName: 'aws-cdk.alexa-ask', + pythonModuleName: 'aws_cdk.alexa_ask', + }); + }); + +}); diff --git a/tools/@aws-cdk/ubergen/bin/ubergen.ts b/tools/@aws-cdk/ubergen/bin/ubergen.ts index 885a7e66f6777..772e6b358b403 100644 --- a/tools/@aws-cdk/ubergen/bin/ubergen.ts +++ b/tools/@aws-cdk/ubergen/bin/ubergen.ts @@ -2,6 +2,7 @@ import * as console from 'console'; import * as path from 'path'; import * as process from 'process'; import cfn2ts from '@aws-cdk/cfn2ts'; +import * as cfnspec from '@aws-cdk/cfnspec'; import * as fs from 'fs-extra'; import * as ts from 'typescript'; @@ -284,12 +285,15 @@ async function transformPackage( const destinationLib = path.join(destination, 'lib'); await fs.mkdirp(destinationLib); await cfn2ts(cfnScopes, destinationLib); + // create a lib/index.ts which only exports the generated files fs.writeFileSync(path.join(destinationLib, 'index.ts'), /// logic copied from `create-missing-libraries.ts` cfnScopes.map(s => (s === 'AWS::Serverless' ? 'AWS::SAM' : s).split('::')[1].toLocaleLowerCase()) .map(s => `export * from './${s}.generated';`) .join('\n')); + await cfnspec.createLibraryReadme(cfnScopes[0], path.join(destination, 'README.md')); + await copyOrTransformFiles(destination, destination, allLibraries, uberPackageJson); } else { await copyOrTransformFiles(library.root, destination, allLibraries, uberPackageJson); @@ -395,9 +399,14 @@ async function copyOrTransformFiles(from: string, to: string, libraries: readonl } await fs.writeJson(destination, cfnTypes2Classes, { spaces: 2 }); } else if (name === 'README.md') { + // Rewrite the README to both adjust imports and remove the redundant stability banner. + // (All modules included in ubergen-ed packages must be stable, so the banner is unnecessary.) + const newReadme = (await rewriteReadmeImports(source)) + .replace(/[\s\S]+/gm, ''); + return fs.writeFile( destination, - await rewriteReadmeImports(source), + newReadme, { encoding: 'utf8' }, ); } else { diff --git a/tools/@aws-cdk/ubergen/package.json b/tools/@aws-cdk/ubergen/package.json index 910aa2152f1af..1f00f627e4c46 100644 --- a/tools/@aws-cdk/ubergen/package.json +++ b/tools/@aws-cdk/ubergen/package.json @@ -35,6 +35,7 @@ }, "dependencies": { "@aws-cdk/cfn2ts": "0.0.0", + "@aws-cdk/cfnspec": "0.0.0", "fs-extra": "^9.1.0", "typescript": "~3.9.10" }, From 0dcd9eca3a1014c39f92d9e052b67974fc751af0 Mon Sep 17 00:00:00 2001 From: Niranjan Jayakar Date: Tue, 12 Oct 2021 19:39:19 +0530 Subject: [PATCH 3/6] chore(assertions): consistent naming in maven (#16921) The maven artifact id is configured to 'cdk-assertions'. This makes the assertions module naming different from the other package managers, and hence harder to discover. Change the artifact id to 'assertions' and make this consistent. BREAKING CHANGE: Starting this release, the `assertions` module will be published to Maven with the name 'assertions' instead of 'cdk-assertions'. --- packages/@aws-cdk/assertions/package.json | 2 +- tools/@aws-cdk/pkglint/lib/rules.ts | 37 ++++++++++++++--------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/packages/@aws-cdk/assertions/package.json b/packages/@aws-cdk/assertions/package.json index 87fb2962b5745..e87d1c9526302 100644 --- a/packages/@aws-cdk/assertions/package.json +++ b/packages/@aws-cdk/assertions/package.json @@ -29,7 +29,7 @@ "package": "software.amazon.awscdk.assertions", "maven": { "groupId": "software.amazon.awscdk", - "artifactId": "cdk-assertions" + "artifactId": "assertions" } }, "dotnet": { diff --git a/tools/@aws-cdk/pkglint/lib/rules.ts b/tools/@aws-cdk/pkglint/lib/rules.ts index 140674492f0cc..f991f7b0a2441 100644 --- a/tools/@aws-cdk/pkglint/lib/rules.ts +++ b/tools/@aws-cdk/pkglint/lib/rules.ts @@ -14,9 +14,8 @@ import { monoRepoRoot, } from './util'; -const AWS_SERVICE_NAMES = require('./aws-service-official-names.json'); // eslint-disable-line @typescript-eslint/no-require-imports - const PKGLINT_VERSION = require('../package.json').version; // eslint-disable-line @typescript-eslint/no-require-imports +const AWS_SERVICE_NAMES = require('./aws-service-official-names.json'); // eslint-disable-line @typescript-eslint/no-require-imports /** * Verify that the package name matches the directory name @@ -831,25 +830,33 @@ function cdkModuleName(name: string) { const isCdkPkg = name === '@aws-cdk/core'; const isLegacyCdkPkg = name === '@aws-cdk/cdk'; - name = name.replace(/^aws-cdk-/, ''); - name = name.replace(/^@aws-cdk\//, ''); + let suffix = name; + suffix = suffix.replace(/^aws-cdk-/, ''); + suffix = suffix.replace(/^@aws-cdk\//, ''); - const dotnetSuffix = name.split('-') + const dotnetSuffix = suffix.split('-') .map(s => s === 'aws' ? 'AWS' : caseUtils.pascal(s)) .join('.'); - const pythonName = name.replace(/^@/g, '').replace(/\//g, '.').split('.').map(caseUtils.kebab).join('.'); + const pythonName = suffix.replace(/^@/g, '').replace(/\//g, '.').split('.').map(caseUtils.kebab).join('.'); + + // list of packages with special-cased Maven ArtifactId. + const mavenIdMap: Record = { + '@aws-cdk/core': 'core', + '@aws-cdk/cdk': 'cdk', + '@aws-cdk/assertions': 'assertions', + '@aws-cdk/assertions-alpha': 'assertions-alpha', + }; + /* eslint-disable @typescript-eslint/indent */ + const mavenArtifactId = + name in mavenIdMap ? mavenIdMap[name] : + (suffix.startsWith('aws-') || suffix.startsWith('alexa-')) ? suffix.replace(/aws-/, '') : + suffix.startsWith('cdk-') ? suffix : `cdk-${suffix}`; + /* eslint-enable @typescript-eslint/indent */ return { - javaPackage: `software.amazon.awscdk${isLegacyCdkPkg ? '' : `.${name.replace(/aws-/, 'services-').replace(/-/g, '.')}`}`, - mavenArtifactId: - isLegacyCdkPkg - ? 'cdk' - : (isCdkPkg - ? 'core' - : (name.startsWith('aws-') || name.startsWith('alexa-') - ? name.replace(/aws-/, '') - : (name.startsWith('cdk-') ? name : `cdk-${name}`))), + javaPackage: `software.amazon.awscdk${isLegacyCdkPkg ? '' : `.${suffix.replace(/aws-/, 'services-').replace(/-/g, '.')}`}`, + mavenArtifactId, dotnetNamespace: `Amazon.CDK${isCdkPkg ? '' : `.${dotnetSuffix}`}`, python: { distName: `aws-cdk.${pythonName}`, From aa5b22f455f462374b273e890b6498cdb349e865 Mon Sep 17 00:00:00 2001 From: Niranjan Jayakar Date: Tue, 12 Oct 2021 20:44:33 +0530 Subject: [PATCH 4/6] chore(cfnspec): improve messaging when property type cannot be determined (#16907) The error message is inaccurate and results in a message like - ``` AWS::Foo::Bar.Properties.Baz has known type: {"Documentation":"...","Required":false,"UpdateType":"Immutable"} ``` Improve the message. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/cfnspec/test/spec-validators.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/cfnspec/test/spec-validators.ts b/packages/@aws-cdk/cfnspec/test/spec-validators.ts index 408febb3e1b50..0c49c6bfa1a03 100644 --- a/packages/@aws-cdk/cfnspec/test/spec-validators.ts +++ b/packages/@aws-cdk/cfnspec/test/spec-validators.ts @@ -110,7 +110,8 @@ function validateProperties( } else { // eslint-disable-next-line no-console - console.error(`${typeName}.Properties.${name} has known type: ${JSON.stringify(property)}`); + console.error(`${typeName}.Properties.${name} does not declare a type.` + + `Property definition is: ${JSON.stringify(property, undefined, 2)}`); expect(false).toBeTruthy(); } From 4300a303f6ca24b05b361ad9c796df69abf3d41d Mon Sep 17 00:00:00 2001 From: kaizen3031593 <36202692+kaizen3031593@users.noreply.github.com> Date: Tue, 12 Oct 2021 12:04:18 -0400 Subject: [PATCH 5/6] docs(ecs): make examples compile (#16912) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/aws-ecs/README.md | 240 +++++++++++------- .../@aws-cdk/aws-ecs/lib/base/base-service.ts | 4 + packages/@aws-cdk/aws-ecs/lib/cluster.ts | 8 +- packages/@aws-cdk/aws-ecs/lib/index.ts | 1 + .../lib/log-drivers/generic-log-driver.ts | 2 +- .../aws-ecs/rosetta/default.ts-fixture | 24 ++ 6 files changed, 185 insertions(+), 94 deletions(-) create mode 100644 packages/@aws-cdk/aws-ecs/rosetta/default.ts-fixture diff --git a/packages/@aws-cdk/aws-ecs/README.md b/packages/@aws-cdk/aws-ecs/README.md index 8ffbde1d9a0e8..6a27855e031f5 100644 --- a/packages/@aws-cdk/aws-ecs/README.md +++ b/packages/@aws-cdk/aws-ecs/README.md @@ -23,7 +23,7 @@ The following example creates an Amazon ECS cluster, adds capacity to it, and runs a service on it: ```ts -import * as ecs from '@aws-cdk/aws-ecs'; +declare const vpc: ec2.Vpc; // Create an ECS cluster const cluster = new ecs.Cluster(this, 'Cluster', { @@ -89,8 +89,10 @@ tasks on. You can run many tasks on a single cluster. The following code creates a cluster that can run AWS Fargate tasks: ```ts +declare const vpc: ec2.Vpc; + const cluster = new ecs.Cluster(this, 'Cluster', { - vpc: vpc + vpc, }); ``` @@ -105,8 +107,10 @@ with various instance types. The following example creates an Amazon ECS cluster and adds capacity to it: ```ts +declare const vpc: ec2.Vpc; + const cluster = new ecs.Cluster(this, 'Cluster', { - vpc: vpc + vpc, }); // Either add default capacity @@ -119,7 +123,7 @@ cluster.addCapacity('DefaultAutoScalingGroupCapacity', { const autoScalingGroup = new autoscaling.AutoScalingGroup(this, 'ASG', { vpc, instanceType: new ec2.InstanceType('t2.xlarge'), - machineImage: EcsOptimizedImage.amazonLinux(), + machineImage: ecs.EcsOptimizedImage.amazonLinux(), // Or use Amazon ECS-Optimized Amazon Linux 2 AMI // machineImage: EcsOptimizedImage.amazonLinux2(), desiredCapacity: 3, @@ -143,9 +147,11 @@ to periodically update to the latest AMI manually by using the [CDK CLI context management commands](https://docs.aws.amazon.com/cdk/latest/guide/context.html): ```ts +declare const vpc: ec2.Vpc; const autoScalingGroup = new autoscaling.AutoScalingGroup(this, 'ASG', { - // ... - machineImage: EcsOptimizedImage.amazonLinux({ cacheInContext: true }), + machineImage: ecs.EcsOptimizedImage.amazonLinux({ cachedInContext: true }), + vpc, + instanceType: new ec2.InstanceType('t2.micro'), }); ``` @@ -159,6 +165,8 @@ The following example will create a capacity with self-managed Amazon EC2 capaci The following example adds Bottlerocket capacity to the cluster: ```ts +declare const cluster: ecs.Cluster; + cluster.addCapacity('bottlerocket-asg', { minCapacity: 2, instanceType: new ec2.InstanceType('c5.large'), @@ -174,6 +182,8 @@ for use when launching your EC2 instances that are powered by Arm-based AWS Graviton Processors. ```ts +declare const cluster: ecs.Cluster; + cluster.addCapacity('graviton-cluster', { minCapacity: 2, instanceType: new ec2.InstanceType('c6g.large'), @@ -184,10 +194,12 @@ cluster.addCapacity('graviton-cluster', { Bottlerocket is also supported: ```ts +declare const cluster: ecs.Cluster; + cluster.addCapacity('graviton-cluster', { minCapacity: 2, instanceType: new ec2.InstanceType('c6g.large'), - machineImage: ecs.MachineImageType.BOTTLEROCKET, + machineImageType: ecs.MachineImageType.BOTTLEROCKET, }); ``` @@ -196,6 +208,8 @@ cluster.addCapacity('graviton-cluster', { To add spot instances into the cluster, you must specify the `spotPrice` in the `ecs.AddCapacityOptions` and optionally enable the `spotInstanceDraining` property. ```ts +declare const cluster: ecs.Cluster; + // Add an AutoScalingGroup with spot instances to the existing cluster cluster.addCapacity('AsgSpot', { maxCapacity: 2, @@ -217,7 +231,8 @@ then you may do so by providing a KMS key for the `topicEncryptionKey` property ```ts // Given -const key = kms.Key(...); +declare const cluster: ecs.Cluster; +declare const key: kms.Key; // Then, use that key to encrypt the lifecycle-event SNS Topic. cluster.addCapacity('ASGEncryptedSNS', { instanceType: new ec2.InstanceType("t2.xlarge"), @@ -244,7 +259,7 @@ For a `FargateTaskDefinition`, specify the task size (`memoryLimitMiB` and `cpu` ```ts const fargateTaskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', { memoryLimitMiB: 512, - cpu: 256 + cpu: 256, }); ``` @@ -255,13 +270,17 @@ On Fargate Platform Version 1.4.0 or later, you may specify up to 200GiB of const fargateTaskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', { memoryLimitMiB: 512, cpu: 256, - ephemeralStorageGiB: 100 + ephemeralStorageGiB: 100, }); ``` To add containers to a task definition, call `addContainer()`: ```ts +const fargateTaskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', { + memoryLimitMiB: 512, + cpu: 256, +}); const container = fargateTaskDefinition.addContainer("WebContainer", { // Use an image from DockerHub image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"), @@ -273,13 +292,13 @@ For a `Ec2TaskDefinition`: ```ts const ec2TaskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef', { - networkMode: NetworkMode.BRIDGE + networkMode: ecs.NetworkMode.BRIDGE, }); const container = ec2TaskDefinition.addContainer("WebContainer", { // Use an image from DockerHub image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"), - memoryLimitMiB: 1024 + memoryLimitMiB: 1024, // ... other options here ... }); ``` @@ -292,7 +311,7 @@ const externalTaskDefinition = new ecs.ExternalTaskDefinition(this, 'TaskDef'); const container = externalTaskDefinition.addContainer("WebContainer", { // Use an image from DockerHub image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"), - memoryLimitMiB: 1024 + memoryLimitMiB: 1024, // ... other options here ... }); ``` @@ -302,34 +321,42 @@ You can specify container properties when you add them to the task definition, o To add a port mapping when adding a container to the task definition, specify the `portMappings` option: ```ts +declare const taskDefinition: ecs.TaskDefinition; + taskDefinition.addContainer("WebContainer", { image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"), memoryLimitMiB: 1024, - portMappings: [{ containerPort: 3000 }] + portMappings: [{ containerPort: 3000 }], }); ``` To add port mappings directly to a container definition, call `addPortMappings()`: ```ts +declare const container: ecs.ContainerDefinition; + container.addPortMappings({ - containerPort: 3000 + containerPort: 3000, }); ``` To add data volumes to a task definition, call `addVolume()`: ```ts +const fargateTaskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', { + memoryLimitMiB: 512, + cpu: 256, +}); const volume = { // Use an Elastic FileSystem name: "mydatavolume", - efsVolumeConfiguration: ecs.EfsVolumeConfiguration({ - fileSystemId: "EFS" + efsVolumeConfiguration: { + fileSystemId: "EFS", // ... other options here ... - }) + }, }; -const container = fargateTaskDefinition.addVolume("mydatavolume"); +const container = fargateTaskDefinition.addVolume(volume); ``` > Note: ECS Anywhere doesn't support volume attachments in the task definition. @@ -345,7 +372,7 @@ The following example uses both: const taskDefinition = new ecs.TaskDefinition(this, 'TaskDef', { memoryMiB: '512', cpu: '256', - networkMode: NetworkMode.AWS_VPC, + networkMode: ecs.NetworkMode.AWS_VPC, compatibility: ecs.Compatibility.EC2_AND_FARGATE, }); ``` @@ -372,6 +399,12 @@ obtained from either DockerHub or from ECR repositories, built directly from a l To pass environment variables to the container, you can use the `environment`, `environmentFiles`, and `secrets` props. ```ts +declare const secret: secretsmanager.Secret; +declare const dbSecret: secretsmanager.Secret; +declare const parameter: ssm.StringParameter; +declare const taskDefinition: ecs.TaskDefinition; +declare const s3Bucket: s3.Bucket; + taskDefinition.addContainer('container', { image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"), memoryLimitMiB: 1024, @@ -386,7 +419,7 @@ taskDefinition.addContainer('container', { SECRET: ecs.Secret.fromSecretsManager(secret), DB_PASSWORD: ecs.Secret.fromSecretsManager(dbSecret, 'password'), // Reference a specific JSON field, (requires platform version 1.4.0 or later for Fargate tasks) PARAMETER: ecs.Secret.fromSsmParameter(parameter), - } + }, }); ``` @@ -402,24 +435,26 @@ If a task fails, Amazon ECS automatically restarts the task. ```ts -const taskDefinition; +declare const cluster: ecs.Cluster; +declare const taskDefinition: ecs.TaskDefinition; const service = new ecs.FargateService(this, 'Service', { cluster, taskDefinition, - desiredCount: 5 + desiredCount: 5, }); ``` ECS Anywhere service definition looks like: ```ts -const taskDefinition; +declare const cluster: ecs.Cluster; +declare const taskDefinition: ecs.TaskDefinition; const service = new ecs.ExternalService(this, 'Service', { cluster, taskDefinition, - desiredCount: 5 + desiredCount: 5, }); ``` @@ -434,7 +469,9 @@ deployment circuit breaker and optionally enable `rollback` for automatic rollba for more details. ```ts -const service = new ecs.FargateService(stack, 'Service', { +declare const cluster: ecs.Cluster; +declare const taskDefinition: ecs.TaskDefinition; +const service = new ecs.FargateService(this, 'Service', { cluster, taskDefinition, circuitBreaker: { rollback: true }, @@ -448,22 +485,23 @@ const service = new ecs.FargateService(stack, 'Service', { `Services` are load balancing targets and can be added to a target group, which will be attached to an application/network load balancers: ```ts -import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2'; - -const service = new ecs.FargateService(this, 'Service', { /* ... */ }); +declare const vpc: ec2.Vpc; +declare const cluster: ecs.Cluster; +declare const taskDefinition: ecs.TaskDefinition; +const service = new ecs.FargateService(this, 'Service', { cluster, taskDefinition }); const lb = new elbv2.ApplicationLoadBalancer(this, 'LB', { vpc, internetFacing: true }); const listener = lb.addListener('Listener', { port: 80 }); const targetGroup1 = listener.addTargets('ECS1', { port: 80, - targets: [service] + targets: [service], }); const targetGroup2 = listener.addTargets('ECS2', { port: 80, targets: [service.loadBalancerTarget({ containerName: 'MyContainer', containerPort: 8080 - })] + })], }); ``` @@ -474,9 +512,10 @@ Note that in the example above, the default `service` only allows you to registe Alternatively, you can also create all load balancer targets to be registered in this service, add them to target groups, and attach target groups to listeners accordingly. ```ts -import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2'; - -const service = new ecs.FargateService(this, 'Service', { /* ... */ }); +declare const cluster: ecs.Cluster; +declare const taskDefinition: ecs.TaskDefinition; +declare const vpc: ec2.Vpc; +const service = new ecs.FargateService(this, 'Service', { cluster, taskDefinition }); const lb = new elbv2.ApplicationLoadBalancer(this, 'LB', { vpc, internetFacing: true }); const listener = lb.addListener('Listener', { port: 80 }); @@ -512,11 +551,12 @@ for the alternatives. `Services` can also be directly attached to a classic load balancer as targets: ```ts -import * as elb from '@aws-cdk/aws-elasticloadbalancing'; - -const service = new ecs.Ec2Service(this, 'Service', { /* ... */ }); +declare const cluster: ecs.Cluster; +declare const taskDefinition: ecs.TaskDefinition; +declare const vpc: ec2.Vpc; +const service = new ecs.Ec2Service(this, 'Service', { cluster, taskDefinition }); -const lb = new elb.LoadBalancer(stack, 'LB', { vpc }); +const lb = new elb.LoadBalancer(this, 'LB', { vpc }); lb.addListener({ externalPort: 80 }); lb.addTarget(service); ``` @@ -524,15 +564,16 @@ lb.addTarget(service); Similarly, if you want to have more control over load balancer targeting: ```ts -import * as elb from '@aws-cdk/aws-elasticloadbalancing'; +declare const cluster: ecs.Cluster; +declare const taskDefinition: ecs.TaskDefinition; +declare const vpc: ec2.Vpc; +const service = new ecs.Ec2Service(this, 'Service', { cluster, taskDefinition }); -const service = new ecs.Ec2Service(this, 'Service', { /* ... */ }); - -const lb = new elb.LoadBalancer(stack, 'LB', { vpc }); +const lb = new elb.LoadBalancer(this, 'LB', { vpc }); lb.addListener({ externalPort: 80 }); lb.addTarget(service.loadBalancerTarget({ containerName: 'MyContainer', - containerPort: 80 + containerPort: 80, })); ``` @@ -547,15 +588,17 @@ You can configure the task count of a service to match demand. Task auto-scaling configured by calling `autoScaleTaskCount()`: ```ts +declare const target: elbv2.ApplicationTargetGroup; +declare const service: ecs.BaseService; const scaling = service.autoScaleTaskCount({ maxCapacity: 10 }); scaling.scaleOnCpuUtilization('CpuScaling', { - targetUtilizationPercent: 50 + targetUtilizationPercent: 50, }); scaling.scaleOnRequestCount('RequestScaling', { requestsPerTarget: 10000, - targetGroup: target -}) + targetGroup: target, +}); ``` Task auto-scaling is powered by *Application Auto-Scaling*. @@ -567,19 +610,18 @@ To start an Amazon ECS task on an Amazon EC2-backed Cluster, instantiate an `@aws-cdk/aws-events-targets.EcsTask` instead of an `Ec2Service`: ```ts -import * as targets from '@aws-cdk/aws-events-targets'; - +declare const cluster: ecs.Cluster; // Create a Task Definition for the container to start const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef'); taskDefinition.addContainer('TheContainer', { image: ecs.ContainerImage.fromAsset(path.resolve(__dirname, '..', 'eventhandler-image')), memoryLimitMiB: 256, - logging: new ecs.AwsLogDriver({ streamPrefix: 'EventDemo', mode: AwsLogDriverMode.NON_BLOCKING }) + logging: new ecs.AwsLogDriver({ streamPrefix: 'EventDemo', mode: ecs.AwsLogDriverMode.NON_BLOCKING }), }); // An Rule that describes the event trigger (in this case a scheduled run) const rule = new events.Rule(this, 'Rule', { - schedule: events.Schedule.expression('rate(1 min)') + schedule: events.Schedule.expression('rate(1 min)'), }); // Pass an environment variable to the container 'TheContainer' in the task @@ -592,8 +634,8 @@ rule.addTarget(new targets.EcsTask({ environment: [{ name: 'I_WAS_TRIGGERED', value: 'From CloudWatch Events' - }] - }] + }], + }], })); ``` @@ -609,6 +651,7 @@ Currently Supported Log Drivers: - splunk - syslog - awsfirelens +- Generic ### awslogs Log Driver @@ -618,7 +661,7 @@ const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef'); taskDefinition.addContainer('TheContainer', { image: ecs.ContainerImage.fromRegistry('example-image'), memoryLimitMiB: 256, - logging: ecs.LogDrivers.awsLogs({ streamPrefix: 'EventDemo' }) + logging: ecs.LogDrivers.awsLogs({ streamPrefix: 'EventDemo' }), }); ``` @@ -630,7 +673,7 @@ const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef'); taskDefinition.addContainer('TheContainer', { image: ecs.ContainerImage.fromRegistry('example-image'), memoryLimitMiB: 256, - logging: ecs.LogDrivers.fluentd() + logging: ecs.LogDrivers.fluentd(), }); ``` @@ -642,7 +685,7 @@ const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef'); taskDefinition.addContainer('TheContainer', { image: ecs.ContainerImage.fromRegistry('example-image'), memoryLimitMiB: 256, - logging: ecs.LogDrivers.gelf({ address: 'my-gelf-address' }) + logging: ecs.LogDrivers.gelf({ address: 'my-gelf-address' }), }); ``` @@ -654,7 +697,7 @@ const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef'); taskDefinition.addContainer('TheContainer', { image: ecs.ContainerImage.fromRegistry('example-image'), memoryLimitMiB: 256, - logging: ecs.LogDrivers.journald() + logging: ecs.LogDrivers.journald(), }); ``` @@ -666,7 +709,7 @@ const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef'); taskDefinition.addContainer('TheContainer', { image: ecs.ContainerImage.fromRegistry('example-image'), memoryLimitMiB: 256, - logging: ecs.LogDrivers.jsonFile() + logging: ecs.LogDrivers.jsonFile(), }); ``` @@ -679,9 +722,9 @@ taskDefinition.addContainer('TheContainer', { image: ecs.ContainerImage.fromRegistry('example-image'), memoryLimitMiB: 256, logging: ecs.LogDrivers.splunk({ - secretToken: cdk.SecretValue.secretsManager('my-splunk-token'), - url: 'my-splunk-url' - }) + token: SecretValue.secretsManager('my-splunk-token'), + url: 'my-splunk-url', + }), }); ``` @@ -693,7 +736,7 @@ const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef'); taskDefinition.addContainer('TheContainer', { image: ecs.ContainerImage.fromRegistry('example-image'), memoryLimitMiB: 256, - logging: ecs.LogDrivers.syslog() + logging: ecs.LogDrivers.syslog(), }); ``` @@ -710,14 +753,17 @@ taskDefinition.addContainer('TheContainer', { Name: 'firehose', region: 'us-west-2', delivery_stream: 'my-stream', - } - }) + }, + }), }); ``` To pass secrets to the log configuration, use the `secretOptions` property of the log configuration. The task execution role is automatically granted read permissions on the secrets/parameters. ```ts +declare const secret: secretsmanager.Secret; +declare const parameter: ssm.StringParameter; + const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef'); taskDefinition.addContainer('TheContainer', { image: ecs.ContainerImage.fromRegistry('example-image'), @@ -730,7 +776,7 @@ taskDefinition.addContainer('TheContainer', { apikey: ecs.Secret.fromSecretsManager(secret), host: ecs.Secret.fromSsmParameter(parameter), }, - }) + }), }); ``` @@ -747,9 +793,9 @@ taskDefinition.addContainer('TheContainer', { logging: new ecs.GenericLogDriver({ logDriver: 'fluentd', options: { - tag: 'example-tag' - } - }) + tag: 'example-tag', + }, + }), }); ``` @@ -759,7 +805,10 @@ To register your ECS service with a CloudMap Service Registry, you may add the `cloudMapOptions` property to your service: ```ts -const service = new ecs.Ec2Service(stack, 'Service', { +declare const taskDefinition: ecs.TaskDefinition; +declare const cluster: ecs.Cluster; + +const service = new ecs.Ec2Service(this, 'Service', { cluster, taskDefinition, cloudMapOptions: { @@ -774,8 +823,14 @@ By default, `SRV` DNS record types will target the default container and default port. However, you may target a different container and port on the same ECS task: ```ts +declare const taskDefinition: ecs.TaskDefinition; +declare const cluster: ecs.Cluster; + // Add a container to the task definition -const specificContainer = taskDefinition.addContainer(...); +const specificContainer = taskDefinition.addContainer('Container', { + image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'), + memoryLimitMiB: 2048, +}); // Add a port mapping specificContainer.addPortMappings({ @@ -783,7 +838,7 @@ specificContainer.addPortMappings({ protocol: ecs.Protocol.TCP, }); -new ecs.Ec2Service(stack, 'Service', { +new ecs.Ec2Service(this, 'Service', { cluster, taskDefinition, cloudMapOptions: { @@ -802,8 +857,8 @@ You may associate an ECS service with a specific CloudMap service. To do this, use the service's `associateCloudMapService` method: ```ts -const cloudMapService = new cloudmap.Service(...); -const ecsService = new ecs.FargateService(...); +declare const cloudMapService: cloudmap.Service; +declare const ecsService: ecs.FargateService; ecsService.associateCloudMapService({ service: cloudMapService, @@ -827,18 +882,20 @@ cluster. This will add both `FARGATE` and `FARGATE_SPOT` as available capacity providers on your cluster. ```ts -const cluster = new ecs.Cluster(stack, 'FargateCPCluster', { +declare const vpc: ec2.Vpc; + +const cluster = new ecs.Cluster(this, 'FargateCPCluster', { vpc, enableFargateCapacityProviders: true, }); -const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef'); +const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef'); taskDefinition.addContainer('web', { image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'), }); -new ecs.FargateService(stack, 'FargateService', { +new ecs.FargateService(this, 'FargateService', { cluster, taskDefinition, capacityProviderStrategies: [ @@ -849,7 +906,7 @@ new ecs.FargateService(stack, 'FargateService', { { capacityProvider: 'FARGATE', weight: 1, - } + }, ], }); ``` @@ -869,11 +926,13 @@ running on them. If you want to disable this behavior, set both `enableManagedScaling` to and `enableManagedTerminationProtection` to `false`. ```ts -const cluster = new ecs.Cluster(stack, 'Cluster', { +declare const vpc: ec2.Vpc; + +const cluster = new ecs.Cluster(this, 'Cluster', { vpc, }); -const autoScalingGroup = new autoscaling.AutoScalingGroup(stack, 'ASG', { +const autoScalingGroup = new autoscaling.AutoScalingGroup(this, 'ASG', { vpc, instanceType: new ec2.InstanceType('t2.micro'), machineImage: ecs.EcsOptimizedImage.amazonLinux2(), @@ -881,26 +940,26 @@ const autoScalingGroup = new autoscaling.AutoScalingGroup(stack, 'ASG', { maxCapacity: 100, }); -const capacityProvider = new ecs.AsgCapacityProvider(stack, 'AsgCapacityProvider', { +const capacityProvider = new ecs.AsgCapacityProvider(this, 'AsgCapacityProvider', { autoScalingGroup, }); cluster.addAsgCapacityProvider(capacityProvider); -const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef'); +const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef'); taskDefinition.addContainer('web', { image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'), memoryReservationMiB: 256, }); -new ecs.Ec2Service(stack, 'EC2Service', { +new ecs.Ec2Service(this, 'EC2Service', { cluster, taskDefinition, capacityProviderStrategies: [ { capacityProvider: capacityProvider.capacityProviderName, weight: 1, - } + }, ], }); ``` @@ -919,7 +978,7 @@ const inferenceAccelerators = [{ deviceType: 'eia2.medium', }]; -const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef', { +const taskDefinition = new ecs.Ec2TaskDefinition(this, 'Ec2TaskDef', { inferenceAccelerators, }); ``` @@ -929,6 +988,7 @@ field and set it to a list of device names used for the inference accelerators. list should match a `DeviceName` for an `InferenceAccelerator` specified in the task definition. ```ts +declare const taskDefinition: ecs.TaskDefinition; const inferenceAcceleratorResources = ['device1']; taskDefinition.addContainer('cont', { @@ -948,7 +1008,10 @@ To enable the ECS Exec feature for your containers, set the boolean flag `enable your `Ec2Service` or `FargateService`. ```ts -const service = new ecs.Ec2Service(stack, 'Service', { +declare const cluster: ecs.Cluster; +declare const taskDefinition: ecs.TaskDefinition; + +const service = new ecs.Ec2Service(this, 'Service', { cluster, taskDefinition, enableExecuteCommand: true, @@ -967,19 +1030,20 @@ of the `executeCommandConfiguration`. To use this key for encrypting CloudWatch to these resources on creation. ```ts -const kmsKey = new kms.Key(stack, 'KmsKey'); +declare const vpc: ec2.Vpc; +const kmsKey = new kms.Key(this, 'KmsKey'); // Pass the KMS key in the `encryptionKey` field to associate the key to the log group -const logGroup = new logs.LogGroup(stack, 'LogGroup', { +const logGroup = new logs.LogGroup(this, 'LogGroup', { encryptionKey: kmsKey, }); // Pass the KMS key in the `encryptionKey` field to associate the key to the S3 bucket -const execBucket = new s3.Bucket(stack, 'EcsExecBucket', { +const execBucket = new s3.Bucket(this, 'EcsExecBucket', { encryptionKey: kmsKey, }); -const cluster = new ecs.Cluster(stack, 'Cluster', { +const cluster = new ecs.Cluster(this, 'Cluster', { vpc, executeCommandConfiguration: { kmsKey, diff --git a/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts b/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts index ce5097e9ebccf..6bac2c663b82d 100644 --- a/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts +++ b/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts @@ -570,6 +570,8 @@ export abstract class BaseService extends Resource * * @example * + * declare const listener: elbv2.ApplicationListener; + * declare const service: ecs.BaseService; * listener.addTargets('ECS', { * port: 80, * targets: [service.loadBalancerTarget({ @@ -605,6 +607,8 @@ export abstract class BaseService extends Resource * * @example * + * declare const listener: elbv2.ApplicationListener; + * declare const service: ecs.BaseService; * service.registerLoadBalancerTargets( * { * containerName: 'web', diff --git a/packages/@aws-cdk/aws-ecs/lib/cluster.ts b/packages/@aws-cdk/aws-ecs/lib/cluster.ts index 49d99bf68925d..cc97f4aadfafe 100644 --- a/packages/@aws-cdk/aws-ecs/lib/cluster.ts +++ b/packages/@aws-cdk/aws-ecs/lib/cluster.ts @@ -821,11 +821,9 @@ export interface AddCapacityOptions extends AddAutoScalingGroupCapacityOptions, * To use an image that does not update on every deployment, pass: * * ```ts - * { - * machineImage: EcsOptimizedImage.amazonLinux2(AmiHardwareType.STANDARD, { - * cachedInContext: true, - * }), - * } + * const machineImage = ecs.EcsOptimizedImage.amazonLinux2(ecs.AmiHardwareType.STANDARD, { + * cachedInContext: true, + * }); * ``` * * For more information, see [Amazon ECS-optimized diff --git a/packages/@aws-cdk/aws-ecs/lib/index.ts b/packages/@aws-cdk/aws-ecs/lib/index.ts index bd076ccfd05f7..09d355dd18b38 100644 --- a/packages/@aws-cdk/aws-ecs/lib/index.ts +++ b/packages/@aws-cdk/aws-ecs/lib/index.ts @@ -36,6 +36,7 @@ export * from './log-drivers/json-file-log-driver'; export * from './log-drivers/splunk-log-driver'; export * from './log-drivers/syslog-log-driver'; export * from './log-drivers/log-driver'; +export * from './log-drivers/generic-log-driver'; export * from './log-drivers/log-drivers'; export * from './proxy-configuration/app-mesh-proxy-configuration'; diff --git a/packages/@aws-cdk/aws-ecs/lib/log-drivers/generic-log-driver.ts b/packages/@aws-cdk/aws-ecs/lib/log-drivers/generic-log-driver.ts index 9e356a8cd3c19..8181b7d689d44 100644 --- a/packages/@aws-cdk/aws-ecs/lib/log-drivers/generic-log-driver.ts +++ b/packages/@aws-cdk/aws-ecs/lib/log-drivers/generic-log-driver.ts @@ -1,5 +1,5 @@ import { ContainerDefinition, Secret } from '../container-definition'; -import { LogDriver, LogDriverConfig } from '../index'; +import { LogDriver, LogDriverConfig } from './log-driver'; import { removeEmpty, renderLogDriverSecretOptions } from './utils'; // v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch. diff --git a/packages/@aws-cdk/aws-ecs/rosetta/default.ts-fixture b/packages/@aws-cdk/aws-ecs/rosetta/default.ts-fixture new file mode 100644 index 0000000000000..2cc599faf6c3f --- /dev/null +++ b/packages/@aws-cdk/aws-ecs/rosetta/default.ts-fixture @@ -0,0 +1,24 @@ +// Fixture with packages imported, but nothing else +import { Construct, SecretValue, Stack } from '@aws-cdk/core'; +import autoscaling = require('@aws-cdk/aws-autoscaling'); +import cloudmap = require('@aws-cdk/aws-servicediscovery'); +import ecs = require('@aws-cdk/aws-ecs'); +import ec2 = require('@aws-cdk/aws-ec2'); +import elb = require('@aws-cdk/aws-elasticloadbalancing'); +import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2'); +import events = require('@aws-cdk/aws-events'); +import kms = require('@aws-cdk/aws-kms'); +import logs = require('@aws-cdk/aws-logs'); +import s3 = require('@aws-cdk/aws-s3'); +import secretsmanager = require('@aws-cdk/aws-secretsmanager'); +import ssm = require('@aws-cdk/aws-ssm'); +import targets = require('@aws-cdk/aws-events-targets'); +import path = require('path'); + +class Fixture extends Stack { + constructor(scope: Construct, id: string) { + super(scope, id); + + /// here + } +} From 93b3fdce80f0997d7b809f9ef7e3edd1e75e1f42 Mon Sep 17 00:00:00 2001 From: Neil Kuan <46012524+neilkuan@users.noreply.github.com> Date: Wed, 13 Oct 2021 00:55:01 +0800 Subject: [PATCH 6/6] fix(aws-ecs): add ASG capacity via Capacity Provider by not specifying machineImageType (#16361) fix(aws-ecs): make `Cluster.addAsgCapacityProvider()` not need specify `machineImageType` close #16360 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/aws-ecs/lib/cluster.ts | 11 +- .../@aws-cdk/aws-ecs/test/cluster.test.ts | 104 ++++++++++++++++++ .../aws-ecs/test/container-definition.test.ts | 2 +- .../test/ec2/ec2-task-definition.test.ts | 2 +- 4 files changed, 115 insertions(+), 4 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs/lib/cluster.ts b/packages/@aws-cdk/aws-ecs/lib/cluster.ts index cc97f4aadfafe..aae5d320b20af 100644 --- a/packages/@aws-cdk/aws-ecs/lib/cluster.ts +++ b/packages/@aws-cdk/aws-ecs/lib/cluster.ts @@ -324,15 +324,15 @@ export class Cluster extends Resource implements ICluster { * * @param provider the capacity provider to add to this cluster. */ - public addAsgCapacityProvider(provider: AsgCapacityProvider, options: AddAutoScalingGroupCapacityOptions = {}) { + public addAsgCapacityProvider(provider: AsgCapacityProvider, options: AddAutoScalingGroupCapacityOptions= {}) { // Don't add the same capacity provider more than once. if (this._capacityProviderNames.includes(provider.capacityProviderName)) { return; } - this._hasEc2Capacity = true; this.configureAutoScalingGroup(provider.autoScalingGroup, { ...options, + machineImageType: provider.machineImageType, // Don't enable the instance-draining lifecycle hook if managed termination protection is enabled taskDrainTime: provider.enableManagedTerminationProtection ? Duration.seconds(0) : options.taskDrainTime, }); @@ -1062,6 +1062,11 @@ export class AsgCapacityProvider extends CoreConstruct { */ readonly autoScalingGroup: autoscaling.AutoScalingGroup; + /** + * Auto Scaling Group machineImageType. + */ + readonly machineImageType: MachineImageType; + /** * Whether managed termination protection is enabled */ @@ -1072,6 +1077,8 @@ export class AsgCapacityProvider extends CoreConstruct { this.autoScalingGroup = props.autoScalingGroup as autoscaling.AutoScalingGroup; + this.machineImageType = props.machineImageType ?? MachineImageType.AMAZON_LINUX_2; + this.enableManagedTerminationProtection = props.enableManagedTerminationProtection === undefined ? true : props.enableManagedTerminationProtection; diff --git a/packages/@aws-cdk/aws-ecs/test/cluster.test.ts b/packages/@aws-cdk/aws-ecs/test/cluster.test.ts index 52de16b353137..594a59d0380a0 100644 --- a/packages/@aws-cdk/aws-ecs/test/cluster.test.ts +++ b/packages/@aws-cdk/aws-ecs/test/cluster.test.ts @@ -2142,3 +2142,107 @@ describe('cluster', () => { }); }); + +test('can add ASG capacity via Capacity Provider by not specifying machineImageType', () => { + // GIVEN + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'test'); + const vpc = new ec2.Vpc(stack, 'Vpc'); + const cluster = new ecs.Cluster(stack, 'EcsCluster'); + + const autoScalingGroupAl2 = new autoscaling.AutoScalingGroup(stack, 'asgal2', { + vpc, + instanceType: new ec2.InstanceType('bogus'), + machineImage: ecs.EcsOptimizedImage.amazonLinux2(), + }); + + const autoScalingGroupBottlerocket = new autoscaling.AutoScalingGroup(stack, 'asgBottlerocket', { + vpc, + instanceType: new ec2.InstanceType('bogus'), + machineImage: new ecs.BottleRocketImage(), + }); + + // WHEN + const capacityProviderAl2 = new ecs.AsgCapacityProvider(stack, 'provideral2', { + autoScalingGroup: autoScalingGroupAl2, + enableManagedTerminationProtection: false, + }); + + const capacityProviderBottlerocket = new ecs.AsgCapacityProvider(stack, 'providerBottlerocket', { + autoScalingGroup: autoScalingGroupBottlerocket, + enableManagedTerminationProtection: false, + machineImageType: ecs.MachineImageType.BOTTLEROCKET, + }); + + cluster.enableFargateCapacityProviders(); + + // Ensure not added twice + cluster.addAsgCapacityProvider(capacityProviderAl2); + cluster.addAsgCapacityProvider(capacityProviderAl2); + + // Add Bottlerocket ASG Capacity Provider + cluster.addAsgCapacityProvider(capacityProviderBottlerocket); + + + // THEN Bottlerocket LaunchConfiguration + expect(stack).toHaveResource('AWS::AutoScaling::LaunchConfiguration', { + ImageId: { + Ref: 'SsmParameterValueawsservicebottlerocketawsecs1x8664latestimageidC96584B6F00A464EAD1953AFF4B05118Parameter', + + }, + UserData: { + 'Fn::Base64': { + 'Fn::Join': [ + '', + [ + '\n[settings.ecs]\ncluster = \"', + { + Ref: 'EcsCluster97242B84', + }, + '\"', + ], + ], + }, + }, + }); + + // THEN AmazonLinux2 LaunchConfiguration + expect(stack).toHaveResource('AWS::AutoScaling::LaunchConfiguration', { + ImageId: { + Ref: 'SsmParameterValueawsserviceecsoptimizedamiamazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter', + }, + UserData: { + 'Fn::Base64': { + 'Fn::Join': [ + '', + [ + '#!/bin/bash\necho ECS_CLUSTER=', + { + Ref: 'EcsCluster97242B84', + + }, + ' >> /etc/ecs/ecs.config\nsudo iptables --insert FORWARD 1 --in-interface docker+ --destination 169.254.169.254/32 --jump DROP\nsudo service iptables save\necho ECS_AWSVPC_BLOCK_IMDS=true >> /etc/ecs/ecs.config', + ], + ], + }, + }, + }); + + expect(stack).toHaveResource('AWS::ECS::ClusterCapacityProviderAssociations', { + CapacityProviders: [ + 'FARGATE', + 'FARGATE_SPOT', + { + Ref: 'provideral2A427CBC0', + }, + { + Ref: 'providerBottlerocket90C039FA', + }, + ], + Cluster: { + Ref: 'EcsCluster97242B84', + }, + DefaultCapacityProviderStrategy: [], + }); + +}); \ No newline at end of file diff --git a/packages/@aws-cdk/aws-ecs/test/container-definition.test.ts b/packages/@aws-cdk/aws-ecs/test/container-definition.test.ts index 524551aba1286..a5153b82d331a 100644 --- a/packages/@aws-cdk/aws-ecs/test/container-definition.test.ts +++ b/packages/@aws-cdk/aws-ecs/test/container-definition.test.ts @@ -5,9 +5,9 @@ import * as ecr_assets from '@aws-cdk/aws-ecr-assets'; import * as s3 from '@aws-cdk/aws-s3'; import * as secretsmanager from '@aws-cdk/aws-secretsmanager'; import * as ssm from '@aws-cdk/aws-ssm'; +import { testFutureBehavior } from '@aws-cdk/cdk-build-tools/lib/feature-flag'; import * as cdk from '@aws-cdk/core'; import * as cxapi from '@aws-cdk/cx-api'; -import { testFutureBehavior } from '@aws-cdk/cdk-build-tools/lib/feature-flag'; import * as ecs from '../lib'; describe('container definition', () => { diff --git a/packages/@aws-cdk/aws-ecs/test/ec2/ec2-task-definition.test.ts b/packages/@aws-cdk/aws-ecs/test/ec2/ec2-task-definition.test.ts index 4f80d48118bc6..966473e5cc6cb 100644 --- a/packages/@aws-cdk/aws-ecs/test/ec2/ec2-task-definition.test.ts +++ b/packages/@aws-cdk/aws-ecs/test/ec2/ec2-task-definition.test.ts @@ -5,9 +5,9 @@ import { Repository } from '@aws-cdk/aws-ecr'; import * as iam from '@aws-cdk/aws-iam'; import * as secretsmanager from '@aws-cdk/aws-secretsmanager'; import * as ssm from '@aws-cdk/aws-ssm'; +import { testFutureBehavior } from '@aws-cdk/cdk-build-tools/lib/feature-flag'; import * as cdk from '@aws-cdk/core'; import * as cxapi from '@aws-cdk/cx-api'; -import { testFutureBehavior } from '@aws-cdk/cdk-build-tools/lib/feature-flag'; import * as ecs from '../../lib'; describe('ec2 task definition', () => {