AWS Amazon EC2 Instance Request
This page shows how to write Terraform and CloudFormation for Amazon EC2 Instance Request and write them securely.
aws_spot_instance_request (Terraform)
The Instance Request in Amazon EC2 can be configured in Terraform with the resource name aws_spot_instance_request
. The following sections describe 3 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_spot_instance_request" "public" {
ami = "ami-0b3e57ee3b63dd76b" # amazon linux 2 ami
spot_price = "0.03"
instance_type = "t3a.nano"
subnet_id = module.vpc.public_subnets[0]
iam_instance_profile = aws_iam_instance_profile.main.id
resource "aws_spot_instance_request" "spot1" {
ami = "ami-02701bcdc5509e57b"
spot_price = "0.016"
instance_type = "t2.micro"
spot_type = "one-time"
wait_for_fulfillment = "true"
resource "aws_spot_instance_request" "webserver_public" {
ami = var.ami
instance_type = var.instance_type
spot_price = var.spot_price
subnet_id = var.subnet_public.id
Parameters
-
ami
required - string -
arn
optional computed - string -
associate_public_ip_address
optional computed - bool -
availability_zone
optional computed - string -
block_duration_minutes
optional - number -
cpu_core_count
optional computed - number -
cpu_threads_per_core
optional computed - number -
disable_api_termination
optional - bool -
ebs_optimized
optional - bool -
get_password_data
optional - bool -
hibernation
optional - bool -
host_id
optional computed - string -
iam_instance_profile
optional - string -
id
optional computed - string -
instance_initiated_shutdown_behavior
optional - string -
instance_interruption_behaviour
optional - string -
instance_state
optional computed - string -
instance_type
required - string -
ipv6_address_count
optional computed - number -
ipv6_addresses
optional computed - list of string -
key_name
optional computed - string -
launch_group
optional - string -
monitoring
optional - bool -
outpost_arn
optional computed - string -
password_data
optional computed - string -
placement_group
optional computed - string -
primary_network_interface_id
optional computed - string -
private_dns
optional computed - string -
private_ip
optional computed - string -
public_dns
optional computed - string -
public_ip
optional computed - string -
secondary_private_ips
optional computed - set of string -
security_groups
optional computed - set of string -
source_dest_check
optional - bool -
spot_bid_status
optional computed - string -
spot_instance_id
optional computed - string -
spot_price
optional computed - string -
spot_request_state
optional computed - string -
spot_type
optional - string -
subnet_id
optional computed - string -
tags
optional - map from string to string -
tenancy
optional computed - string -
user_data
optional - string -
user_data_base64
optional - string -
valid_from
optional computed - string -
valid_until
optional computed - string -
volume_tags
optional - map from string to string -
vpc_security_group_ids
optional computed - set of string -
wait_for_fulfillment
optional - bool -
credit_specification
list block-
cpu_credits
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 -
tags
optional - map from string to string -
throughput
optional computed - number -
volume_id
optional computed - string -
volume_size
optional computed - number -
volume_type
optional computed - string
-
-
enclave_options
list block-
enabled
optional computed - bool
-
-
ephemeral_block_device
set block-
device_name
required - string -
no_device
optional - bool -
virtual_name
optional - string
-
-
metadata_options
list block-
http_endpoint
optional computed - string -
http_put_response_hop_limit
optional computed - number -
http_tokens
optional computed - string
-
-
network_interface
set block-
delete_on_termination
optional - bool -
device_index
required - number -
network_interface_id
required - string
-
-
root_block_device
list block-
delete_on_termination
optional - bool -
device_name
optional computed - string -
encrypted
optional computed - bool -
iops
optional computed - number -
kms_key_id
optional computed - string -
tags
optional - map from string to string -
throughput
optional computed - number -
volume_id
optional computed - string -
volume_size
optional computed - number -
volume_type
optional computed - string
-
-
timeouts
single block
Explanation in Terraform Registry
Provides an EC2 Spot Instance Request resource. This allows instances to be requested on the spot market. By default Terraform creates Spot Instance Requests with a
persistent
type, which means that for the duration of their lifetime, AWS will launch an instance with the configured details if and when the spot market will accept the requested price. On destruction, Terraform will make an attempt to terminate the associated Spot Instance if there is one present. Spot Instances requests with aone-time
type will close the spot request when the instance is terminated either by the request being below the current spot price availability or by a user.NOTE: Because their behavior depends on the live status of the spot market, Spot Instance Requests have a unique lifecycle that makes them behave differently than other Terraform resources. Most importantly: there is no guarantee that a Spot Instance exists to fulfill the request at any given point in time. See the AWS Spot Instance documentation for more information.
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 10 examples of how to use the resource and its parameters.
Example Usage from GitHub
Type: "AWS::EC2::SpotFleet"
Properties:
SpotFleetRequestConfigData:
AllocationStrategy: lowestPrice
LaunchTemplateConfigs:
-
Type: AWS::EC2::SpotFleet
Properties:
SpotFleetRequestConfigData:
AllocationStrategy: diversified
IamFleetRole: !GetAtt SpotFleetRole.Arn
LaunchTemplateConfigs:
Type: AWS::EC2::SpotFleet
Properties:
SpotFleetRequestConfigData:
IamFleetRole: 'arn:aws:iam::123456789012:role/role-name-with-path'
SpotPrice: '1000'
TargetCapacity: 0
Type: AWS::EC2::SpotFleet
Properties:
SpotFleetRequestConfigData:
IamFleetRole: 'arn:aws:iam::123456789012:role/role-name-with-path'
SpotPrice: '1000'
TargetCapacity: 0
Type: AWS::EC2::SpotFleet
Properties:
SpotFleetRequestConfigData:
IamFleetRole: 'arn:aws:iam::123456789012:role/role-name-with-path'
SpotPrice: '1000'
TargetCapacity: 0
"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::082449889088:role/aws-ec2-spot-fleet-role",
"LaunchSpecifications" : [ {
"ImageId" : "ami-b70554c8",
"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": { "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 Instance Request?
AWS Amazon EC2 Instance 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 Instance Request?
For Terraform, the ggirault/poc-vpc-shared, Angelo512/Terabyte-CIT481-Senior-Project and cdeucher/terraform-aws-vpc source code examples are useful. See the Terraform Example section for further details.
For CloudFormation, the rafaelszt/SplunkSpot, thebeebs/powershell and almog27/iac-mega-repo source code examples are useful. See the CloudFormation Example section for further details.