AWS Amazon EC2 Capacity Reservation
This page shows how to write Terraform and CloudFormation for Amazon EC2 Capacity Reservation and write them securely.
aws_ec2_capacity_reservation (Terraform)
The Capacity Reservation in Amazon EC2 can be configured in Terraform with the resource name aws_ec2_capacity_reservation
. The following sections describe 4 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_ec2_capacity_reservation" "this" {
availability_zone = var.availability_zone
ebs_optimized = var.ebs_optimized
end_date = var.end_date
end_date_type = var.end_date_type
ephemeral_storage = var.ephemeral_storage
resource "aws_ec2_capacity_reservation" "nile-capacity-reservation" {
instance_type = var.instance_type
instance_platform = var.instance_platform
availability_zone = var.placement_availability_zone
instance_count = var.instance_count
}
resource "aws_ec2_capacity_reservation" "nile-capacity-reservation" {
instance_type = var.instance_type
instance_platform = var.instance_platform
availability_zone = var.placement_availability_zone
instance_count = var.instance_count
}
resource "aws_ec2_capacity_reservation" "named_test_resource" {
instance_type = "t2.micro"
instance_platform = "Linux/UNIX"
availability_zone = "us-east-1a"
instance_count = 1
}
Parameters
-
arn
optional computed - string -
availability_zone
required - string -
ebs_optimized
optional - bool -
end_date
optional - string -
end_date_type
optional - string -
ephemeral_storage
optional - bool -
id
optional computed - string -
instance_count
required - number -
instance_match_criteria
optional - string -
instance_platform
required - string -
instance_type
required - string -
owner_id
optional computed - string -
tags
optional - map from string to string -
tenancy
optional - string
Explanation in Terraform Registry
Provides an EC2 Capacity Reservation. This allows you to reserve capacity for your Amazon EC2 instances in a specific Availability Zone for any duration.
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::CapacityReservation (CloudFormation)
The CapacityReservation in EC2 can be configured in CloudFormation with the resource name AWS::EC2::CapacityReservation
. The following sections describe 10 examples of how to use the resource and its parameters.
Example Usage from GitHub
Type: AWS::EC2::CapacityReservation
Description: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-capacityreservation.html
Properties:
InstanceCount: !Ref 'InstanceCount'
AvailabilityZone: !Ref 'AvailabilityZone'
InstancePlatform: !Ref 'InstancePlatform'
Type: AWS::EC2::CapacityReservation
Description: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-capacityreservation.html
Properties:
InstanceCount: !Ref 'InstanceCount'
AvailabilityZone: !Ref 'AvailabilityZone'
InstancePlatform: !Ref 'InstancePlatform'
Type: AWS::EC2::CapacityReservation
Description: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-capacityreservation.html
Properties:
InstanceCount: !Ref 'InstanceCount'
AvailabilityZone: !Ref 'AvailabilityZone'
InstancePlatform: !Ref 'InstancePlatform'
Type: AWS::EC2::CapacityReservation
Description: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-capacityreservation.html
Properties:
InstanceCount: !Ref 'InstanceCount'
AvailabilityZone: !Ref 'AvailabilityZone'
InstancePlatform: !Ref 'InstancePlatform'
Type: AWS::EC2::CapacityReservation
Description: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-capacityreservation.html
Properties:
InstanceCount: !Ref 'InstanceCount'
AvailabilityZone: !Ref 'AvailabilityZone'
InstancePlatform: !Ref 'InstancePlatform'
"Type": "AWS::EC2::CapacityReservation",
"Properties": {
"AvailabilityZone": "us-east-1b",
"EndDate": "2019-05-30",
"EndDateType": "limited",
"InstanceCount": 2,
"Type": "AWS::EC2::CapacityReservation",
"Properties": {
"AvailabilityZone": "us-east-1b",
"InstanceCount": 2,
"InstanceType": "t2.nano",
"InstancePlatform": "Linux/UNIX",
"AWS::EC2::CapacityReservation.TagSpecification": {
"Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-capacityreservation-tagspecification.html",
"Properties": {
"ResourceType": {
"Required": false,
"Documentation": "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-capacityreservation-tagspecification.html#cfn-ec2-capacityreservation-tagspecification-resourcetype",
"resourceType": "AWS::EC2::CapacityReservation",
"filePath": null
},
{
"resourceType": "AWS::EC2::VPCEndpointServicePermissions",
"filePath": null
"AWS::EC2::CapacityReservation": {
"Type": "AWS::EC2::CapacityReservation",
"Properties": {}
},
"AWS::AppSync::Resolver": {
"Type": "AWS::AppSync::Resolver",
Parameters
-
Tenancy
optional - String -
EndDateType
optional - String -
InstanceCount
required - Integer -
TagSpecifications
optional - List of TagSpecification -
AvailabilityZone
required - String -
InstancePlatform
required - String -
InstanceType
required - String -
EphemeralStorage
optional - Boolean -
InstanceMatchCriteria
optional - String -
EndDate
optional - String -
EbsOptimized
optional - Boolean
Explanation in CloudFormation Registry
Creates a new Capacity Reservation with the specified attributes. For more information, see Capacity Reservations in the Amazon EC2 User Guide.
Frequently asked questions
What is AWS Amazon EC2 Capacity Reservation?
AWS Amazon EC2 Capacity Reservation 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 Capacity Reservation?
For Terraform, the niveklabs/aws, mike23101994/terraform-scripts and mike23101994/terraform-scripts source code examples are useful. See the Terraform Example section for further details.
For CloudFormation, the awslabs/aws-service-catalog-products, awslabs/aws-service-catalog-products and awslabs/aws-service-catalog-products source code examples are useful. See the CloudFormation Example section for further details.