@@ -12,10 +12,8 @@ import {
12
12
TagType ,
13
13
Tags ,
14
14
Token ,
15
- Aspects ,
16
15
} from '@aws-cdk/core' ;
17
16
import { Construct } from 'constructs' ;
18
- import { LaunchTemplateRequireImdsv2Aspect } from '.' ;
19
17
import { Connections , IConnectable } from './connections' ;
20
18
import { CfnLaunchTemplate } from './ec2.generated' ;
21
19
import { InstanceType } from './instance-types' ;
@@ -193,6 +191,23 @@ export interface LaunchTemplateSpotOptions {
193
191
readonly validUntil ?: Expiration ;
194
192
} ;
195
193
194
+ /**
195
+ * The state of token usage for your instance metadata requests.
196
+ *
197
+ * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-launchtemplatedata-metadataoptions.html#cfn-ec2-launchtemplate-launchtemplatedata-metadataoptions-httptokens
198
+ */
199
+ export enum LaunchTemplateHttpTokens {
200
+ /**
201
+ * If the state is optional, you can choose to retrieve instance metadata with or without a signed token header on your request.
202
+ */
203
+ OPTIONAL = 'optional' ,
204
+ /**
205
+ * If the state is required, you must send a signed token header with any instance metadata retrieval requests. In this state,
206
+ * retrieving the IAM role credentials always returns the version 2.0 credentials; the version 1.0 credentials are not available.
207
+ */
208
+ REQUIRED = 'required' ,
209
+ }
210
+
196
211
/**
197
212
* Properties of a LaunchTemplate.
198
213
*/
@@ -341,6 +356,52 @@ export interface LaunchTemplateProps {
341
356
* @default - false
342
357
*/
343
358
readonly requireImdsv2 ?: boolean ;
359
+
360
+ /**
361
+ * Enables or disables the HTTP metadata endpoint on your instances.
362
+ *
363
+ * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-launchtemplatedata-metadataoptions.html#cfn-ec2-launchtemplate-launchtemplatedata-metadataoptions-httpendpoint
364
+ *
365
+ * @default true
366
+ */
367
+ readonly httpEndpoint ?: boolean ;
368
+
369
+ /**
370
+ * Enables or disables the IPv6 endpoint for the instance metadata service.
371
+ *
372
+ * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-launchtemplatedata-metadataoptions.html#cfn-ec2-launchtemplate-launchtemplatedata-metadataoptions-httpprotocolipv6
373
+ *
374
+ * @default true
375
+ */
376
+ readonly httpProtocolIpv6 ?: boolean ;
377
+
378
+ /**
379
+ * The desired HTTP PUT response hop limit for instance metadata requests. The larger the number, the further instance metadata requests can travel.
380
+ *
381
+ * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-launchtemplatedata-metadataoptions.html#cfn-ec2-launchtemplate-launchtemplatedata-metadataoptions-httpputresponsehoplimit
382
+ *
383
+ * @default 1
384
+ */
385
+ readonly httpPutResponseHopLimit ?: number ;
386
+
387
+ /**
388
+ * The state of token usage for your instance metadata requests. The default state is `optional` if not specified. However,
389
+ * if requireImdsv2 is true, the state must be `required`.
390
+ *
391
+ * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-launchtemplatedata-metadataoptions.html#cfn-ec2-launchtemplate-launchtemplatedata-metadataoptions-httptokens
392
+ *
393
+ * @default LaunchTemplateHttpTokens.OPTIONAL
394
+ */
395
+ readonly httpTokens ?: LaunchTemplateHttpTokens ;
396
+
397
+ /**
398
+ * Set to enabled to allow access to instance tags from the instance metadata. Set to disabled to turn off access to instance tags from the instance metadata.
399
+ *
400
+ * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-launchtemplatedata-metadataoptions.html#cfn-ec2-launchtemplate-launchtemplatedata-metadataoptions-instancemetadatatags
401
+ *
402
+ * @default false
403
+ */
404
+ readonly instanceMetadataTags ?: boolean ;
344
405
}
345
406
346
407
/**
@@ -506,6 +567,12 @@ export class LaunchTemplate extends Resource implements ILaunchTemplate, iam.IGr
506
567
Annotations . of ( this ) . addError ( 'Spot block duration must be exactly 1, 2, 3, 4, 5, or 6 hours.' ) ;
507
568
}
508
569
570
+ // Basic validation of the provided httpPutResponseHopLimit
571
+ if ( props . httpPutResponseHopLimit !== undefined && ( props . httpPutResponseHopLimit < 1 || props . httpPutResponseHopLimit > 64 ) ) {
572
+ // See: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-launchtemplatedata-metadataoptions.html#cfn-ec2-launchtemplate-launchtemplatedata-metadataoptions-httpputresponsehoplimit
573
+ Annotations . of ( this ) . addError ( 'HttpPutResponseHopLimit must between 1 and 64' ) ;
574
+ }
575
+
509
576
this . role = props . role ;
510
577
this . _grantPrincipal = this . role ;
511
578
const iamProfile : iam . CfnInstanceProfile | undefined = this . role ? new iam . CfnInstanceProfile ( this , 'Profile' , {
@@ -639,6 +706,7 @@ export class LaunchTemplate extends Resource implements ILaunchTemplate, iam.IGr
639
706
securityGroupIds : securityGroupsToken ,
640
707
tagSpecifications : tagsToken ,
641
708
userData : userDataToken ,
709
+ metadataOptions : this . renderMetadataOptions ( props ) ,
642
710
643
711
// Fields not yet implemented:
644
712
// ==========================
@@ -663,9 +731,6 @@ export class LaunchTemplate extends Resource implements ILaunchTemplate, iam.IGr
663
731
// Also not implemented in Instance L2
664
732
// licenseSpecifications: undefined,
665
733
666
- // https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-launchtemplatedata.html#cfn-ec2-launchtemplate-launchtemplatedata-metadataoptions
667
- // metadataOptions: undefined,
668
-
669
734
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-launchtemplatedata.html#cfn-ec2-launchtemplate-launchtemplatedata-tagspecifications
670
735
// Should be implemented via the Tagging aspect in CDK core. Complication will be that this tagging interface is very unique to LaunchTemplates.
671
736
// tagSpecification: undefined
@@ -686,9 +751,31 @@ export class LaunchTemplate extends Resource implements ILaunchTemplate, iam.IGr
686
751
this . latestVersionNumber = resource . attrLatestVersionNumber ;
687
752
this . launchTemplateId = resource . ref ;
688
753
this . versionNumber = Token . asString ( resource . getAtt ( 'LatestVersionNumber' ) ) ;
754
+ }
689
755
690
- if ( props . requireImdsv2 ) {
691
- Aspects . of ( this ) . add ( new LaunchTemplateRequireImdsv2Aspect ( ) ) ;
756
+ private renderMetadataOptions ( props : LaunchTemplateProps ) {
757
+ let requireMetadataOptions = false ;
758
+ // if requireImdsv2 is true, httpTokens must be required.
759
+ if ( props . requireImdsv2 === true && props . httpTokens === LaunchTemplateHttpTokens . OPTIONAL ) {
760
+ Annotations . of ( this ) . addError ( 'httpTokens must be required when requireImdsv2 is true' ) ;
761
+ }
762
+ if ( props . httpEndpoint !== undefined || props . httpProtocolIpv6 !== undefined || props . httpPutResponseHopLimit !== undefined ||
763
+ props . httpTokens !== undefined || props . instanceMetadataTags !== undefined || props . requireImdsv2 === true ) {
764
+ requireMetadataOptions = true ;
765
+ }
766
+ if ( requireMetadataOptions ) {
767
+ return {
768
+ httpEndpoint : props . httpEndpoint === true ? 'enabled' :
769
+ props . httpEndpoint === false ? 'disabled' : undefined ,
770
+ httpProtocolIpv6 : props . httpProtocolIpv6 === true ? 'enabled' :
771
+ props . httpProtocolIpv6 === false ? 'disabled' : undefined ,
772
+ httpPutResponseHopLimit : props . httpPutResponseHopLimit ,
773
+ httpTokens : props . requireImdsv2 === true ? LaunchTemplateHttpTokens . REQUIRED : props . httpTokens ,
774
+ instanceMetadataTags : props . instanceMetadataTags === true ? 'enabled' :
775
+ props . instanceMetadataTags === false ? 'disabled' : undefined ,
776
+ } ;
777
+ } else {
778
+ return undefined ;
692
779
}
693
780
}
694
781
0 commit comments