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

main.tf#L7
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.tf#L35
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.tf#L35
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
}
variables.tf#L43
resource "aws_ec2_capacity_reservation" "named_test_resource" {
  instance_type     = "t2.micro"
  instance_platform = "Linux/UNIX"
  availability_zone = "us-east-1a"
  instance_count    = 1
}

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

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

product.template-us-east-1.yaml#L18
    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'
product.template-eu-west-2.yaml#L18
    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'
product.template-us-east-2.yaml#L18
    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'
product.template-us-west-1.yaml#L18
    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'
product.template-ap-northeast-2.yaml#L18
    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'
Cap_ReserveT2Nano.json#L5
            "Type": "AWS::EC2::CapacityReservation",
            "Properties": {
                "AvailabilityZone": "us-east-1b",
                "InstanceCount": 2,
                "InstanceType": "t2.nano",
                "InstancePlatform": "Linux/UNIX",
Cap_Reserve.json#L5
            "Type": "AWS::EC2::CapacityReservation",
            "Properties": {
                "AvailabilityZone": "us-east-1b",
                "EndDate": "2019-05-30",
                "EndDateType": "limited",
                "InstanceCount": 2,
EC2CapacityReservationSpecification.json#L3
    "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",
awsResouceIconMatches.json#L19
        "resourceType": "AWS::EC2::CapacityReservation",
        "filePath": null
      },
      {
        "resourceType": "AWS::EC2::VPCEndpointServicePermissions",
        "filePath": null
template.json#L127
    "AWS::EC2::CapacityReservation": {
      "Type": "AWS::EC2::CapacityReservation",
      "Properties": {}
    },
    "AWS::AppSync::Resolver": {
      "Type": "AWS::AppSync::Resolver",

Parameters

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.