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!

Sample

# 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')

SSH connect

By default the cdk-spot-one does not assign 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}

You may also create a simple ec2-connect.sh script like this and save in your $PATH:

#!/bin/bash

instanceId=$1
pubkey="$HOME/.ssh/aws_2020_id_rsa.pub"
sshUser='ec2-user'

az=$(aws ec2 describe-instances --instance-id ${instanceId} --query 'Reservations[0].Instances[0].Placement.AvailabilityZone' --output text)
publicIp=$(aws ec2 describe-instances --instance-id ${instanceId} --query 'Reservations[0].Instances[0].PublicIpAddress' --output text)


echo "sending public key to ${instanceId}"
aws ec2-instance-connect send-ssh-public-key --instance-id ${instanceId} --instance-os-user ${sshUser} \
--ssh-public-key file://${pubkey} --availability-zone ${az} > /dev/null

if [[ $2 != '--send-key-only' ]]; then
  echo "connecting to ${publicIp} at ${az}"
	ssh ${sshUser}@${publicIp}
fi

And simply run this to connect to the EC2 instance.

$ ec2-connect.sh i-01f827ab9de7b93a9

It's also possible to explicitly specify your existing SSH key with the keyName construct property if you like.

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.82.tar.gz (47.4 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.82-py3-none-any.whl (45.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cdk-spot-one-0.6.82.tar.gz
  • Upload date:
  • Size: 47.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for cdk-spot-one-0.6.82.tar.gz
Algorithm Hash digest
SHA256 ef461e7f1de8e9d1a3a02ae1e72932e8cc77ca79899b1788cc6a96f1d701e41d
MD5 9532db3060ddbb1a11fd8548415d8848
BLAKE2b-256 304e17a88d32a9c9505e68c6e5b2fdc286e8f4d0adfc9a2033445046bce59de9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cdk_spot_one-0.6.82-py3-none-any.whl
  • Upload date:
  • Size: 45.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for cdk_spot_one-0.6.82-py3-none-any.whl
Algorithm Hash digest
SHA256 cab6247e7157a761bfa74f19f726b396e80e6eb7cad6f90fa4c6c0247f3265f7
MD5 8209a2a1a0a76f69a9b51bd5ffb0f18c
BLAKE2b-256 10213b6becc857033eab44e870fb6c116508ff2d1d1c6a1c4d43f45215f41f74

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