AWS Amazon EC2 Fleet
This page shows how to write Terraform and CloudFormation for Amazon EC2 Fleet and write them securely.
aws_ec2_fleet (Terraform)
The Fleet in Amazon EC2 can be configured in Terraform with the resource name aws_ec2_fleet. The following sections describe 4 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_ec2_fleet" "main" {
type = var.ec2_fleet_type
tags = merge({ Name = var.ec2_fleet_name }, var.tags, var.ec2_fleet_tags)
terminate_instances = var.ec2_terminate_instances
excess_capacity_termination_policy = var.ec2_fleet_excess_capacity_termination_policy
resource "aws_ec2_fleet" "example" {
launch_template_config {
launch_template_specification {
# launch_template_id = "lt-0bd6ecc75445e53cb"
launch_template_id = aws_launch_template.launch-template.id
# launch_template_id = data.aws_launch_template.default.id
resource "aws_ec2_fleet" "example" {
launch_template_config {
launch_template_specification {
# launch_template_id = "lt-0bd6ecc75445e53cb"
launch_template_id = aws_launch_template.launch-template.id
# launch_template_id = data.aws_launch_template.default.id
resource "aws_ec2_fleet" "this" {
excess_capacity_termination_policy = var.excess_capacity_termination_policy
replace_unhealthy_instances = var.replace_unhealthy_instances
tags = var.tags
terminate_instances = var.terminate_instances
terminate_instances_with_expiration = var.terminate_instances_with_expiration
Parameters
-
excess_capacity_termination_policyoptional - string -
idoptional computed - string -
replace_unhealthy_instancesoptional - bool -
tagsoptional - map from string to string -
terminate_instancesoptional - bool -
terminate_instances_with_expirationoptional - bool -
typeoptional - string -
launch_template_configlist block-
launch_template_specificationlist block-
launch_template_idoptional - string -
launch_template_nameoptional - string -
versionrequired - string
-
-
overridelist block-
availability_zoneoptional - string -
instance_typeoptional - string -
max_priceoptional - string -
priorityoptional - number -
subnet_idoptional - string -
weighted_capacityoptional - number
-
-
-
on_demand_optionslist block-
allocation_strategyoptional - string
-
-
spot_optionslist block-
allocation_strategyoptional - string -
instance_interruption_behavioroptional - string -
instance_pools_to_use_countoptional - number -
maintenance_strategieslist block-
capacity_rebalancelist block-
replacement_strategyoptional - string
-
-
-
-
target_capacity_specificationlist block-
default_target_capacity_typerequired - string -
on_demand_target_capacityoptional - number -
spot_target_capacityoptional - number -
total_target_capacityrequired - number
-
-
timeoutssingle block
Explanation in Terraform Registry
Provides a resource to manage EC2 Fleets.
Tips: Best Practices for The Other AWS Amazon EC2 Resources
In addition to the aws_default_vpc, AWS Amazon EC2 has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
aws_default_vpc
Ensure to avoid using default VPC
It is better to define the own VPC and use it.
aws_network_acl_rule
Ensure your network ACL rule blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
aws_ebs_volume
Ensure to use a customer-managed key for EBS volume encryption
It is better to use a customer-managed key for EBS volume encryption. It can be gain more control over the encryption by using customer-managed keys (CMK).
aws_instance
Ensure to avoid storing AWS access keys in user data
It is better to avoid storing AWS access keys in user data. `aws_iam_instance_profile` could be used instead.
aws_security_group
Ensure your security group blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
AWS::EC2::EC2Fleet (CloudFormation)
The EC2Fleet in EC2 can be configured in CloudFormation with the resource name AWS::EC2::EC2Fleet. The following sections describe 10 examples of how to use the resource and its parameters.
Example Usage from GitHub
Type: AWS::EC2::EC2Fleet
DependsOn: WebServerEC2LaunchTemplate
Properties:
LaunchTemplateConfigs:
- LaunchTemplateSpecification:
LaunchTemplateName: WebServerEC2LaunchTemplate
Type: AWS::EC2::EC2Fleet
Properties:
ExcessCapacityTerminationPolicy: termination
LaunchTemplateConfigs:
- LaunchTemplateSpecification:
LaunchTemplateId: !Ref EC2LaunchTemplate
Type: AWS::EC2::EC2Fleet
Properties:
LaunchTemplateConfigs:
- LaunchTemplateSpecification:
LaunchTemplateId: !Ref WorkerNodeLaunchTemplate
Version: 1
Type: AWS::EC2::EC2Fleet
Properties:
LaunchTemplateConfigs:
- LaunchTemplateSpecification:
LaunchTemplateId: !Ref WorkerNodeLaunchTemplate
Version: 1
Type: AWS::EC2::EC2Fleet
Properties:
ExcessCapacityTerminationPolicy: String
LaunchTemplateConfigs:
- FleetLaunchTemplateConfigRequest
OnDemandOptions:
"AWS::EC2::EC2Fleet.OnDemandOptionsRequest": {
"Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-ec2fleet-ondemandoptionsrequest.html",
"Properties": {
"SingleAvailabilityZone": {
"Required": false,
"Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-ec2fleet-ondemandoptionsrequest.html#cfn-ec2-ec2fleet-ondemandoptionsrequest-singleavailabilityzone",
"AWS::EC2::EC2Fleet.OnDemandOptionsRequest": {
"Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-ec2fleet-ondemandoptionsrequest.html",
"Properties": {
"AllocationStrategy": {
"Required": false,
"Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-ec2fleet-ondemandoptionsrequest.html#cfn-ec2-ec2fleet-ondemandoptionsrequest-allocationstrategy",
"AWS::EC2::EC2Fleet.OnDemandOptionsRequest": {
"Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-ec2fleet-ondemandoptionsrequest.html",
"Properties": {
"AllocationStrategy": {
"Required": false,
"Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-ec2fleet-ondemandoptionsrequest.html#cfn-ec2-ec2fleet-ondemandoptionsrequest-allocationstrategy",
"AWS::EC2::EC2Fleet.OnDemandOptionsRequest": {
"Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-ec2fleet-ondemandoptionsrequest.html",
"Properties": {
"AllocationStrategy": {
"Required": false,
"Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-ec2fleet-ondemandoptionsrequest.html#cfn-ec2-ec2fleet-ondemandoptionsrequest-allocationstrategy",
"AWS::EC2::EC2Fleet.OnDemandOptionsRequest": {
"Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-ec2fleet-ondemandoptionsrequest.html",
"Properties": {
"AllocationStrategy": {
"Required": false,
"Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-ec2fleet-ondemandoptionsrequest.html#cfn-ec2-ec2fleet-ondemandoptionsrequest-allocationstrategy",
Parameters
-
TargetCapacitySpecificationrequired - TargetCapacitySpecificationRequest -
OnDemandOptionsoptional - OnDemandOptionsRequest -
Typeoptional - String -
ExcessCapacityTerminationPolicyoptional - String -
TagSpecificationsoptional - List of TagSpecification -
SpotOptionsoptional - SpotOptionsRequest -
ValidFromoptional - String -
ReplaceUnhealthyInstancesoptional - Boolean -
LaunchTemplateConfigsrequired - List of FleetLaunchTemplateConfigRequest -
TerminateInstancesWithExpirationoptional - Boolean -
ValidUntiloptional - String -
Contextoptional - String
Explanation in CloudFormation Registry
Specifies the configuration information to launch a fleet--or group--of instances. An EC2 Fleet can launch multiple instance types across multiple Availability Zones, using the On-Demand Instance, Reserved Instance, and Spot Instance purchasing models together. Using EC2 Fleet, you can define separate On-Demand and Spot capacity targets, specify the instance types that work best for your applications, and specify how Amazon EC2 should distribute your fleet capacity within each purchasing model. For more information, see Launching an EC2 Fleet in the Amazon EC2 User Guide for Linux Instances.
Frequently asked questions
What is AWS Amazon EC2 Fleet?
AWS Amazon EC2 Fleet is a resource for Amazon EC2 of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.
Where can I find the example code for the AWS Amazon EC2 Fleet?
For Terraform, the concord-workflow/concord-terraform, mike23101994/terraform-scripts and mike23101994/terraform-scripts source code examples are useful. See the Terraform Example section for further details.
For CloudFormation, the hpsawant7887/ec2-infra-stack, Manoj2087/cloudformation-template and ianp5uk/cfn-aws-demo source code examples are useful. See the CloudFormation Example section for further details.