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_policy
optional - string -
id
optional computed - string -
replace_unhealthy_instances
optional - bool -
tags
optional - map from string to string -
terminate_instances
optional - bool -
terminate_instances_with_expiration
optional - bool -
type
optional - string -
launch_template_config
list block-
launch_template_specification
list block-
launch_template_id
optional - string -
launch_template_name
optional - string -
version
required - string
-
-
override
list block-
availability_zone
optional - string -
instance_type
optional - string -
max_price
optional - string -
priority
optional - number -
subnet_id
optional - string -
weighted_capacity
optional - number
-
-
-
on_demand_options
list block-
allocation_strategy
optional - string
-
-
spot_options
list block-
allocation_strategy
optional - string -
instance_interruption_behavior
optional - string -
instance_pools_to_use_count
optional - number -
maintenance_strategies
list block-
capacity_rebalance
list block-
replacement_strategy
optional - string
-
-
-
-
target_capacity_specification
list block-
default_target_capacity_type
required - string -
on_demand_target_capacity
optional - number -
spot_target_capacity
optional - number -
total_target_capacity
required - number
-
-
timeouts
single 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
-
TargetCapacitySpecification
required - TargetCapacitySpecificationRequest -
OnDemandOptions
optional - OnDemandOptionsRequest -
Type
optional - String -
ExcessCapacityTerminationPolicy
optional - String -
TagSpecifications
optional - List of TagSpecification -
SpotOptions
optional - SpotOptionsRequest -
ValidFrom
optional - String -
ReplaceUnhealthyInstances
optional - Boolean -
LaunchTemplateConfigs
required - List of FleetLaunchTemplateConfigRequest -
TerminateInstancesWithExpiration
optional - Boolean -
ValidUntil
optional - String -
Context
optional - 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.