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
-
amirequired - string -
arnoptional computed - string -
associate_public_ip_addressoptional computed - bool -
availability_zoneoptional computed - string -
block_duration_minutesoptional - number -
cpu_core_countoptional computed - number -
cpu_threads_per_coreoptional computed - number -
disable_api_terminationoptional - bool -
ebs_optimizedoptional - bool -
get_password_dataoptional - bool -
hibernationoptional - bool -
host_idoptional computed - string -
iam_instance_profileoptional - string -
idoptional computed - string -
instance_initiated_shutdown_behavioroptional - string -
instance_interruption_behaviouroptional - string -
instance_stateoptional computed - string -
instance_typerequired - string -
ipv6_address_countoptional computed - number -
ipv6_addressesoptional computed - list of string -
key_nameoptional computed - string -
launch_groupoptional - string -
monitoringoptional - bool -
outpost_arnoptional computed - string -
password_dataoptional computed - string -
placement_groupoptional computed - string -
primary_network_interface_idoptional computed - string -
private_dnsoptional computed - string -
private_ipoptional computed - string -
public_dnsoptional computed - string -
public_ipoptional computed - string -
secondary_private_ipsoptional computed - set of string -
security_groupsoptional computed - set of string -
source_dest_checkoptional - bool -
spot_bid_statusoptional computed - string -
spot_instance_idoptional computed - string -
spot_priceoptional computed - string -
spot_request_stateoptional computed - string -
spot_typeoptional - string -
subnet_idoptional computed - string -
tagsoptional - map from string to string -
tenancyoptional computed - string -
user_dataoptional - string -
user_data_base64optional - string -
valid_fromoptional computed - string -
valid_untiloptional computed - string -
volume_tagsoptional - map from string to string -
vpc_security_group_idsoptional computed - set of string -
wait_for_fulfillmentoptional - bool -
credit_specificationlist block-
cpu_creditsoptional - 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 -
tagsoptional - map from string to string -
throughputoptional computed - number -
volume_idoptional computed - string -
volume_sizeoptional computed - number -
volume_typeoptional computed - string
-
-
enclave_optionslist block-
enabledoptional computed - bool
-
-
ephemeral_block_deviceset block-
device_namerequired - string -
no_deviceoptional - bool -
virtual_nameoptional - string
-
-
metadata_optionslist block-
http_endpointoptional computed - string -
http_put_response_hop_limitoptional computed - number -
http_tokensoptional computed - string
-
-
network_interfaceset block-
delete_on_terminationoptional - bool -
device_indexrequired - number -
network_interface_idrequired - string
-
-
root_block_devicelist block-
delete_on_terminationoptional - bool -
device_nameoptional computed - string -
encryptedoptional computed - bool -
iopsoptional computed - number -
kms_key_idoptional computed - string -
tagsoptional - map from string to string -
throughputoptional computed - number -
volume_idoptional computed - string -
volume_sizeoptional computed - number -
volume_typeoptional computed - string
-
-
timeoutssingle 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
persistenttype, 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-timetype 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
-
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 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.