Skip to main content

One spot instance with EIP and defined duration. No interruption.

Project description

awscdk-jsii-template NPM version PyPI version Release

cdk-spot-one

One spot instance with EIP and defined duration. No interruption.

Why

Sometimes we need an Amazon EC2 instance with static fixed IP for testing or development purpose for a duration of time(probably hours). We need to make sure during this time, no interruption will occur and we don't want to pay for on-demand rate. cdk-spot-one helps you reserve one single spot instance with pre-allocated or new Elastic IP addresses(EIP) with defined blockDuration, during which time the spot instance will be secured with no spot interruption.

Behind the scene, cdk-spot-one provisions a spot fleet with capacity of single instance for you and it associates the EIP with this instance. The spot fleet is reserved as spot block with blockDuration from one hour up to six hours to ensure the high availability for your spot instance.

Multiple spot instances are possible by simply specifying the targetCapacity construct property, but we only associate the EIP with the first spot instance at this moment.

Enjoy your highly durable one spot instance with AWS CDK!

Constructs

This library provides two major constructs:

SpotInstance

  • Create a spot instance without any fleet
  • Does NOT support Spot Block
  • Support stop or hibernate instance

Scenario: To leverage the stop or hibernate capabilities of the spot instance to persist the data in the ebs volume.

SpotFleet

  • Create a spot instance with a Spot Fleet
  • Support Spot Block
  • Does NOT support stop or hibernate instance

Scenario: To ensure the availability with no disruption with defined period up to 6 hours.

Sample

SpotInstance

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from cdk_spot_one import SpotInstance

# Default use Amazon Linux 2
SpotInstance(stack, "SpotInstance")

# Custom Id use Ubuntu 20.04 Arm64 Server.
SpotInstance(stack, "SpotInstanceUbuntu",
    vpc=vpc,
    custom_ami_id="ami-076d8ebdd0e1ec091", # ubuntu ami id.
    default_instance_type=InstanceType("t4g.medium"),
    key_name=key_name,
    block_device_mappings=[{"device_name": "/dev/sda1", "ebs": {"volume_size": 20}}],
    additional_user_data=["curl -fsSL https://get.docker.com -o get-docker.sh", "sudo sh get-docker.sh"]
)

SpotFleet

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from cdk_spot_one import SpotFleet

# create the first fleet for one hour and associate with our existing EIP
fleet = SpotFleet(stack, "SpotFleet")

# configure the expiration after 1 hour
fleet.expire_after(Duration.hours(1))

# create the 2nd fleet with single Gravition 2 instance for 6 hours and associate with new EIP
fleet2 = SpotFleet(stack, "SpotFleet2",
    block_duration=BlockDuration.SIX_HOURS,
    eip_allocation_id="eipalloc-0d1bc6d85895a5410",
    default_instance_type=InstanceType("c6g.large"),
    vpc=fleet.vpc
)
# configure the expiration after 6 hours
fleet2.expire_after(Duration.hours(6))

# print the instanceId from each spot fleet
CfnOutput(stack, "SpotFleetInstanceId", value=fleet.instance_id)
CfnOutput(stack, "SpotFleet2InstanceId", value=fleet2.instance_id)

Create spot instances without duration block

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
fleet = SpotFleet(stack, "SpotFleet",
    block_duration=BlockDuration.NONE
)

NOTE: This kind of spot instance will be interrupted by AWS. However the fleet is using type maintain, the fleet can be refulfilled.

ARM64 and Graviton 2 support

cdk-spot-one selects the latest Amazon Linux 2 AMI for your ARM64 instances. Simply select the instance types with the defaultInstanceType property and the SpotFleet will auto configure correct AMI for the instance.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
defaultInstanceType: new InstanceType('c6g.large')

ECS Cluster support

See https://github.com/pahud/cdk-spot-one/issues/270#issuecomment-877152685

SSH connect

By default the cdk-spot-one does not bind any SSH public key for you on the instance. You are encouraged to use ec2-instance-connect to send your public key from local followed by one-time SSH connect.

For example:

pubkey="$HOME/.ssh/aws_2020_id_rsa.pub"
echo "sending public key to ${instanceId}"
aws ec2-instance-connect send-ssh-public-key --instance-id ${instanceId} --instance-os-user ec2-user \
--ssh-public-key file://${pubkey} --availability-zone ${az}

npx ec2-connect INSTANCE_ID

To connect to the instance, run npx ec2-connect as below:

$ npx ec2-connect i-01f827ab9de7b93a9

or

$ npx ec2-connect i-01f827ab9de7b93a9 ~/.ssh/other_public_key_path

If you are using different SSH public key(default is ~/.ssh/id_rsa.pub)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdk-spot-one-0.6.293.tar.gz (63.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cdk_spot_one-0.6.293-py3-none-any.whl (61.6 kB view details)

Uploaded Python 3

File details

Details for the file cdk-spot-one-0.6.293.tar.gz.

File metadata

  • Download URL: cdk-spot-one-0.6.293.tar.gz
  • Upload date:
  • Size: 63.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.3

File hashes

Hashes for cdk-spot-one-0.6.293.tar.gz
Algorithm Hash digest
SHA256 34d99bb4d420f69186965b569878d9be91ec0dfc000566aef948adc438a1d883
MD5 9809d28893ffa585d26a16219a893c2b
BLAKE2b-256 ae6e1afd3a0d58859617702e1bc33f689d6cbb0674e9afab0d9313fba0c06ccc

See more details on using hashes here.

File details

Details for the file cdk_spot_one-0.6.293-py3-none-any.whl.

File metadata

  • Download URL: cdk_spot_one-0.6.293-py3-none-any.whl
  • Upload date:
  • Size: 61.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.3

File hashes

Hashes for cdk_spot_one-0.6.293-py3-none-any.whl
Algorithm Hash digest
SHA256 59a1eb34e2313f6210f82a104a1d5c4543f09fc2876e81538383792a92817c49
MD5 e790ec0245685ef5d72ccde3b91ba2e9
BLAKE2b-256 ca2000095d9a41915d13b5ef6515177310f8f6c8f9ed80c517f95ce53cd871d1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page