diff --git a/packages/@aws-cdk/aws-autoscaling/README.md b/packages/@aws-cdk/aws-autoscaling/README.md index d1a75f16e493b..9d4924cc1b547 100644 --- a/packages/@aws-cdk/aws-autoscaling/README.md +++ b/packages/@aws-cdk/aws-autoscaling/README.md @@ -383,9 +383,20 @@ new autoscaling.AutoScalingGroup(stack, 'ASG', { ### Toggling IMDSv1 You can configure [EC2 Instance Metadata Service](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) options to either -allow both IMDSv1 and IMDSv2 or enforce IMDSv2 when interacting with the IMDS. To do this, you can use the `AutoScalingGroupImdsAspect`. +allow both IMDSv1 and IMDSv2 or enforce IMDSv2 when interacting with the IMDS. -The following example demonstrates how to use the `AutoScalingGroupImdsAspect` to disable IMDSv1 (thus enforcing IMDSv2) for all AutoScalingGroups in a stack: +To do this for a single `AutoScalingGroup`, you can use set the `disableImdsv1` property. +The example below demonstrates IMDSv1 being disabled on a single `AutoScalingGroup`: + +```ts +new autoscaling.AutoScalingGroup(stack, 'ASG', { + disableImdsv1: true, + // ... +}); +``` + +You can also use `AutoScalingGroupImdsAspect` to apply the operation to multiple AutoScalingGroups. +The example below demonstrates the `AutoScalingGroupImdsAspect` being used to disable IMDSv1 for all AutoScalingGroups in a stack: ```ts const aspect = new autoscaling.AutoScalingGroupImdsAspect({ diff --git a/packages/@aws-cdk/aws-autoscaling/lib/aspects/imds-aspect.ts b/packages/@aws-cdk/aws-autoscaling/lib/aspects/imds-aspect.ts index 184343a89f8ab..17e9ff1551c4f 100644 --- a/packages/@aws-cdk/aws-autoscaling/lib/aspects/imds-aspect.ts +++ b/packages/@aws-cdk/aws-autoscaling/lib/aspects/imds-aspect.ts @@ -10,13 +10,6 @@ interface ImdsAspectProps { * Whether IMDSv1 should be enabled or not. */ readonly enableImdsV1: boolean; - - /** - * Whether warning annotations from this Aspect should be suppressed or not. - * - * @default - false - */ - readonly suppressWarnings?: boolean; } /** @@ -24,25 +17,21 @@ interface ImdsAspectProps { */ abstract class ImdsAspect implements cdk.IAspect { protected readonly enableImdsV1: boolean; - protected readonly suppressWarnings: boolean; constructor(props: ImdsAspectProps) { this.enableImdsV1 = props.enableImdsV1; - this.suppressWarnings = props.suppressWarnings ?? false; } abstract visit(node: cdk.IConstruct): void; /** - * Adds a warning annotation to a node, unless `suppressWarnings` is true. + * Adds a warning annotation to a node. * * @param node The scope to add the warning to. * @param message The warning message. */ protected warn(node: cdk.IConstruct, message: string) { - if (this.suppressWarnings !== true) { - cdk.Annotations.of(node).addWarning(`${ImdsAspect.name} failed on node ${node.node.id}: ${message}`); - } + cdk.Annotations.of(node).addWarning(`${ImdsAspect.name} failed on node ${node.node.id}: ${message}`); } } diff --git a/packages/@aws-cdk/aws-autoscaling/package.json b/packages/@aws-cdk/aws-autoscaling/package.json index 7c1b1149922fe..53d2bb7d6db51 100644 --- a/packages/@aws-cdk/aws-autoscaling/package.json +++ b/packages/@aws-cdk/aws-autoscaling/package.json @@ -72,17 +72,15 @@ }, "license": "Apache-2.0", "devDependencies": { - "@types/jest": "^26.0.24", - "@types/sinon": "^9.0.11", + "@aws-cdk/assert-internal": "0.0.0", + "@aws-cdk/cloud-assembly-schema": "0.0.0", "@aws-cdk/cx-api": "0.0.0", + "@types/jest": "^26.0.24", "cdk-build-tools": "0.0.0", "cdk-integ-tools": "0.0.0", "cfn2ts": "0.0.0", "jest": "^26.6.3", - "pkglint": "0.0.0", - "sinon": "^9.2.4", - "@aws-cdk/cloud-assembly-schema": "0.0.0", - "@aws-cdk/assert-internal": "0.0.0" + "pkglint": "0.0.0" }, "dependencies": { "@aws-cdk/aws-autoscaling-common": "0.0.0", diff --git a/packages/@aws-cdk/aws-autoscaling/test/aspects/imds-aspect.test.ts b/packages/@aws-cdk/aws-autoscaling/test/aspects/imds-aspect.test.ts index 879250a6374af..3e3a2d929f9f7 100644 --- a/packages/@aws-cdk/aws-autoscaling/test/aspects/imds-aspect.test.ts +++ b/packages/@aws-cdk/aws-autoscaling/test/aspects/imds-aspect.test.ts @@ -5,7 +5,6 @@ import { import '@aws-cdk/assert-internal/jest'; import * as ec2 from '@aws-cdk/aws-ec2'; import * as cdk from '@aws-cdk/core'; -import * as sinon from 'sinon'; import { AutoScalingGroup, AutoScalingGroupImdsAspect, @@ -23,31 +22,6 @@ describe('ImdsAspect', () => { vpc = new ec2.Vpc(stack, 'Vpc'); }); - test('suppresses warnings', () => { - // GIVEN - const aspect = new AutoScalingGroupImdsAspect({ - enableImdsV1: true, - suppressWarnings: true, - }); - const errmsg = 'ERROR'; - const stub = sinon.stub(aspect, 'visit').callsFake((node) => { - // @ts-ignore - aspect.warn(node, errmsg); - }); - const construct = new cdk.Construct(stack, 'Construct'); - - // WHEN - aspect.visit(construct); - - // THEN - expect(stub.calledOnce).toBeTruthy(); - expect(construct.node.metadataEntry).not.toContainEqual({ - data: expect.stringContaining(errmsg), - type: 'aws:cdk:warning', - trace: undefined, - }); - }); - describe('AutoScalingGroupImdsAspect', () => { test('warns when metadataOptions is a token', () => { // GIVEN