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

ec2_fleet-main.tf#L1
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

ec2-fleet.tf#L1
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
ec2-fleet.tf#L1
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
main.tf#L7
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

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

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::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

aws_ec2_infra_template.yml#L77
    Type: AWS::EC2::EC2Fleet
    DependsOn: WebServerEC2LaunchTemplate
    Properties:
      LaunchTemplateConfigs:
        - LaunchTemplateSpecification:
            LaunchTemplateName: WebServerEC2LaunchTemplate
ec2-fleet.yml#L23
    Type: AWS::EC2::EC2Fleet
    Properties:
      ExcessCapacityTerminationPolicy: termination
      LaunchTemplateConfigs:
        - LaunchTemplateSpecification:
            LaunchTemplateId: !Ref EC2LaunchTemplate
LaunchFleet.yml#L18
    Type: AWS::EC2::EC2Fleet
    Properties:
      LaunchTemplateConfigs:
      - LaunchTemplateSpecification:
          LaunchTemplateId: !Ref WorkerNodeLaunchTemplate
          Version: 1
LaunchFleet.yml#L15
    Type: AWS::EC2::EC2Fleet
    Properties:
      LaunchTemplateConfigs:
      - LaunchTemplateSpecification:
          LaunchTemplateId: !Ref WorkerNodeLaunchTemplate
          Version: 1
Instance%20Fleet.yml#L1
Type: AWS::EC2::EC2Fleet
Properties:
  ExcessCapacityTerminationPolicy: String
  LaunchTemplateConfigs:
    - FleetLaunchTemplateConfigRequest
  OnDemandOptions:
EC2EC2FleetSpecification.json#L3
    "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",
EC2EC2FleetSpecification.json#L3
    "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",
EC2EC2FleetSpecification.json#L3
    "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",
EC2EC2FleetSpecification.json#L3
    "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",
EC2EC2FleetSpecification.json#L3
    "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

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.