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_strategyoptional - string -
client_tokenoptional computed - string -
excess_capacity_termination_policyoptional - string -
fleet_typeoptional - string -
iam_fleet_rolerequired - string -
idoptional computed - string -
instance_interruption_behaviouroptional - string -
instance_pools_to_use_countoptional - number -
load_balancersoptional computed - set of string -
replace_unhealthy_instancesoptional - bool -
spot_priceoptional - string -
spot_request_stateoptional computed - string -
tagsoptional - map from string to string -
target_capacityrequired - number -
target_group_arnsoptional computed - set of string -
terminate_instances_with_expirationoptional - bool -
valid_fromoptional - string -
valid_untiloptional - string -
wait_for_fulfillmentoptional - bool -
launch_specificationset block-
amirequired - string -
associate_public_ip_addressoptional - bool -
availability_zoneoptional computed - string -
ebs_optimizedoptional - bool -
iam_instance_profileoptional - string -
iam_instance_profile_arnoptional - string -
instance_typerequired - string -
key_nameoptional computed - string -
monitoringoptional - bool -
placement_groupoptional computed - string -
placement_tenancyoptional - string -
spot_priceoptional - string -
subnet_idoptional computed - string -
tagsoptional - map from string to string -
user_dataoptional - string -
vpc_security_group_idsoptional computed - set of string -
weighted_capacityoptional - string -
ebs_block_deviceset block-
delete_on_terminationoptional - bool -
device_namerequired - string -
encryptedoptional computed - bool -
iopsoptional computed - number -
kms_key_idoptional computed - string -
snapshot_idoptional computed - string -
throughputoptional computed - number -
volume_sizeoptional computed - number -
volume_typeoptional computed - string
-
-
ephemeral_block_deviceset block-
device_namerequired - string -
virtual_namerequired - string
-
-
root_block_deviceset block-
delete_on_terminationoptional - bool -
encryptedoptional computed - bool -
iopsoptional computed - number -
kms_key_idoptional computed - string -
throughputoptional computed - number -
volume_sizeoptional computed - number -
volume_typeoptional computed - string
-
-
-
launch_template_configset block-
launch_template_specificationlist block -
overridesset block-
availability_zoneoptional - string -
instance_typeoptional - string -
priorityoptional computed - number -
spot_priceoptional computed - string -
subnet_idoptional computed - string -
weighted_capacityoptional computed - number
-
-
-
spot_maintenance_strategieslist block-
capacity_rebalancelist block-
replacement_strategyoptional - string
-
-
-
timeoutssingle 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
-
SpotFleetRequestConfigDatarequired - 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
instanceresource 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.