AWS Amazon EC2 Fleet Request
This page shows how to write Terraform and CloudFormation for Amazon EC2 Fleet Request and write them securely.
aws_spot_fleet_request (Terraform)
The Fleet Request in Amazon EC2 can be configured in Terraform with the resource name aws_spot_fleet_request
. The following sections describe 5 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_spot_fleet_request" "debian" {
iam_fleet_role = "arn:aws:iam::652694334613:role/aws-service-role/spotfleet.amazonaws.com/AWSServiceRoleForEC2SpotFleet"
target_capacity = 0
# terminate_instances = true
terminate_instances_with_expiration = true
resource "aws_spot_fleet_request" "master_nodes" {
iam_fleet_role = "arn:aws:iam::345025288683:role/aws-ec2-spot-fleet-tagging-role"
spot_price = var.master_bid
allocation_strategy = "lowestPrice"
target_capacity = 1
valid_until = timeadd(timestamp(), var.spot_time)
resource "aws_spot_fleet_request" "test" {
iam_fleet_role = "arn:aws:iam::695507447459:role/aws-ec2-spot-fleet-tagging-role"
}
resource "aws_launch_template" "test_spot_fleet" {
name_prefix = "spot-"
resource "aws_spot_fleet_request" "test" {
iam_fleet_role = aws_iam_role.test.arn
spot_price = "0.05"
target_capacity = 1
terminate_instances_with_expiration = true
valid_until = "01/01/2222"
resource "aws_spot_fleet_request" "spot-ssm-example-spot-request" {
iam_fleet_role = aws_iam_role.role.arn
target_capacity = 1
terminate_instances_with_expiration = true
wait_for_fulfillment = "true"
Parameters
-
allocation_strategy
optional - string -
client_token
optional computed - string -
excess_capacity_termination_policy
optional - string -
fleet_type
optional - string -
iam_fleet_role
required - string -
id
optional computed - string -
instance_interruption_behaviour
optional - string -
instance_pools_to_use_count
optional - number -
load_balancers
optional computed - set of string -
replace_unhealthy_instances
optional - bool -
spot_price
optional - string -
spot_request_state
optional computed - string -
tags
optional - map from string to string -
target_capacity
required - number -
target_group_arns
optional computed - set of string -
terminate_instances_with_expiration
optional - bool -
valid_from
optional - string -
valid_until
optional - string -
wait_for_fulfillment
optional - bool -
launch_specification
set block-
ami
required - string -
associate_public_ip_address
optional - bool -
availability_zone
optional computed - string -
ebs_optimized
optional - bool -
iam_instance_profile
optional - string -
iam_instance_profile_arn
optional - string -
instance_type
required - string -
key_name
optional computed - string -
monitoring
optional - bool -
placement_group
optional computed - string -
placement_tenancy
optional - string -
spot_price
optional - string -
subnet_id
optional computed - string -
tags
optional - map from string to string -
user_data
optional - string -
vpc_security_group_ids
optional computed - set of string -
weighted_capacity
optional - string -
ebs_block_device
set block-
delete_on_termination
optional - bool -
device_name
required - string -
encrypted
optional computed - bool -
iops
optional computed - number -
kms_key_id
optional computed - string -
snapshot_id
optional computed - string -
throughput
optional computed - number -
volume_size
optional computed - number -
volume_type
optional computed - string
-
-
ephemeral_block_device
set block-
device_name
required - string -
virtual_name
required - string
-
-
root_block_device
set block-
delete_on_termination
optional - bool -
encrypted
optional computed - bool -
iops
optional computed - number -
kms_key_id
optional computed - string -
throughput
optional computed - number -
volume_size
optional computed - number -
volume_type
optional computed - string
-
-
-
launch_template_config
set block-
launch_template_specification
list block -
overrides
set block-
availability_zone
optional - string -
instance_type
optional - string -
priority
optional computed - number -
spot_price
optional computed - string -
subnet_id
optional computed - string -
weighted_capacity
optional computed - number
-
-
-
spot_maintenance_strategies
list block-
capacity_rebalance
list block-
replacement_strategy
optional - string
-
-
-
timeouts
single block
Explanation in Terraform Registry
Provides an EC2 Spot Fleet Request resource. This allows a fleet of Spot instances to be requested on the Spot market.
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::SpotFleet (CloudFormation)
The SpotFleet in EC2 can be configured in CloudFormation with the resource name AWS::EC2::SpotFleet
. The following sections describe 6 examples of how to use the resource and its parameters.
Example Usage from GitHub
Type: AWS::EC2::SpotFleet
Properties:
SpotFleetRequestConfigData:
AllocationStrategy: diversified
IamFleetRole: !GetAtt SpotFleetRole.Arn
LaunchTemplateConfigs:
"resourceType" : "AWS::EC2::SpotFleet",
"properties" : [ {
"propertyName" : "SpotFleetRequestConfigData",
"propertyType" : "AWS::EC2::SpotFleet::SpotFleetRequestConfigDatum",
"required" : true,
"propertyHref" : "aws-properties-ec2-spotfleet-spotfleetrequestconfigdata.html"
"Type" : "AWS::EC2::SpotFleet",
"Properties" : {
"SpotFleetRequestConfigData" : {
"IamFleetRole" : "arn:aws:iam::<insert account id here>:role/aws-ec2-spot-fleet-role",
"LaunchSpecifications" : [ {
"ImageId" : "ami-f2210191",
"Type" : "AWS::EC2::SpotFleet",
"Properties" : {
"SpotFleetRequestConfigData" : {
"IamFleetRole" : "arn:aws:iam::082449889088:role/aws-ec2-spot-fleet-role",
"LaunchSpecifications" : [ {
"ImageId" : "ami-b70554c8",
"Type": "AWS::EC2::SpotFleet",
"Properties": {
"SpotFleetRequestConfigData": {
"IamFleetRole": { "Ref": "IAMFleetRole" },
"SpotPrice": "1000",
"TargetCapacity": { "Ref": "TargetCapacity" },
"Type": "AWS::EC2::SpotFleet",
"Properties": {
"SpotFleetRequestConfigData": {
"IamFleetRole": {
"Ref": "myIamFleetRole"
},
Parameters
-
SpotFleetRequestConfigData
required - SpotFleetRequestConfigData
Explanation in CloudFormation Registry
Specifies a Spot Fleet request. A Spot Fleet request contains the configuration information to launch a fleet, or group, of instances.
The Spot Fleet request specifies the total target capacity and the On-Demand target capacity for the fleet. Amazon EC2 calculates the difference between the total capacity and On-Demand capacity, and launches the difference as Spot capacity.
The Spot Fleet request can include multiple launch specifications that vary by instance type, AMI, Availability Zone, or subnet.
By default, the Spot Fleet requests Spot Instances in the Spot pool where the price per unit is the lowest. Each launch specification can include its own instance weighting that reflects the value of the instance type to your application workload.
Alternatively, you can specify that the Spot Fleet distribute the target capacity across the Spot pools included in its launch specifications. By ensuring that the Spot Instances in your Spot Fleet are in different Spot pools, you can improve the availability of your fleet.
You can specify tags for the Spot Instances. You cannot tag other resource types in a Spot Fleet request because only the
instance
resource type is supported.For more information, see Spot Fleet Requests in the Amazon EC2 User Guide for Linux Instances.
Frequently asked questions
What is AWS Amazon EC2 Fleet Request?
AWS Amazon EC2 Fleet Request 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 Request?
For Terraform, the solemnwarning/build-server-deployment, fdns/terraform-k8s and pmkuny/aws-env source code examples are useful. See the Terraform Example section for further details.
For CloudFormation, the thebeebs/powershell, kohesive/kohesive-iac and GorillaStack/spot-fleet-request-example source code examples are useful. See the CloudFormation Example section for further details.