From 01028367b3ec282262b37de3abd1572d19370aa5 Mon Sep 17 00:00:00 2001 From: tomoki10 Date: Tue, 31 Jan 2023 11:03:36 +0900 Subject: [PATCH] feat(guest-webapp-sample): Replace LaunchConfiguration with LaunchTemplate (#183) Due to deprecation of LaunchConfiguration, replace it with LaunchTemplate. --- .../lib/blea-asgapp-stack.ts | 30 ++-- .../blea-guest-asgapp-sample.test.ts.snap | 149 ++++++++++++------ 2 files changed, 118 insertions(+), 61 deletions(-) diff --git a/usecases/guest-webapp-sample/lib/blea-asgapp-stack.ts b/usecases/guest-webapp-sample/lib/blea-asgapp-stack.ts index 602cb9174..e91a80649 100644 --- a/usecases/guest-webapp-sample/lib/blea-asgapp-stack.ts +++ b/usecases/guest-webapp-sample/lib/blea-asgapp-stack.ts @@ -67,21 +67,23 @@ export class BLEAASGAppStack extends cdk.Stack { vpcSubnets: props.myVpc.selectSubnets({ subnetGroupName: 'Private', }), - instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MICRO), - machineImage: new ec2.AmazonLinuxImage({ - generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2, + launchTemplate: new ec2.LaunchTemplate(this, 'LaunchTemplate', { + instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MICRO), + machineImage: new ec2.AmazonLinuxImage({ + generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2, + }), + userData: userDataForApp, + role: ssmInstanceRole, + blockDevices: [ + { + deviceName: '/dev/xvda', + volume: autoscaling.BlockDeviceVolume.ebs(10, { + encrypted: true, + }), + }, + ], + securityGroup: securityGroupForApp, }), - blockDevices: [ - { - deviceName: '/dev/xvda', - volume: autoscaling.BlockDeviceVolume.ebs(10, { - encrypted: true, - }), - }, - ], - securityGroup: securityGroupForApp, - role: ssmInstanceRole, - userData: userDataForApp, healthCheck: autoscaling.HealthCheck.elb({ grace: cdk.Duration.seconds(60), }), diff --git a/usecases/guest-webapp-sample/test/__snapshots__/blea-guest-asgapp-sample.test.ts.snap b/usecases/guest-webapp-sample/test/__snapshots__/blea-guest-asgapp-sample.test.ts.snap index 2f83e28a8..e605b94a0 100644 --- a/usecases/guest-webapp-sample/test/__snapshots__/blea-guest-asgapp-sample.test.ts.snap +++ b/usecases/guest-webapp-sample/test/__snapshots__/blea-guest-asgapp-sample.test.ts.snap @@ -1946,8 +1946,16 @@ Object { "Properties": Object { "HealthCheckGracePeriod": 60, "HealthCheckType": "ELB", - "LaunchConfigurationName": Object { - "Ref": "AsgAppLaunchConfig341A9477", + "LaunchTemplate": Object { + "LaunchTemplateId": Object { + "Ref": "LaunchTemplate04EC5460", + }, + "Version": Object { + "Fn::GetAtt": Array [ + "LaunchTemplate04EC5460", + "LatestVersionNumber", + ], + }, }, "MaxSize": "4", "MinSize": "2", @@ -1989,70 +1997,117 @@ Object { }, }, }, - "AsgAppInstanceProfile21D0A966": Object { + "AsgAppScalingPolicykeepSpareCPU63DAB833": Object { "Properties": Object { - "Roles": Array [ - Object { - "Ref": "ssminstancerole0E7D791C", + "AutoScalingGroupName": Object { + "Ref": "AsgAppASG65DC93B2", + }, + "PolicyType": "TargetTrackingScaling", + "TargetTrackingConfiguration": Object { + "PredefinedMetricSpecification": Object { + "PredefinedMetricType": "ASGAverageCPUUtilization", }, - ], + "TargetValue": 50, + }, }, - "Type": "AWS::IAM::InstanceProfile", + "Type": "AWS::AutoScaling::ScalingPolicy", }, - "AsgAppLaunchConfig341A9477": Object { - "DependsOn": Array [ - "ssminstancerole0E7D791C", - ], + "LaunchTemplate04EC5460": Object { "Properties": Object { - "BlockDeviceMappings": Array [ - Object { - "DeviceName": "/dev/xvda", - "Ebs": Object { - "Encrypted": true, - "VolumeSize": 10, + "LaunchTemplateData": Object { + "BlockDeviceMappings": Array [ + Object { + "DeviceName": "/dev/xvda", + "Ebs": Object { + "Encrypted": true, + "VolumeSize": 10, + }, + }, + ], + "IamInstanceProfile": Object { + "Arn": Object { + "Fn::GetAtt": Array [ + "LaunchTemplateProfile94AA77CE", + "Arn", + ], }, }, - ], - "IamInstanceProfile": Object { - "Ref": "AsgAppInstanceProfile21D0A966", - }, - "ImageId": Object { - "Ref": "SsmParameterValueawsserviceamiamazonlinuxlatestamzn2amihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter", - }, - "InstanceType": "t3.micro", - "SecurityGroups": Array [ - Object { - "Fn::GetAtt": Array [ - "SgApp7DE78E7E", - "GroupId", - ], + "ImageId": Object { + "Ref": "SsmParameterValueawsserviceamiamazonlinuxlatestamzn2amihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter", }, - ], - "UserData": Object { - "Fn::Base64": "#!/bin/bash + "InstanceType": "t3.micro", + "SecurityGroupIds": Array [ + Object { + "Fn::GetAtt": Array [ + "SgApp7DE78E7E", + "GroupId", + ], + }, + ], + "TagSpecifications": Array [ + Object { + "ResourceType": "instance", + "Tags": Array [ + Object { + "Key": "Environment", + "Value": "Development", + }, + Object { + "Key": "Name", + "Value": "BLEA-ASGApp/LaunchTemplate", + }, + ], + }, + Object { + "ResourceType": "volume", + "Tags": Array [ + Object { + "Key": "Environment", + "Value": "Development", + }, + Object { + "Key": "Name", + "Value": "BLEA-ASGApp/LaunchTemplate", + }, + ], + }, + ], + "UserData": Object { + "Fn::Base64": "#!/bin/bash sudo yum -y install httpd sudo systemctl enable httpd sudo systemctl start httpd touch /var/www/html/index.html chown apache.apache /var/www/html/index.html", + }, }, + "TagSpecifications": Array [ + Object { + "ResourceType": "launch-template", + "Tags": Array [ + Object { + "Key": "Environment", + "Value": "Development", + }, + Object { + "Key": "Name", + "Value": "BLEA-ASGApp/LaunchTemplate", + }, + ], + }, + ], }, - "Type": "AWS::AutoScaling::LaunchConfiguration", + "Type": "AWS::EC2::LaunchTemplate", }, - "AsgAppScalingPolicykeepSpareCPU63DAB833": Object { + "LaunchTemplateProfile94AA77CE": Object { "Properties": Object { - "AutoScalingGroupName": Object { - "Ref": "AsgAppASG65DC93B2", - }, - "PolicyType": "TargetTrackingScaling", - "TargetTrackingConfiguration": Object { - "PredefinedMetricSpecification": Object { - "PredefinedMetricType": "ASGAverageCPUUtilization", + "Roles": Array [ + Object { + "Ref": "ssminstancerole0E7D791C", }, - "TargetValue": 50, - }, + ], }, - "Type": "AWS::AutoScaling::ScalingPolicy", + "Type": "AWS::IAM::InstanceProfile", }, "SgAlbEA3AC417": Object { "Properties": Object {