@@ -9,6 +9,7 @@ import * as s3 from 'aws-cdk-lib/aws-s3'
99import * as s3seeder from 'aws-cdk-lib/aws-s3-deployment'
1010import * as rds from 'aws-cdk-lib/aws-rds' ;
1111import * as ssm from 'aws-cdk-lib/aws-ssm' ;
12+ import * as kms from 'aws-cdk-lib/aws-kms' ;
1213import * as eks from 'aws-cdk-lib/aws-eks' ;
1314import * as yaml from 'js-yaml' ;
1415import * as path from 'path' ;
@@ -31,6 +32,8 @@ import { CfnJson, RemovalPolicy, Fn, Duration, Stack, StackProps, CfnOutput } fr
3132import { readFileSync } from 'fs' ;
3233import 'ts-replace-all'
3334import { TreatMissingData , ComparisonOperator } from 'aws-cdk-lib/aws-cloudwatch' ;
35+ import { KubectlLayer } from 'aws-cdk-lib/lambda-layer-kubectl' ;
36+ import { Cloud9Environment } from './modules/core/cloud9' ;
3437
3538export class Services extends Stack {
3639 constructor ( scope : Construct , id : string , props ?: StackProps ) {
@@ -109,7 +112,8 @@ export class Services extends Stack {
109112 }
110113 // The VPC where all the microservices will be deployed into
111114 const theVPC = new ec2 . Vpc ( this , 'Microservices' , {
112- cidr : cidrRange ,
115+ ipAddresses : ec2 . IpAddresses . cidr ( cidrRange ) ,
116+ // cidr: cidrRange,
113117 natGateways : 1 ,
114118 maxAzs : 2
115119 } ) ;
@@ -327,13 +331,16 @@ export class Services extends Stack {
327331 parameterName : '/eks/petsite/EKSMasterRoleArn'
328332 } )
329333
334+ const secretsKey = new kms . Key ( this , 'SecretsKey' ) ;
330335 const cluster = new eks . Cluster ( this , 'petsite' , {
331336 clusterName : 'PetSite' ,
332337 mastersRole : clusterAdmin ,
333338 vpc : theVPC ,
334339 defaultCapacity : 2 ,
335340 defaultCapacityInstance : ec2 . InstanceType . of ( ec2 . InstanceClass . T3 , ec2 . InstanceSize . MEDIUM ) ,
336- version : KubernetesVersion . V1_23
341+ secretsEncryptionKey : secretsKey ,
342+ version : KubernetesVersion . of ( '1.27' ) ,
343+ kubectlLayer : new KubectlLayer ( this , 'kubectl' )
337344 } ) ;
338345
339346 const clusterSG = ec2 . SecurityGroup . fromSecurityGroupId ( this , 'ClusterSG' , cluster . clusterSecurityGroupId ) ;
@@ -448,9 +455,16 @@ export class Services extends Stack {
448455
449456 if ( isEventEngine === 'true' )
450457 {
451- var c9role = undefined
452- var c9InstanceProfile = undefined
453- var c9env = undefined
458+
459+ var c9Env = new Cloud9Environment ( this , 'Cloud9Environment' , {
460+ vpcId : theVPC . vpcId ,
461+ subnetId : theVPC . publicSubnets [ 0 ] . subnetId ,
462+ cloud9OwnerArn : "assumed-role/WSParticipantRole/Participant" ,
463+ templateFile : __dirname + "/../../../../cloud9-cfn.yaml"
464+
465+ } ) ;
466+
467+ var c9role = c9Env . c9Role ;
454468
455469 // Dynamically check if AWSCloud9SSMAccessRole and AWSCloud9SSMInstanceProfile exists
456470 const c9SSMRole = new iam . Role ( this , 'AWSCloud9SSMAccessRole' , {
@@ -460,51 +474,15 @@ export class Services extends Stack {
460474 managedPolicies : [ iam . ManagedPolicy . fromAwsManagedPolicyName ( "AWSCloud9SSMInstanceProfile" ) , iam . ManagedPolicy . fromAwsManagedPolicyName ( "AdministratorAccess" ) ]
461475 } ) ;
462476
463- const c9SSMRoleNoPath = iam . Role . fromRoleArn ( this , 'c9SSMRoleNoPath' , "arn:aws:iam::" + stack . account + ":role/AWSCloud9SSMAccessRole" )
464- cluster . awsAuth . addMastersRole ( c9SSMRoleNoPath ) ;
465-
466- new iam . CfnInstanceProfile ( this , 'AWSCloud9SSMInstanceProfile' , {
467- path : '/cloud9/' ,
468- roles : [ c9SSMRole . roleName ] ,
469- instanceProfileName : 'AWSCloud9SSMInstanceProfile'
470- } ) ;
471-
472- c9env = new cloud9 . CfnEnvironmentEC2 ( this , "CloudEnv" , {
473- ownerArn : "arn:aws:iam::" + stack . account + ":assumed-role/WSParticipantRole/Participant" ,
474- instanceType : "t2.micro" ,
475- name : "observabilityworkshop" ,
476- subnetId : theVPC . privateSubnets [ 0 ] . subnetId ,
477- connectionType : 'CONNECT_SSM' ,
478- repositories : [
479- {
480- repositoryUrl : "https://github.com/aws-samples/one-observability-demo.git" ,
481- pathComponent : "workshopfiles/one-observability-demo"
482- }
483- ]
484- } ) ;
485-
486- c9role = new iam . Role ( this , 'cloud9InstanceRole' , {
487- assumedBy : new iam . ServicePrincipal ( "ec2.amazonaws.com" ) ,
488- managedPolicies : [ iam . ManagedPolicy . fromAwsManagedPolicyName ( "AdministratorAccess" ) , iam . ManagedPolicy . fromAwsManagedPolicyName ( "AmazonSSMManagedInstanceCore" ) ] ,
489- roleName : "observabilityworkshop-admin"
490- } ) ;
491-
492- c9InstanceProfile = new iam . CfnInstanceProfile ( this , 'cloud9InstanceProfile' , {
493- roles : [ c9role . roleName ] ,
494- instanceProfileName : "observabilityworkshop-profile"
495- } )
496-
497- const teamRole = iam . Role . fromRoleArn ( this , 'TeamRole' , "arn:aws:iam::" + stack . account + ":role/TeamRole" ) ;
477+ const teamRole = iam . Role . fromRoleArn ( this , 'TeamRole' , "arn:aws:iam::" + stack . account + ":role/WSParticipantRole" ) ;
498478 cluster . awsAuth . addRoleMapping ( teamRole , { groups :[ "dashboard-view" ] } ) ;
479+
499480
481+ if ( c9role != undefined ) {
482+ cluster . awsAuth . addMastersRole ( iam . Role . fromRoleArn ( this , 'c9role' , c9role . attrArn , { mutable : false } ) ) ;
483+ }
500484
501485
502- if ( c9role != undefined )
503- cluster . awsAuth . addMastersRole ( c9role )
504-
505- if ( c9env != undefined )
506- cluster . node . addDependency ( c9env )
507-
508486 }
509487
510488 const eksAdminArn = this . node . tryGetContext ( 'admin_role' ) ;
0 commit comments