Skip to content

Instantly share code, notes, and snippets.

@jpalomaki
Last active November 17, 2023 01:04
Show Gist options
  • Save jpalomaki/003c4d173a856cf64c6d35f8869a2de8 to your computer and use it in GitHub Desktop.
Save jpalomaki/003c4d173a856cf64c6d35f8869a2de8 to your computer and use it in GitHub Desktop.
AWSTemplateFormatVersion: 2010-09-09
Description: Configures an EC2 launch template
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: General
Parameters:
- Name
- ImageId
- InstanceType
- Ec2KeyPair
- Label:
default: Network
Parameters:
- SubnetId
- SecurityGroupIds
- AssociatePublicIp
- Label:
default: Storage
Parameters:
- EbsDeviceName
- EbsVolumeType
- EbsVolumeSize
- Label:
default: Advanced
Parameters:
- UseSpotInstances
- InstanceProfileArn
- TerminateOnShutdown
- UseIMDSv2
Parameters:
Name:
Type: String
Description: Launch template name
ImageId:
Type: AWS::EC2::Image::Id
Description: Image (AMI) ID
InstanceType:
Type: String
Description: Instance type
SecurityGroupIds:
Type: List<AWS::EC2::SecurityGroup::Id>
Description: List of VPC security groups IDs
Ec2KeyPair:
Type: String
Description: SSH key pair name (optional)
Default: ''
SubnetId:
Type: String
Description: VPC subnet ID (optional)
Default: ''
AssociatePublicIp:
Type: String
Description: Associate public IPv4 address at launch?
AllowedValues:
- true
- false
Default: false
EbsDeviceName:
Type: String
Description: EBS root volume device name. Adjust per AMI
Default: /dev/sda1
EbsVolumeType:
Type: String
Description: EBS root volume type
Default: gp3
EbsVolumeSize:
Type: Number
Description: EBS root volume size (GiB)
Default: 8
UseSpotInstances:
Type: String
Description: Use spot instances for cost savings?
AllowedValues:
- true
- false
Default: false
InstanceProfileArn:
Type: String
Description: IAM instance profile ARN (optional)
Default: ''
TerminateOnShutdown:
Type: String
Description: Terminate instance on OS-initiated shutdown?
AllowedValues:
- true
- false
Default: false
UseIMDSv2:
Type: String
Description: Use IMDSv2 (requiring tokens for accessing instance metadata)?
AllowedValues:
- true
- false
Default: true
Conditions:
SetSubnetId: !Not [ !Equals [ !Ref SubnetId, ''] ]
SetSpotMarketOptions: !Equals [ !Ref UseSpotInstances, true ]
SetInstanceProfile: !Not [ !Equals [ !Ref InstanceProfileArn, ''] ]
TerminateOnShutdown: !Equals [ !Ref TerminateOnShutdown, true ]
SetKeyPair: !Not [ !Equals [ !Ref Ec2KeyPair, ''] ]
UseIMDSv2: !Equals [ !Ref UseIMDSv2, true ]
Resources:
LaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateName: !Ref Name
LaunchTemplateData:
ImageId: !Ref ImageId
InstanceType: !Ref InstanceType
IamInstanceProfile: !If [ SetInstanceProfile, { Arn: !Ref InstanceProfileArn }, !Ref AWS::NoValue ]
InstanceInitiatedShutdownBehavior: !If [ TerminateOnShutdown, terminate, !Ref AWS::NoValue ]
KeyName: !If [ SetKeyPair, !Ref Ec2KeyPair, !Ref AWS::NoValue ]
NetworkInterfaces:
- DeviceIndex: 0
Groups: !Ref SecurityGroupIds
AssociatePublicIpAddress: !Ref AssociatePublicIp
SubnetId: !If [ SetSubnetId, !Ref SubnetId, !Ref AWS::NoValue ]
DeleteOnTermination: true
InstanceMarketOptions: !If [ SetSpotMarketOptions, { MarketType: spot }, !Ref AWS::NoValue ]
BlockDeviceMappings:
- DeviceName: !Ref EbsDeviceName
Ebs:
VolumeType: !Ref EbsVolumeType
VolumeSize: !Ref EbsVolumeSize
DeleteOnTermination: true
MetadataOptions:
HttpTokens: !If [ UseIMDSv2, required, optional ]
TagSpecifications:
- ResourceType: instance
Tags:
- Key: Name
Value: GitHub self-hosted runner
Outputs:
TemplateId:
Description: Launch template ID
Value: !Ref LaunchTemplate
TemplateName:
Description: Launch template name
Value: !Ref Name
TemplateVersion:
Description: Launch template version (latest)
Value: !GetAtt LaunchTemplate.LatestVersionNumber
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment