diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/InstanceTerminationPolicyTestDefaultTestDeployAssertD453E37E.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/InstanceTerminationPolicyTestDefaultTestDeployAssertD453E37E.assets.json new file mode 100644 index 0000000000000..fd1e04a64481e --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/InstanceTerminationPolicyTestDefaultTestDeployAssertD453E37E.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "InstanceTerminationPolicyTestDefaultTestDeployAssertD453E37E.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/InstanceTerminationPolicyTestDefaultTestDeployAssertD453E37E.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/InstanceTerminationPolicyTestDefaultTestDeployAssertD453E37E.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/InstanceTerminationPolicyTestDefaultTestDeployAssertD453E37E.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/aws-cdk-autoscaling-instance-termination-policy.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/aws-cdk-autoscaling-instance-termination-policy.assets.json new file mode 100644 index 0000000000000..947f77e03a964 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/aws-cdk-autoscaling-instance-termination-policy.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.0", + "files": { + "0ece8e7474492fb9bf96c13fe64d386d3095f1c46c71d34d0cd9d77c870eb47c": { + "source": { + "path": "aws-cdk-autoscaling-instance-termination-policy.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "0ece8e7474492fb9bf96c13fe64d386d3095f1c46c71d34d0cd9d77c870eb47c.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/aws-cdk-autoscaling-instance-termination-policy.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/aws-cdk-autoscaling-instance-termination-policy.template.json new file mode 100644 index 0000000000000..555eed1513ed1 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/aws-cdk-autoscaling-instance-termination-policy.template.json @@ -0,0 +1,692 @@ +{ + "Resources": { + "VPCB9E5F0B4": { + "Type": "AWS::EC2::VPC", + "Properties": { + "CidrBlock": "10.0.0.0/16", + "EnableDnsHostnames": true, + "EnableDnsSupport": true, + "InstanceTenancy": "default", + "Tags": [ + { + "Key": "Name", + "Value": "aws-cdk-autoscaling-instance-termination-policy/VPC" + } + ] + } + }, + "VPCPublicSubnet1SubnetB4246D30": { + "Type": "AWS::EC2::Subnet", + "Properties": { + "AvailabilityZone": { + "Fn::Select": [ + 0, + { + "Fn::GetAZs": "" + } + ] + }, + "CidrBlock": "10.0.0.0/18", + "MapPublicIpOnLaunch": true, + "Tags": [ + { + "Key": "aws-cdk:subnet-name", + "Value": "Public" + }, + { + "Key": "aws-cdk:subnet-type", + "Value": "Public" + }, + { + "Key": "Name", + "Value": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet1" + } + ], + "VpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "VPCPublicSubnet1RouteTableFEE4B781": { + "Type": "AWS::EC2::RouteTable", + "Properties": { + "Tags": [ + { + "Key": "Name", + "Value": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet1" + } + ], + "VpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "VPCPublicSubnet1RouteTableAssociation0B0896DC": { + "Type": "AWS::EC2::SubnetRouteTableAssociation", + "Properties": { + "RouteTableId": { + "Ref": "VPCPublicSubnet1RouteTableFEE4B781" + }, + "SubnetId": { + "Ref": "VPCPublicSubnet1SubnetB4246D30" + } + } + }, + "VPCPublicSubnet1DefaultRoute91CEF279": { + "Type": "AWS::EC2::Route", + "Properties": { + "DestinationCidrBlock": "0.0.0.0/0", + "GatewayId": { + "Ref": "VPCIGWB7E252D3" + }, + "RouteTableId": { + "Ref": "VPCPublicSubnet1RouteTableFEE4B781" + } + }, + "DependsOn": [ + "VPCVPCGW99B986DC" + ] + }, + "VPCPublicSubnet2Subnet74179F39": { + "Type": "AWS::EC2::Subnet", + "Properties": { + "AvailabilityZone": { + "Fn::Select": [ + 1, + { + "Fn::GetAZs": "" + } + ] + }, + "CidrBlock": "10.0.64.0/18", + "MapPublicIpOnLaunch": true, + "Tags": [ + { + "Key": "aws-cdk:subnet-name", + "Value": "Public" + }, + { + "Key": "aws-cdk:subnet-type", + "Value": "Public" + }, + { + "Key": "Name", + "Value": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet2" + } + ], + "VpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "VPCPublicSubnet2RouteTable6F1A15F1": { + "Type": "AWS::EC2::RouteTable", + "Properties": { + "Tags": [ + { + "Key": "Name", + "Value": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet2" + } + ], + "VpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "VPCPublicSubnet2RouteTableAssociation5A808732": { + "Type": "AWS::EC2::SubnetRouteTableAssociation", + "Properties": { + "RouteTableId": { + "Ref": "VPCPublicSubnet2RouteTable6F1A15F1" + }, + "SubnetId": { + "Ref": "VPCPublicSubnet2Subnet74179F39" + } + } + }, + "VPCPublicSubnet2DefaultRouteB7481BBA": { + "Type": "AWS::EC2::Route", + "Properties": { + "DestinationCidrBlock": "0.0.0.0/0", + "GatewayId": { + "Ref": "VPCIGWB7E252D3" + }, + "RouteTableId": { + "Ref": "VPCPublicSubnet2RouteTable6F1A15F1" + } + }, + "DependsOn": [ + "VPCVPCGW99B986DC" + ] + }, + "VPCIsolatedSubnet1SubnetEBD00FC6": { + "Type": "AWS::EC2::Subnet", + "Properties": { + "AvailabilityZone": { + "Fn::Select": [ + 0, + { + "Fn::GetAZs": "" + } + ] + }, + "CidrBlock": "10.0.128.0/18", + "MapPublicIpOnLaunch": false, + "Tags": [ + { + "Key": "aws-cdk:subnet-name", + "Value": "Isolated" + }, + { + "Key": "aws-cdk:subnet-type", + "Value": "Isolated" + }, + { + "Key": "Name", + "Value": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet1" + } + ], + "VpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "VPCIsolatedSubnet1RouteTableEB156210": { + "Type": "AWS::EC2::RouteTable", + "Properties": { + "Tags": [ + { + "Key": "Name", + "Value": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet1" + } + ], + "VpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "VPCIsolatedSubnet1RouteTableAssociationA2D18F7C": { + "Type": "AWS::EC2::SubnetRouteTableAssociation", + "Properties": { + "RouteTableId": { + "Ref": "VPCIsolatedSubnet1RouteTableEB156210" + }, + "SubnetId": { + "Ref": "VPCIsolatedSubnet1SubnetEBD00FC6" + } + } + }, + "VPCIsolatedSubnet2Subnet4B1C8CAA": { + "Type": "AWS::EC2::Subnet", + "Properties": { + "AvailabilityZone": { + "Fn::Select": [ + 1, + { + "Fn::GetAZs": "" + } + ] + }, + "CidrBlock": "10.0.192.0/18", + "MapPublicIpOnLaunch": false, + "Tags": [ + { + "Key": "aws-cdk:subnet-name", + "Value": "Isolated" + }, + { + "Key": "aws-cdk:subnet-type", + "Value": "Isolated" + }, + { + "Key": "Name", + "Value": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet2" + } + ], + "VpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "VPCIsolatedSubnet2RouteTable9B4F78DC": { + "Type": "AWS::EC2::RouteTable", + "Properties": { + "Tags": [ + { + "Key": "Name", + "Value": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet2" + } + ], + "VpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "VPCIsolatedSubnet2RouteTableAssociation7BF8E0EB": { + "Type": "AWS::EC2::SubnetRouteTableAssociation", + "Properties": { + "RouteTableId": { + "Ref": "VPCIsolatedSubnet2RouteTable9B4F78DC" + }, + "SubnetId": { + "Ref": "VPCIsolatedSubnet2Subnet4B1C8CAA" + } + } + }, + "VPCIGWB7E252D3": { + "Type": "AWS::EC2::InternetGateway", + "Properties": { + "Tags": [ + { + "Key": "Name", + "Value": "aws-cdk-autoscaling-instance-termination-policy/VPC" + } + ] + } + }, + "VPCVPCGW99B986DC": { + "Type": "AWS::EC2::VPCGatewayAttachment", + "Properties": { + "InternetGatewayId": { + "Ref": "VPCIGWB7E252D3" + }, + "VpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "MyFunctionServiceRole3C357FF2": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "MyFunction3BAA72D1": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "ZipFile": "exports.handler = function asgHandler(_event, _context, callback) {\n let json = JSON.parse(JSON.stringify(_event));\n let instanceIds = [];\n json.Instances.forEach((instance) => {\n instanceIds.push(instance.InstanceId);\n });\n // eslint-disable-next-line quote-props\n const result = { 'InstanceIDs': instanceIds };\n return callback(undefined, result);\n}" + }, + "Handler": "index.handler", + "Role": { + "Fn::GetAtt": [ + "MyFunctionServiceRole3C357FF2", + "Arn" + ] + }, + "Runtime": "nodejs20.x" + }, + "DependsOn": [ + "MyFunctionServiceRole3C357FF2" + ] + }, + "MyFunctionAllowInvokeByAutoScalingE1D035DF": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Fn::GetAtt": [ + "MyFunction3BAA72D1", + "Arn" + ] + }, + "Principal": { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::", + { + "Ref": "AWS::AccountId" + }, + ":role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling" + ] + ] + } + } + }, + "ASGInstanceSecurityGroup0525485D": { + "Type": "AWS::EC2::SecurityGroup", + "Properties": { + "GroupDescription": "aws-cdk-autoscaling-instance-termination-policy/ASG/InstanceSecurityGroup", + "SecurityGroupEgress": [ + { + "CidrIp": "0.0.0.0/0", + "Description": "Allow all outbound traffic by default", + "IpProtocol": "-1" + } + ], + "Tags": [ + { + "Key": "Name", + "Value": "aws-cdk-autoscaling-instance-termination-policy/ASG" + } + ], + "VpcId": { + "Ref": "VPCB9E5F0B4" + } + }, + "DependsOn": [ + "MyFunctionAllowInvokeByAutoScalingE1D035DF", + "MyFunction3BAA72D1", + "MyFunctionServiceRole3C357FF2", + "VPCIGWB7E252D3", + "VPCIsolatedSubnet1RouteTableEB156210", + "VPCIsolatedSubnet1RouteTableAssociationA2D18F7C", + "VPCIsolatedSubnet1SubnetEBD00FC6", + "VPCIsolatedSubnet2RouteTable9B4F78DC", + "VPCIsolatedSubnet2RouteTableAssociation7BF8E0EB", + "VPCIsolatedSubnet2Subnet4B1C8CAA", + "VPCPublicSubnet1DefaultRoute91CEF279", + "VPCPublicSubnet1RouteTableFEE4B781", + "VPCPublicSubnet1RouteTableAssociation0B0896DC", + "VPCPublicSubnet1SubnetB4246D30", + "VPCPublicSubnet2DefaultRouteB7481BBA", + "VPCPublicSubnet2RouteTable6F1A15F1", + "VPCPublicSubnet2RouteTableAssociation5A808732", + "VPCPublicSubnet2Subnet74179F39", + "VPCB9E5F0B4", + "VPCVPCGW99B986DC" + ] + }, + "ASGInstanceRoleE263A41B": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "ec2.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "Tags": [ + { + "Key": "Name", + "Value": "aws-cdk-autoscaling-instance-termination-policy/ASG" + } + ] + }, + "DependsOn": [ + "MyFunctionAllowInvokeByAutoScalingE1D035DF", + "MyFunction3BAA72D1", + "MyFunctionServiceRole3C357FF2", + "VPCIGWB7E252D3", + "VPCIsolatedSubnet1RouteTableEB156210", + "VPCIsolatedSubnet1RouteTableAssociationA2D18F7C", + "VPCIsolatedSubnet1SubnetEBD00FC6", + "VPCIsolatedSubnet2RouteTable9B4F78DC", + "VPCIsolatedSubnet2RouteTableAssociation7BF8E0EB", + "VPCIsolatedSubnet2Subnet4B1C8CAA", + "VPCPublicSubnet1DefaultRoute91CEF279", + "VPCPublicSubnet1RouteTableFEE4B781", + "VPCPublicSubnet1RouteTableAssociation0B0896DC", + "VPCPublicSubnet1SubnetB4246D30", + "VPCPublicSubnet2DefaultRouteB7481BBA", + "VPCPublicSubnet2RouteTable6F1A15F1", + "VPCPublicSubnet2RouteTableAssociation5A808732", + "VPCPublicSubnet2Subnet74179F39", + "VPCB9E5F0B4", + "VPCVPCGW99B986DC" + ] + }, + "ASGInstanceProfile0A2834D7": { + "Type": "AWS::IAM::InstanceProfile", + "Properties": { + "Roles": [ + { + "Ref": "ASGInstanceRoleE263A41B" + } + ] + }, + "DependsOn": [ + "MyFunctionAllowInvokeByAutoScalingE1D035DF", + "MyFunction3BAA72D1", + "MyFunctionServiceRole3C357FF2", + "VPCIGWB7E252D3", + "VPCIsolatedSubnet1RouteTableEB156210", + "VPCIsolatedSubnet1RouteTableAssociationA2D18F7C", + "VPCIsolatedSubnet1SubnetEBD00FC6", + "VPCIsolatedSubnet2RouteTable9B4F78DC", + "VPCIsolatedSubnet2RouteTableAssociation7BF8E0EB", + "VPCIsolatedSubnet2Subnet4B1C8CAA", + "VPCPublicSubnet1DefaultRoute91CEF279", + "VPCPublicSubnet1RouteTableFEE4B781", + "VPCPublicSubnet1RouteTableAssociation0B0896DC", + "VPCPublicSubnet1SubnetB4246D30", + "VPCPublicSubnet2DefaultRouteB7481BBA", + "VPCPublicSubnet2RouteTable6F1A15F1", + "VPCPublicSubnet2RouteTableAssociation5A808732", + "VPCPublicSubnet2Subnet74179F39", + "VPCB9E5F0B4", + "VPCVPCGW99B986DC" + ] + }, + "ASGLaunchTemplate0CA92847": { + "Type": "AWS::EC2::LaunchTemplate", + "Properties": { + "LaunchTemplateData": { + "IamInstanceProfile": { + "Arn": { + "Fn::GetAtt": [ + "ASGInstanceProfile0A2834D7", + "Arn" + ] + } + }, + "ImageId": { + "Ref": "SsmParameterValueawsserviceamiamazonlinuxlatestamznamihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter" + }, + "InstanceType": "t2.micro", + "Monitoring": { + "Enabled": false + }, + "SecurityGroupIds": [ + { + "Fn::GetAtt": [ + "ASGInstanceSecurityGroup0525485D", + "GroupId" + ] + } + ], + "TagSpecifications": [ + { + "ResourceType": "instance", + "Tags": [ + { + "Key": "Name", + "Value": "aws-cdk-autoscaling-instance-termination-policy/ASG/LaunchTemplate" + } + ] + }, + { + "ResourceType": "volume", + "Tags": [ + { + "Key": "Name", + "Value": "aws-cdk-autoscaling-instance-termination-policy/ASG/LaunchTemplate" + } + ] + } + ], + "UserData": { + "Fn::Base64": "#!/bin/bash" + } + }, + "TagSpecifications": [ + { + "ResourceType": "launch-template", + "Tags": [ + { + "Key": "Name", + "Value": "aws-cdk-autoscaling-instance-termination-policy/ASG/LaunchTemplate" + } + ] + } + ] + }, + "DependsOn": [ + "ASGInstanceRoleE263A41B", + "MyFunctionAllowInvokeByAutoScalingE1D035DF", + "MyFunction3BAA72D1", + "MyFunctionServiceRole3C357FF2", + "VPCIGWB7E252D3", + "VPCIsolatedSubnet1RouteTableEB156210", + "VPCIsolatedSubnet1RouteTableAssociationA2D18F7C", + "VPCIsolatedSubnet1SubnetEBD00FC6", + "VPCIsolatedSubnet2RouteTable9B4F78DC", + "VPCIsolatedSubnet2RouteTableAssociation7BF8E0EB", + "VPCIsolatedSubnet2Subnet4B1C8CAA", + "VPCPublicSubnet1DefaultRoute91CEF279", + "VPCPublicSubnet1RouteTableFEE4B781", + "VPCPublicSubnet1RouteTableAssociation0B0896DC", + "VPCPublicSubnet1SubnetB4246D30", + "VPCPublicSubnet2DefaultRouteB7481BBA", + "VPCPublicSubnet2RouteTable6F1A15F1", + "VPCPublicSubnet2RouteTableAssociation5A808732", + "VPCPublicSubnet2Subnet74179F39", + "VPCB9E5F0B4", + "VPCVPCGW99B986DC" + ] + }, + "ASG46ED3070": { + "Type": "AWS::AutoScaling::AutoScalingGroup", + "Properties": { + "LaunchTemplate": { + "LaunchTemplateId": { + "Ref": "ASGLaunchTemplate0CA92847" + }, + "Version": { + "Fn::GetAtt": [ + "ASGLaunchTemplate0CA92847", + "LatestVersionNumber" + ] + } + }, + "MaxSize": "1", + "MinSize": "1", + "Tags": [ + { + "Key": "Name", + "PropagateAtLaunch": true, + "Value": "aws-cdk-autoscaling-instance-termination-policy/ASG" + } + ], + "TerminationPolicies": [ + { + "Fn::GetAtt": [ + "MyFunction3BAA72D1", + "Arn" + ] + }, + "OldestInstance" + ], + "VPCZoneIdentifier": [ + { + "Ref": "VPCIsolatedSubnet1SubnetEBD00FC6" + }, + { + "Ref": "VPCIsolatedSubnet2Subnet4B1C8CAA" + } + ] + }, + "DependsOn": [ + "MyFunctionAllowInvokeByAutoScalingE1D035DF", + "MyFunction3BAA72D1", + "MyFunctionServiceRole3C357FF2", + "VPCIGWB7E252D3", + "VPCIsolatedSubnet1RouteTableEB156210", + "VPCIsolatedSubnet1RouteTableAssociationA2D18F7C", + "VPCIsolatedSubnet1SubnetEBD00FC6", + "VPCIsolatedSubnet2RouteTable9B4F78DC", + "VPCIsolatedSubnet2RouteTableAssociation7BF8E0EB", + "VPCIsolatedSubnet2Subnet4B1C8CAA", + "VPCPublicSubnet1DefaultRoute91CEF279", + "VPCPublicSubnet1RouteTableFEE4B781", + "VPCPublicSubnet1RouteTableAssociation0B0896DC", + "VPCPublicSubnet1SubnetB4246D30", + "VPCPublicSubnet2DefaultRouteB7481BBA", + "VPCPublicSubnet2RouteTable6F1A15F1", + "VPCPublicSubnet2RouteTableAssociation5A808732", + "VPCPublicSubnet2Subnet74179F39", + "VPCB9E5F0B4", + "VPCVPCGW99B986DC" + ], + "UpdatePolicy": { + "AutoScalingScheduledAction": { + "IgnoreUnmodifiedGroupSizeProperties": true + } + } + } + }, + "Parameters": { + "SsmParameterValueawsserviceamiamazonlinuxlatestamznamihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-gp2" + }, + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/cdk.out new file mode 100644 index 0000000000000..1f0068d32659a --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"36.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/integ.json new file mode 100644 index 0000000000000..65851698b5abf --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "36.0.0", + "testCases": { + "InstanceTerminationPolicyTest/DefaultTest": { + "stacks": [ + "aws-cdk-autoscaling-instance-termination-policy" + ], + "assertionStack": "InstanceTerminationPolicyTest/DefaultTest/DeployAssert", + "assertionStackName": "InstanceTerminationPolicyTestDefaultTestDeployAssertD453E37E" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/manifest.json new file mode 100644 index 0000000000000..7a6af2232ada0 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/manifest.json @@ -0,0 +1,263 @@ +{ + "version": "36.0.0", + "artifacts": { + "aws-cdk-autoscaling-instance-termination-policy.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "aws-cdk-autoscaling-instance-termination-policy.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "aws-cdk-autoscaling-instance-termination-policy": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "aws-cdk-autoscaling-instance-termination-policy.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/0ece8e7474492fb9bf96c13fe64d386d3095f1c46c71d34d0cd9d77c870eb47c.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "aws-cdk-autoscaling-instance-termination-policy.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "aws-cdk-autoscaling-instance-termination-policy.assets" + ], + "metadata": { + "/aws-cdk-autoscaling-instance-termination-policy/VPC/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCB9E5F0B4" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet1/Subnet": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCPublicSubnet1SubnetB4246D30" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet1/RouteTable": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCPublicSubnet1RouteTableFEE4B781" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet1/RouteTableAssociation": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCPublicSubnet1RouteTableAssociation0B0896DC" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet1/DefaultRoute": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCPublicSubnet1DefaultRoute91CEF279" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet2/Subnet": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCPublicSubnet2Subnet74179F39" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet2/RouteTable": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCPublicSubnet2RouteTable6F1A15F1" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet2/RouteTableAssociation": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCPublicSubnet2RouteTableAssociation5A808732" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet2/DefaultRoute": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCPublicSubnet2DefaultRouteB7481BBA" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet1/Subnet": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCIsolatedSubnet1SubnetEBD00FC6" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet1/RouteTable": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCIsolatedSubnet1RouteTableEB156210" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet1/RouteTableAssociation": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCIsolatedSubnet1RouteTableAssociationA2D18F7C" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet2/Subnet": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCIsolatedSubnet2Subnet4B1C8CAA" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet2/RouteTable": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCIsolatedSubnet2RouteTable9B4F78DC" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet2/RouteTableAssociation": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCIsolatedSubnet2RouteTableAssociation7BF8E0EB" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/IGW": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCIGWB7E252D3" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/VPC/VPCGW": [ + { + "type": "aws:cdk:logicalId", + "data": "VPCVPCGW99B986DC" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/MyFunction/ServiceRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "MyFunctionServiceRole3C357FF2" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/MyFunction/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "MyFunction3BAA72D1" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/MyFunction/AllowInvokeByAutoScaling": [ + { + "type": "aws:cdk:logicalId", + "data": "MyFunctionAllowInvokeByAutoScalingE1D035DF" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/ASG/InstanceSecurityGroup/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "ASGInstanceSecurityGroup0525485D" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/ASG/InstanceRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "ASGInstanceRoleE263A41B" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/ASG/InstanceProfile": [ + { + "type": "aws:cdk:logicalId", + "data": "ASGInstanceProfile0A2834D7" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/ASG/LaunchTemplate/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "ASGLaunchTemplate0CA92847" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/ASG/ASG": [ + { + "type": "aws:cdk:logicalId", + "data": "ASG46ED3070" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/SsmParameterValue:--aws--service--ami-amazon-linux-latest--amzn-ami-hvm-x86_64-gp2:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter": [ + { + "type": "aws:cdk:logicalId", + "data": "SsmParameterValueawsserviceamiamazonlinuxlatestamznamihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/aws-cdk-autoscaling-instance-termination-policy/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "aws-cdk-autoscaling-instance-termination-policy" + }, + "InstanceTerminationPolicyTestDefaultTestDeployAssertD453E37E.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "InstanceTerminationPolicyTestDefaultTestDeployAssertD453E37E.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "InstanceTerminationPolicyTestDefaultTestDeployAssertD453E37E": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "InstanceTerminationPolicyTestDefaultTestDeployAssertD453E37E.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "InstanceTerminationPolicyTestDefaultTestDeployAssertD453E37E.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "InstanceTerminationPolicyTestDefaultTestDeployAssertD453E37E.assets" + ], + "metadata": { + "/InstanceTerminationPolicyTest/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/InstanceTerminationPolicyTest/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "InstanceTerminationPolicyTest/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/tree.json new file mode 100644 index 0000000000000..0767f9c769098 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.js.snapshot/tree.json @@ -0,0 +1,1008 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "aws-cdk-autoscaling-instance-termination-policy": { + "id": "aws-cdk-autoscaling-instance-termination-policy", + "path": "aws-cdk-autoscaling-instance-termination-policy", + "children": { + "VPC": { + "id": "VPC", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC", + "children": { + "Resource": { + "id": "Resource", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::VPC", + "aws:cdk:cloudformation:props": { + "cidrBlock": "10.0.0.0/16", + "enableDnsHostnames": true, + "enableDnsSupport": true, + "instanceTenancy": "default", + "tags": [ + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/VPC" + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnVPC", + "version": "0.0.0" + } + }, + "PublicSubnet1": { + "id": "PublicSubnet1", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet1", + "children": { + "Subnet": { + "id": "Subnet", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet1/Subnet", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::Subnet", + "aws:cdk:cloudformation:props": { + "availabilityZone": { + "Fn::Select": [ + 0, + { + "Fn::GetAZs": "" + } + ] + }, + "cidrBlock": "10.0.0.0/18", + "mapPublicIpOnLaunch": true, + "tags": [ + { + "key": "aws-cdk:subnet-name", + "value": "Public" + }, + { + "key": "aws-cdk:subnet-type", + "value": "Public" + }, + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet1" + } + ], + "vpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnSubnet", + "version": "0.0.0" + } + }, + "Acl": { + "id": "Acl", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet1/Acl", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "RouteTable": { + "id": "RouteTable", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet1/RouteTable", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::RouteTable", + "aws:cdk:cloudformation:props": { + "tags": [ + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet1" + } + ], + "vpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnRouteTable", + "version": "0.0.0" + } + }, + "RouteTableAssociation": { + "id": "RouteTableAssociation", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet1/RouteTableAssociation", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::SubnetRouteTableAssociation", + "aws:cdk:cloudformation:props": { + "routeTableId": { + "Ref": "VPCPublicSubnet1RouteTableFEE4B781" + }, + "subnetId": { + "Ref": "VPCPublicSubnet1SubnetB4246D30" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnSubnetRouteTableAssociation", + "version": "0.0.0" + } + }, + "DefaultRoute": { + "id": "DefaultRoute", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet1/DefaultRoute", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::Route", + "aws:cdk:cloudformation:props": { + "destinationCidrBlock": "0.0.0.0/0", + "gatewayId": { + "Ref": "VPCIGWB7E252D3" + }, + "routeTableId": { + "Ref": "VPCPublicSubnet1RouteTableFEE4B781" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnRoute", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.PublicSubnet", + "version": "0.0.0" + } + }, + "PublicSubnet2": { + "id": "PublicSubnet2", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet2", + "children": { + "Subnet": { + "id": "Subnet", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet2/Subnet", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::Subnet", + "aws:cdk:cloudformation:props": { + "availabilityZone": { + "Fn::Select": [ + 1, + { + "Fn::GetAZs": "" + } + ] + }, + "cidrBlock": "10.0.64.0/18", + "mapPublicIpOnLaunch": true, + "tags": [ + { + "key": "aws-cdk:subnet-name", + "value": "Public" + }, + { + "key": "aws-cdk:subnet-type", + "value": "Public" + }, + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet2" + } + ], + "vpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnSubnet", + "version": "0.0.0" + } + }, + "Acl": { + "id": "Acl", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet2/Acl", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "RouteTable": { + "id": "RouteTable", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet2/RouteTable", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::RouteTable", + "aws:cdk:cloudformation:props": { + "tags": [ + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet2" + } + ], + "vpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnRouteTable", + "version": "0.0.0" + } + }, + "RouteTableAssociation": { + "id": "RouteTableAssociation", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet2/RouteTableAssociation", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::SubnetRouteTableAssociation", + "aws:cdk:cloudformation:props": { + "routeTableId": { + "Ref": "VPCPublicSubnet2RouteTable6F1A15F1" + }, + "subnetId": { + "Ref": "VPCPublicSubnet2Subnet74179F39" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnSubnetRouteTableAssociation", + "version": "0.0.0" + } + }, + "DefaultRoute": { + "id": "DefaultRoute", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/PublicSubnet2/DefaultRoute", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::Route", + "aws:cdk:cloudformation:props": { + "destinationCidrBlock": "0.0.0.0/0", + "gatewayId": { + "Ref": "VPCIGWB7E252D3" + }, + "routeTableId": { + "Ref": "VPCPublicSubnet2RouteTable6F1A15F1" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnRoute", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.PublicSubnet", + "version": "0.0.0" + } + }, + "IsolatedSubnet1": { + "id": "IsolatedSubnet1", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet1", + "children": { + "Subnet": { + "id": "Subnet", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet1/Subnet", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::Subnet", + "aws:cdk:cloudformation:props": { + "availabilityZone": { + "Fn::Select": [ + 0, + { + "Fn::GetAZs": "" + } + ] + }, + "cidrBlock": "10.0.128.0/18", + "mapPublicIpOnLaunch": false, + "tags": [ + { + "key": "aws-cdk:subnet-name", + "value": "Isolated" + }, + { + "key": "aws-cdk:subnet-type", + "value": "Isolated" + }, + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet1" + } + ], + "vpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnSubnet", + "version": "0.0.0" + } + }, + "Acl": { + "id": "Acl", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet1/Acl", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "RouteTable": { + "id": "RouteTable", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet1/RouteTable", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::RouteTable", + "aws:cdk:cloudformation:props": { + "tags": [ + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet1" + } + ], + "vpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnRouteTable", + "version": "0.0.0" + } + }, + "RouteTableAssociation": { + "id": "RouteTableAssociation", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet1/RouteTableAssociation", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::SubnetRouteTableAssociation", + "aws:cdk:cloudformation:props": { + "routeTableId": { + "Ref": "VPCIsolatedSubnet1RouteTableEB156210" + }, + "subnetId": { + "Ref": "VPCIsolatedSubnet1SubnetEBD00FC6" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnSubnetRouteTableAssociation", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.PrivateSubnet", + "version": "0.0.0" + } + }, + "IsolatedSubnet2": { + "id": "IsolatedSubnet2", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet2", + "children": { + "Subnet": { + "id": "Subnet", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet2/Subnet", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::Subnet", + "aws:cdk:cloudformation:props": { + "availabilityZone": { + "Fn::Select": [ + 1, + { + "Fn::GetAZs": "" + } + ] + }, + "cidrBlock": "10.0.192.0/18", + "mapPublicIpOnLaunch": false, + "tags": [ + { + "key": "aws-cdk:subnet-name", + "value": "Isolated" + }, + { + "key": "aws-cdk:subnet-type", + "value": "Isolated" + }, + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet2" + } + ], + "vpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnSubnet", + "version": "0.0.0" + } + }, + "Acl": { + "id": "Acl", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet2/Acl", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "RouteTable": { + "id": "RouteTable", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet2/RouteTable", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::RouteTable", + "aws:cdk:cloudformation:props": { + "tags": [ + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet2" + } + ], + "vpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnRouteTable", + "version": "0.0.0" + } + }, + "RouteTableAssociation": { + "id": "RouteTableAssociation", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/IsolatedSubnet2/RouteTableAssociation", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::SubnetRouteTableAssociation", + "aws:cdk:cloudformation:props": { + "routeTableId": { + "Ref": "VPCIsolatedSubnet2RouteTable9B4F78DC" + }, + "subnetId": { + "Ref": "VPCIsolatedSubnet2Subnet4B1C8CAA" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnSubnetRouteTableAssociation", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.PrivateSubnet", + "version": "0.0.0" + } + }, + "IGW": { + "id": "IGW", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/IGW", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::InternetGateway", + "aws:cdk:cloudformation:props": { + "tags": [ + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/VPC" + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnInternetGateway", + "version": "0.0.0" + } + }, + "VPCGW": { + "id": "VPCGW", + "path": "aws-cdk-autoscaling-instance-termination-policy/VPC/VPCGW", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::VPCGatewayAttachment", + "aws:cdk:cloudformation:props": { + "internetGatewayId": { + "Ref": "VPCIGWB7E252D3" + }, + "vpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnVPCGatewayAttachment", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.Vpc", + "version": "0.0.0" + } + }, + "MyFunction": { + "id": "MyFunction", + "path": "aws-cdk-autoscaling-instance-termination-policy/MyFunction", + "children": { + "ServiceRole": { + "id": "ServiceRole", + "path": "aws-cdk-autoscaling-instance-termination-policy/MyFunction/ServiceRole", + "children": { + "ImportServiceRole": { + "id": "ImportServiceRole", + "path": "aws-cdk-autoscaling-instance-termination-policy/MyFunction/ServiceRole/ImportServiceRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "aws-cdk-autoscaling-instance-termination-policy/MyFunction/ServiceRole/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "assumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "managedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnRole", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.Role", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "aws-cdk-autoscaling-instance-termination-policy/MyFunction/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Function", + "aws:cdk:cloudformation:props": { + "code": { + "zipFile": "exports.handler = function asgHandler(_event, _context, callback) {\n let json = JSON.parse(JSON.stringify(_event));\n let instanceIds = [];\n json.Instances.forEach((instance) => {\n instanceIds.push(instance.InstanceId);\n });\n // eslint-disable-next-line quote-props\n const result = { 'InstanceIDs': instanceIds };\n return callback(undefined, result);\n}" + }, + "handler": "index.handler", + "role": { + "Fn::GetAtt": [ + "MyFunctionServiceRole3C357FF2", + "Arn" + ] + }, + "runtime": "nodejs20.x" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnFunction", + "version": "0.0.0" + } + }, + "AllowInvokeByAutoScaling": { + "id": "AllowInvokeByAutoScaling", + "path": "aws-cdk-autoscaling-instance-termination-policy/MyFunction/AllowInvokeByAutoScaling", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Fn::GetAtt": [ + "MyFunction3BAA72D1", + "Arn" + ] + }, + "principal": { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::", + { + "Ref": "AWS::AccountId" + }, + ":role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling" + ] + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.Function", + "version": "0.0.0" + } + }, + "ASG": { + "id": "ASG", + "path": "aws-cdk-autoscaling-instance-termination-policy/ASG", + "children": { + "InstanceSecurityGroup": { + "id": "InstanceSecurityGroup", + "path": "aws-cdk-autoscaling-instance-termination-policy/ASG/InstanceSecurityGroup", + "children": { + "Resource": { + "id": "Resource", + "path": "aws-cdk-autoscaling-instance-termination-policy/ASG/InstanceSecurityGroup/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::SecurityGroup", + "aws:cdk:cloudformation:props": { + "groupDescription": "aws-cdk-autoscaling-instance-termination-policy/ASG/InstanceSecurityGroup", + "securityGroupEgress": [ + { + "cidrIp": "0.0.0.0/0", + "description": "Allow all outbound traffic by default", + "ipProtocol": "-1" + } + ], + "tags": [ + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/ASG" + } + ], + "vpcId": { + "Ref": "VPCB9E5F0B4" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnSecurityGroup", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.SecurityGroup", + "version": "0.0.0" + } + }, + "InstanceRole": { + "id": "InstanceRole", + "path": "aws-cdk-autoscaling-instance-termination-policy/ASG/InstanceRole", + "children": { + "ImportInstanceRole": { + "id": "ImportInstanceRole", + "path": "aws-cdk-autoscaling-instance-termination-policy/ASG/InstanceRole/ImportInstanceRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "aws-cdk-autoscaling-instance-termination-policy/ASG/InstanceRole/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "assumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "ec2.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "tags": [ + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/ASG" + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnRole", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.Role", + "version": "0.0.0" + } + }, + "InstanceProfile": { + "id": "InstanceProfile", + "path": "aws-cdk-autoscaling-instance-termination-policy/ASG/InstanceProfile", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::InstanceProfile", + "aws:cdk:cloudformation:props": { + "roles": [ + { + "Ref": "ASGInstanceRoleE263A41B" + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnInstanceProfile", + "version": "0.0.0" + } + }, + "ImportedInstanceProfile": { + "id": "ImportedInstanceProfile", + "path": "aws-cdk-autoscaling-instance-termination-policy/ASG/ImportedInstanceProfile", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "LaunchTemplate": { + "id": "LaunchTemplate", + "path": "aws-cdk-autoscaling-instance-termination-policy/ASG/LaunchTemplate", + "children": { + "Resource": { + "id": "Resource", + "path": "aws-cdk-autoscaling-instance-termination-policy/ASG/LaunchTemplate/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::EC2::LaunchTemplate", + "aws:cdk:cloudformation:props": { + "launchTemplateData": { + "iamInstanceProfile": { + "arn": { + "Fn::GetAtt": [ + "ASGInstanceProfile0A2834D7", + "Arn" + ] + } + }, + "imageId": { + "Ref": "SsmParameterValueawsserviceamiamazonlinuxlatestamznamihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter" + }, + "instanceType": "t2.micro", + "monitoring": { + "enabled": false + }, + "securityGroupIds": [ + { + "Fn::GetAtt": [ + "ASGInstanceSecurityGroup0525485D", + "GroupId" + ] + } + ], + "tagSpecifications": [ + { + "resourceType": "instance", + "tags": [ + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/ASG/LaunchTemplate" + } + ] + }, + { + "resourceType": "volume", + "tags": [ + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/ASG/LaunchTemplate" + } + ] + } + ], + "userData": { + "Fn::Base64": "#!/bin/bash" + } + }, + "tagSpecifications": [ + { + "resourceType": "launch-template", + "tags": [ + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/ASG/LaunchTemplate" + } + ] + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.CfnLaunchTemplate", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ec2.LaunchTemplate", + "version": "0.0.0" + } + }, + "ASG": { + "id": "ASG", + "path": "aws-cdk-autoscaling-instance-termination-policy/ASG/ASG", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::AutoScaling::AutoScalingGroup", + "aws:cdk:cloudformation:props": { + "launchTemplate": { + "launchTemplateId": { + "Ref": "ASGLaunchTemplate0CA92847" + }, + "version": { + "Fn::GetAtt": [ + "ASGLaunchTemplate0CA92847", + "LatestVersionNumber" + ] + } + }, + "maxSize": "1", + "minSize": "1", + "tags": [ + { + "key": "Name", + "value": "aws-cdk-autoscaling-instance-termination-policy/ASG", + "propagateAtLaunch": true + } + ], + "terminationPolicies": [ + { + "Fn::GetAtt": [ + "MyFunction3BAA72D1", + "Arn" + ] + }, + "OldestInstance" + ], + "vpcZoneIdentifier": [ + { + "Ref": "VPCIsolatedSubnet1SubnetEBD00FC6" + }, + { + "Ref": "VPCIsolatedSubnet2Subnet4B1C8CAA" + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_autoscaling.CfnAutoScalingGroup", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_autoscaling.AutoScalingGroup", + "version": "0.0.0" + } + }, + "SsmParameterValue:--aws--service--ami-amazon-linux-latest--amzn-ami-hvm-x86_64-gp2:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter": { + "id": "SsmParameterValue:--aws--service--ami-amazon-linux-latest--amzn-ami-hvm-x86_64-gp2:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter", + "path": "aws-cdk-autoscaling-instance-termination-policy/SsmParameterValue:--aws--service--ami-amazon-linux-latest--amzn-ami-hvm-x86_64-gp2:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "SsmParameterValue:--aws--service--ami-amazon-linux-latest--amzn-ami-hvm-x86_64-gp2:C96584B6-F00A-464E-AD19-53AFF4B05118": { + "id": "SsmParameterValue:--aws--service--ami-amazon-linux-latest--amzn-ami-hvm-x86_64-gp2:C96584B6-F00A-464E-AD19-53AFF4B05118", + "path": "aws-cdk-autoscaling-instance-termination-policy/SsmParameterValue:--aws--service--ami-amazon-linux-latest--amzn-ami-hvm-x86_64-gp2:C96584B6-F00A-464E-AD19-53AFF4B05118", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "aws-cdk-autoscaling-instance-termination-policy/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "aws-cdk-autoscaling-instance-termination-policy/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "InstanceTerminationPolicyTest": { + "id": "InstanceTerminationPolicyTest", + "path": "InstanceTerminationPolicyTest", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "InstanceTerminationPolicyTest/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "InstanceTerminationPolicyTest/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "InstanceTerminationPolicyTest/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "InstanceTerminationPolicyTest/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "InstanceTerminationPolicyTest/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", + "version": "0.0.0" + } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.ts new file mode 100644 index 0000000000000..67083dc8e8fe3 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-autoscaling/test/integ.asg-instance-termination-policy.ts @@ -0,0 +1,58 @@ +#!/usr/bin/env node +import * as ec2 from 'aws-cdk-lib/aws-ec2'; +import * as cdk from 'aws-cdk-lib'; +import * as integ from '@aws-cdk/integ-tests-alpha'; +import * as autoscaling from 'aws-cdk-lib/aws-autoscaling'; +import * as lambda from 'aws-cdk-lib/aws-lambda'; +import * as iam from 'aws-cdk-lib/aws-iam'; + +const app = new cdk.App(); +const stack = new cdk.Stack(app, 'aws-cdk-autoscaling-instance-termination-policy'); + +const vpc = new ec2.Vpc(stack, 'VPC', { + natGateways: 0, + maxAzs: 2, + restrictDefaultSecurityGroup: false, +}); + +const lambdaFn = new lambda.Function(stack, 'MyFunction', { + code: lambda.Code.fromInline(`exports.handler = ${asgHandler}`), + handler: 'index.handler', + runtime: lambda.Runtime.NODEJS_20_X, +}); + +lambdaFn.addPermission('AllowInvokeByAutoScaling', { + action: 'lambda:InvokeFunction', + principal: new iam.ArnPrincipal(`arn:${stack.partition}:iam::${stack.account}:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling`), +}); + +const asg = new autoscaling.AutoScalingGroup(stack, 'ASG', { + vpc, + instanceType: new ec2.InstanceType('t2.micro'), + machineImage: new ec2.AmazonLinuxImage(), + terminationPolicies: [ + autoscaling.TerminationPolicy.CUSTOM_LAMBDA_FUNCTION, + autoscaling.TerminationPolicy.OLDEST_INSTANCE, + ], + terminationPolicyCustomLambdaFunctionArn: lambdaFn.functionArn, +}); + +asg.node.addDependency(lambdaFn); +asg.node.addDependency(vpc); + +new integ.IntegTest(app, 'InstanceTerminationPolicyTest', { + testCases: [stack], +}); + +app.synth(); + +function asgHandler(_event: any, _context: any, callback: any) { + let json = JSON.parse(JSON.stringify(_event)); + let instanceIds: string[] = []; + json.Instances.forEach((instance: any) => { + instanceIds.push(instance.InstanceId); + }); + // eslint-disable-next-line quote-props + const result = { 'InstanceIDs': instanceIds }; + return callback(undefined, result); +} \ No newline at end of file diff --git a/packages/aws-cdk-lib/aws-autoscaling/README.md b/packages/aws-cdk-lib/aws-autoscaling/README.md index 99ac388f81a31..ff03c316549ed 100644 --- a/packages/aws-cdk-lib/aws-autoscaling/README.md +++ b/packages/aws-cdk-lib/aws-autoscaling/README.md @@ -539,10 +539,22 @@ to determine which instances it terminates first during scale-in events. You can specify one or more termination policies with the `terminationPolicies` property: +[Custom termination policy](https://docs.aws.amazon.com/autoscaling/ec2/userguide/lambda-custom-termination-policy.html) with lambda +can be used to determine which instances to terminate based on custom logic. +The custom termination policy can be specified using `TerminationPolicy.CUSTOM_LAMBDA_FUNCTION`. If this is +specified, you must also supply a value of lambda arn in the `terminationPolicyCustomLambdaFunctionArn` property and +attach necessary [permission](https://docs.aws.amazon.com/autoscaling/ec2/userguide/lambda-custom-termination-policy.html#lambda-custom-termination-policy-create-function) +to invoke the lambda function. + +If there are multiple termination policies specified, +custom termination policy with lambda `TerminationPolicy.CUSTOM_LAMBDA_FUNCTION` +must be specified first. + ```ts declare const vpc: ec2.Vpc; declare const instanceType: ec2.InstanceType; declare const machineImage: ec2.IMachineImage; +declare const arn: string; new autoscaling.AutoScalingGroup(this, 'ASG', { vpc, @@ -552,9 +564,13 @@ new autoscaling.AutoScalingGroup(this, 'ASG', { // ... terminationPolicies: [ + autoscaling.TerminationPolicy.CUSTOM_LAMBDA_FUNCTION, autoscaling.TerminationPolicy.OLDEST_INSTANCE, autoscaling.TerminationPolicy.DEFAULT, ], + + //terminationPolicyCustomLambdaFunctionArn property must be specified if the TerminationPolicy.CUSTOM_LAMBDA_FUNCTION is used + terminationPolicyCustomLambdaFunctionArn: arn, }); ``` diff --git a/packages/aws-cdk-lib/aws-autoscaling/lib/auto-scaling-group.ts b/packages/aws-cdk-lib/aws-autoscaling/lib/auto-scaling-group.ts index d968b7a3f2706..dee4d5b3a372a 100644 --- a/packages/aws-cdk-lib/aws-autoscaling/lib/auto-scaling-group.ts +++ b/packages/aws-cdk-lib/aws-autoscaling/lib/auto-scaling-group.ts @@ -338,6 +338,17 @@ export interface CommonAutoScalingGroupProps { */ readonly terminationPolicies?: TerminationPolicy[]; + /** + * A lambda function Arn that can be used as a custom termination policy to select the instances + * to terminate. This property must be specified if the TerminationPolicy.CUSTOM_LAMBDA_FUNCTION + * is used. + * + * @see https://docs.aws.amazon.com/autoscaling/ec2/userguide/lambda-custom-termination-policy.html + * + * @default - No lambda function Arn will be supplied + */ + readonly terminationPolicyCustomLambdaFunctionArn?: string; + /** * The amount of time, in seconds, until a newly launched instance can contribute to the Amazon CloudWatch metrics. * This delay lets an instance finish initializing before Amazon EC2 Auto Scaling aggregates instance metrics, @@ -1471,6 +1482,26 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements } const { subnetIds, hasPublic } = props.vpc.selectSubnets(props.vpcSubnets); + + const terminationPolicies: string[] = []; + if (props.terminationPolicies) { + props.terminationPolicies.forEach((terminationPolicy, index) => { + if (terminationPolicy === TerminationPolicy.CUSTOM_LAMBDA_FUNCTION) { + if (index !== 0) { + throw new Error('TerminationPolicy.CUSTOM_LAMBDA_FUNCTION must be specified first in the termination policies'); + } + + if (!props.terminationPolicyCustomLambdaFunctionArn) { + throw new Error('terminationPolicyCustomLambdaFunctionArn property must be specified if the TerminationPolicy.CUSTOM_LAMBDA_FUNCTION is used'); + } + + terminationPolicies.push(props.terminationPolicyCustomLambdaFunctionArn); + } else { + terminationPolicies.push(terminationPolicy); + } + }); + } + const asgProps: CfnAutoScalingGroupProps = { autoScalingGroupName: this.physicalName, cooldown: props.cooldown?.toSeconds().toString(), @@ -1486,7 +1517,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements healthCheckGracePeriod: props.healthCheck && props.healthCheck.gracePeriod && props.healthCheck.gracePeriod.toSeconds(), maxInstanceLifetime: this.maxInstanceLifetime ? this.maxInstanceLifetime.toSeconds() : undefined, newInstancesProtectedFromScaleIn: Lazy.any({ produce: () => this.newInstancesProtectedFromScaleIn }), - terminationPolicies: props.terminationPolicies, + terminationPolicies: terminationPolicies.length === 0 ? undefined : terminationPolicies, defaultInstanceWarmup: props.defaultInstanceWarmup?.toSeconds(), capacityRebalance: props.capacityRebalance, instanceMaintenancePolicy: this.renderInstanceMaintenancePolicy( diff --git a/packages/aws-cdk-lib/aws-autoscaling/lib/termination-policy.ts b/packages/aws-cdk-lib/aws-autoscaling/lib/termination-policy.ts index 1fe6b4d139459..92483cb2683bc 100644 --- a/packages/aws-cdk-lib/aws-autoscaling/lib/termination-policy.ts +++ b/packages/aws-cdk-lib/aws-autoscaling/lib/termination-policy.ts @@ -39,4 +39,14 @@ export enum TerminationPolicy { * Terminate instances that have the oldest launch template. */ OLDEST_LAUNCH_TEMPLATE = 'OldestLaunchTemplate', + + /** + * Terminate instances using custom termination policy with lambda. If this is + * specified, you must also supply a value of lambda arn in the terminationPolicyCustomLambdaFunctionArn property. + * + * If there are multiple termination policies specified, the custom termination policy with lambda + * must be specified first in the order. + * @see https://docs.aws.amazon.com/autoscaling/ec2/userguide/lambda-custom-termination-policy.html#lambda-custom-termination-policy-limitations + */ + CUSTOM_LAMBDA_FUNCTION = 'CustomLambdaFunction', } diff --git a/packages/aws-cdk-lib/aws-autoscaling/test/auto-scaling-group.test.ts b/packages/aws-cdk-lib/aws-autoscaling/test/auto-scaling-group.test.ts index f6c8b8fbe6295..df0b4f6c40c59 100644 --- a/packages/aws-cdk-lib/aws-autoscaling/test/auto-scaling-group.test.ts +++ b/packages/aws-cdk-lib/aws-autoscaling/test/auto-scaling-group.test.ts @@ -1678,6 +1678,85 @@ describe('auto scaling group', () => { }); }); + test('supports custom termination policy with lambda function arn specified', () => { + // GIVEN + const stack = new cdk.Stack(); + const vpc = mockVpc(stack); + const arn = stack.formatArn({ + service: 'lambda', + resource: 'function', + account: '123456789012', + region: 'us-east-1', + partition: 'aws', + resourceName: 'CustomTerminationPolicyLambda:1', + }); + + // WHEN + new autoscaling.AutoScalingGroup(stack, 'MyASG', { + vpc, + instanceType: new ec2.InstanceType('t2.micro'), + machineImage: new ec2.AmazonLinuxImage(), + terminationPolicies: [ + autoscaling.TerminationPolicy.CUSTOM_LAMBDA_FUNCTION, + autoscaling.TerminationPolicy.OLDEST_INSTANCE, + autoscaling.TerminationPolicy.DEFAULT, + ], + terminationPolicyCustomLambdaFunctionArn: arn, + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::AutoScaling::AutoScalingGroup', { + TerminationPolicies: [ + arn, + 'OldestInstance', + 'Default', + ], + }); + }); + + test('Should specify TerminationPolicy.CUSTOM_LAMBDA_FUNCTION in first', () => { + // GIVEN + const stack = new cdk.Stack(); + const vpc = mockVpc(stack); + + // WHEN + const terminationPolicies = [ + autoscaling.TerminationPolicy.DEFAULT, + autoscaling.TerminationPolicy.CUSTOM_LAMBDA_FUNCTION, + ]; + + // THEN + expect(() => { + new autoscaling.AutoScalingGroup(stack, 'MyASG', { + vpc, + instanceType: new ec2.InstanceType('t2.micro'), + machineImage: new ec2.AmazonLinuxImage(), + terminationPolicies: terminationPolicies, + }); + }).toThrow('TerminationPolicy.CUSTOM_LAMBDA_FUNCTION must be specified first in the termination policies'); + }); + + test('Should specify terminationPolicyCustomLambdaFunctionArn property if TerminationPolicy.CUSTOM_LAMBDA_FUNCTION is used', () => { + // GIVEN + const stack = new cdk.Stack(); + const vpc = mockVpc(stack); + + // WHEN + const terminationPolicies = [ + autoscaling.TerminationPolicy.CUSTOM_LAMBDA_FUNCTION, + ]; + + // THEN + expect(() => { + new autoscaling.AutoScalingGroup(stack, 'MyASG', { + vpc, + instanceType: new ec2.InstanceType('t2.micro'), + machineImage: new ec2.AmazonLinuxImage(), + terminationPolicies: terminationPolicies, + }); + }).toThrow('terminationPolicyCustomLambdaFunctionArn property must be specified if the TerminationPolicy.CUSTOM_LAMBDA_FUNCTION is used'); + }); + test('Can use imported Launch Template with ID', () => { // GIVEN const stack = new cdk.Stack();