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

build-cluster-aws.tf#L109
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
main.tf#L1
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)
spot.tf#L3
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-"
aws_spot_fleet_request.tf#L1
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"
instance-fleet.tf#L7
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"

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

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.

risk-label

aws_default_vpc

Ensure to avoid using default VPC

It is better to define the own VPC and use it.

risk-label

aws_network_acl_rule

Ensure your network ACL rule blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.

risk-label

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).

risk-label

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.

risk-label

aws_security_group

Ensure your security group blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.

Review your AWS Amazon EC2 settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

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

ec2-spot-fleet.yml#L171
    Type: AWS::EC2::SpotFleet
    Properties:
      SpotFleetRequestConfigData:
        AllocationStrategy: diversified
        IamFleetRole: !GetAtt SpotFleetRole.Arn
        LaunchTemplateConfigs:
EC2.json#L786
  "resourceType" : "AWS::EC2::SpotFleet",
  "properties" : [ {
    "propertyName" : "SpotFleetRequestConfigData",
    "propertyType" : "AWS::EC2::SpotFleet::SpotFleetRequestConfigDatum",
    "required" : true,
    "propertyHref" : "aws-properties-ec2-spotfleet-spotfleetrequestconfigdata.html"
template.json#L6
      "Type" : "AWS::EC2::SpotFleet",
      "Properties" : {
        "SpotFleetRequestConfigData" : {
          "IamFleetRole" : "arn:aws:iam::<insert account id here>:role/aws-ec2-spot-fleet-role",
          "LaunchSpecifications" : [ {
              "ImageId" : "ami-f2210191",
SpotInstanceCFT.json#L6
      "Type" : "AWS::EC2::SpotFleet",
      "Properties" : {
        "SpotFleetRequestConfigData" : {
          "IamFleetRole" : "arn:aws:iam::082449889088:role/aws-ec2-spot-fleet-role",
          "LaunchSpecifications" : [ {
              "ImageId" : "ami-b70554c8",
integration_test.json#L45
      "Type": "AWS::EC2::SpotFleet",
      "Properties": {
        "SpotFleetRequestConfigData": {
          "IamFleetRole": { "Ref": "IAMFleetRole" },
          "SpotPrice": "1000",
          "TargetCapacity": { "Ref": "TargetCapacity" },
template.json#L23
      "Type": "AWS::EC2::SpotFleet",
      "Properties": {
        "SpotFleetRequestConfigData": {
          "IamFleetRole": {
            "Ref": "myIamFleetRole"
          },

Parameters

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.