From 6e824670fc2cfd0081b3b31c855dbfe55a85c469 Mon Sep 17 00:00:00 2001 From: Michael Chandler Date: Sun, 6 Oct 2024 13:44:47 +1000 Subject: feat: Migrate from EC2 Launch Configuration to Launch Template. --- cf.yml | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) (limited to 'cf.yml') diff --git a/cf.yml b/cf.yml index b269586..2eef48b 100644 --- a/cf.yml +++ b/cf.yml @@ -144,7 +144,8 @@ Resources: - !GetAZs Ref: 'AWS::Region' CidrBlock: !Select [ 0, !Cidr [ 10.100.0.0/26, 4, 4 ] ] - VpcId: !Ref Vpc + VpcId: !Ref Vpc + MapPublicIpOnLaunch: true SubnetARoute: Type: AWS::EC2::SubnetRouteTableAssociation @@ -167,6 +168,7 @@ Resources: Ref: 'AWS::Region' CidrBlock: !Select [ 1, !Cidr [ 10.100.0.0/26, 4, 4 ] ] VpcId: !Ref Vpc + MapPublicIpOnLaunch: true InternetGateway: Type: AWS::EC2::InternetGateway @@ -260,29 +262,41 @@ Resources: - !Ref 'AWS::NoValue' VpcId: !Ref Vpc - LaunchConfiguration: - Type: AWS::AutoScaling::LaunchConfiguration + LaunchTemplate: + Type: AWS::EC2::LaunchTemplate Properties: - AssociatePublicIpAddress: true - IamInstanceProfile: !Ref InstanceProfile - ImageId: !Ref ECSAMI - InstanceType: !Ref InstanceType - KeyName: - !If [ KeyPairNameProvided, !Ref KeyPairName, !Ref 'AWS::NoValue' ] - SecurityGroups: - - !Ref Ec2Sg - SpotPrice: !If [ SpotPriceProvided, !Ref SpotPrice, !Ref 'AWS::NoValue' ] - UserData: - Fn::Base64: !Sub | - #!/bin/bash -xe - echo ECS_CLUSTER=${EcsCluster} >> /etc/ecs/ecs.config + LaunchTemplateName: !Sub ${AWS::StackName}-launch-template + LaunchTemplateData: + IamInstanceProfile: + Arn: !GetAtt InstanceProfile.Arn + ImageId: !Ref ECSAMI + SecurityGroupIds: + - !Ref Ec2Sg + KeyName: + !If [ KeyPairNameProvided, !Ref KeyPairName, !Ref 'AWS::NoValue' ] + UserData: + Fn::Base64: !Sub | + #!/bin/bash -xe + echo ECS_CLUSTER=${EcsCluster} >> /etc/ecs/ecs.config AutoScalingGroup: Type: AWS::AutoScaling::AutoScalingGroup Properties: AutoScalingGroupName: !Sub "${AWS::StackName}-asg" DesiredCapacity: !FindInMap [ ServerState, !Ref ServerState, DesiredCapacity ] - LaunchConfigurationName: !Ref LaunchConfiguration + MixedInstancesPolicy: + InstancesDistribution: + OnDemandPercentageAboveBaseCapacity: + !If [ SpotPriceProvided, 0, 100 ] + SpotAllocationStrategy: price-capacity-optimized + SpotMaxPrice: + !If [ SpotPriceProvided, !Ref SpotPrice, !Ref AWS::NoValue ] + LaunchTemplate: + LaunchTemplateSpecification: + LaunchTemplateId: !Ref LaunchTemplate + Version: !GetAtt LaunchTemplate.LatestVersionNumber + Overrides: + - InstanceType: !Ref InstanceType MaxSize: !FindInMap [ ServerState, !Ref ServerState, DesiredCapacity ] MinSize: !FindInMap [ ServerState, !Ref ServerState, DesiredCapacity ] VPCZoneIdentifier: -- cgit v1.2.3