import * as ec2 from '@aws-cdk/aws-ec2' ; import { Policy , PolicyInterface } from '../../lib/policy' ; import { Reportable } from '../../lib/reporter' ; export interface Ec2InstanceTypePolicyConfig { instanceClasses ? : ec2 . InstanceClass [ ] ; instanceSizes ? : ec2 . InstanceSize [ ] ; } cloudformationResource see https export class Ec2InstanceTypePolicy extends Policy implements PolicyInterface { public policyName = 'Ec2InstanceType' ; public description = 'This Policy ensures that we\'re using instances of a certain class' ; public link = 'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#cfn-ec2-instance-instancetype' ; public scope = ec2 . CfnInstance ; public instanceClasses ? : ec2 . InstanceClass [ ] ; public instanceSizes ? : ec2 . InstanceSize [ ] ; constructor ( public config ) { super ( ) ; this . instanceClasses = config . instanceClasses ; this . instanceSizes = config . instanceSizes ; } public validator ( node , reporter ) { if ( this . instanceTypes ( ) . find ( instanceType => node . instanceType ?. includes ( instanceType ) ) === undefined ) { reporter . addInfo ( node , this , `Please consider using instances from the following types ${ this . instanceTypes ( ) }` ) ; } } private instanceTypes ( ) { if ( Array . isArray ( this . instanceClasses ) ) { return this . instanceClasses . flatMap ( c => ( ( this . instanceSizes && this . instanceSizes . map ( s => `${ c }.${ s }` ) || [ `${ c }.` ] ) ) ) ; } else if ( Array . isArray ( this . instanceSizes ) ) { return this . instanceSizes ; } else { return [ ] ; } } }