cdk-spot-one


Namecdk-spot-one JSON
Version 2.0.328 PyPI version JSON
download
home_pagehttps://github.com/pahud/cdk-spot-one.git
SummaryOne spot instance with EIP and defined duration. No interruption.
upload_time2023-03-21 00:29:10
maintainer
docs_urlNone
authorPahud Hsieh<pahudnet@gmail.com>
requires_python~=3.7
licenseApache-2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![NPM version](https://badge.fury.io/js/cdk-spot-one.svg)](https://badge.fury.io/js/cdk-spot-one)
[![PyPI version](https://badge.fury.io/py/cdk-spot-one.svg)](https://badge.fury.io/py/cdk-spot-one)
![Release](https://github.com/pahud/cdk-spot-one/workflows/Release/badge.svg)

# cdk-spot-one

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

# Install

Use the npm dist tag to opt in CDKv1 or CDKv2:

```sh
// for CDKv2
npm install cdk-spot-one
or
npm install cdk-spot-one@latest

// for CDKv1
npm install cdk-spot-one@cdkv1
```

# 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](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-requests.html#fixed-duration-spot-instances)
* 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](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-fleet.html)
* Support [Spot Block](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-requests.html#fixed-duration-spot-instances)
* Does **NOT** support `stop` or `hibernate` instance

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

# Sample

## SpotInstance

```python
import { SpotInstance, AmazonMachineImage } from 'cdk-spot-one';

// Default use Amazon Linux 2
new SpotInstance(stack, 'SpotInstance');


// Custom Id use Ubuntu 20.04 Arm64 Server.
new SpotInstance(stack, 'SpotInstanceUbuntu', {
      vpc,
      customAmiId: AmazonMachineImage.UBUNTU_20_04_ARM64.getImage(stack).imageId,
      defaultInstanceType: new InstanceType('t4g.medium'),
      keyName,
      blockDeviceMappings: [{ deviceName: '/dev/sda1', ebs: { volumeSize: 20 } }],
      additionalUserData: ['curl -fsSL https://get.docker.com -o get-docker.sh', 'sudo sh get-docker.sh'],
    });
```

## SpotFleet

```python
import { SpotFleet } from 'cdk-spot-one';

// create the first fleet for one hour and associate with our existing EIP
const fleet = new SpotFleet(stack, 'SpotFleet')

// configure the expiration after 1 hour
fleet.expireAfter(Duration.hours(1))


// create the 2nd fleet with single Gravition 2 instance for 6 hours and associate with new EIP
const fleet2 = new SpotFleet(stack, 'SpotFleet2', {
  blockDuration: BlockDuration.SIX_HOURS,
  eipAllocationId: 'eipalloc-0d1bc6d85895a5410',
  defaultInstanceType: new InstanceType('c6g.large'),
  vpc: fleet.vpc,
})
// configure the expiration after 6 hours
fleet2.expireAfter(Duration.hours(6))

// print the instanceId from each spot fleet
new CfnOutput(stack, 'SpotFleetInstanceId', { value: fleet.instanceId })
new CfnOutput(stack, 'SpotFleet2InstanceId', { value: fleet2.instanceId })
```

# Create spot instances without duration block

```python
const fleet = new SpotFleet(stack, 'SpotFleet', {
  blockDuration: BlockDuration.NONE,
})
```

NOTE: This kind of spot instance will be interrupted by AWS. However the fleet is using type [maintain](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-fleet.html#spot-fleet-allocation-strategy), 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.

```python
defaultInstanceType: new InstanceType('c6g.large')
```

# ECS Cluster support

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

# Connect with Session Manager(recommended)

You may connect to the spot instance with [Session Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-sessions-start.html).

```sh
# make sure you have installed session-manager-plugin
$ session-manager-plugin
# start session
$ aws ssm start-session --target INSTANCE_ID
```

# Connect with EC2 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:

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

```sh
$ npx ec2-connect i-01f827ab9de7b93a9
```

or

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

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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/pahud/cdk-spot-one.git",
    "name": "cdk-spot-one",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "~=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "Pahud Hsieh<pahudnet@gmail.com>",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/3a/f0/bcd0f1f43e4a29ad96a271b3beefb8ba64e0cbb1f19ac9397009553ce687/cdk-spot-one-2.0.328.tar.gz",
    "platform": null,
    "description": "[![NPM version](https://badge.fury.io/js/cdk-spot-one.svg)](https://badge.fury.io/js/cdk-spot-one)\n[![PyPI version](https://badge.fury.io/py/cdk-spot-one.svg)](https://badge.fury.io/py/cdk-spot-one)\n![Release](https://github.com/pahud/cdk-spot-one/workflows/Release/badge.svg)\n\n# cdk-spot-one\n\nOne spot instance with EIP and defined duration. No interruption.\n\n# Install\n\nUse the npm dist tag to opt in CDKv1 or CDKv2:\n\n```sh\n// for CDKv2\nnpm install cdk-spot-one\nor\nnpm install cdk-spot-one@latest\n\n// for CDKv1\nnpm install cdk-spot-one@cdkv1\n```\n\n# Why\n\nSometimes we need an Amazon EC2 instance with static fixed IP for testing or development purpose for a duration of\ntime(probably hours). We need to make sure during this time, no interruption will occur and we don't want to pay\nfor on-demand rate. `cdk-spot-one` helps you reserve one single spot instance with pre-allocated or new\nElastic IP addresses(EIP) with defined `blockDuration`, during which time the spot instance will be secured with no spot interruption.\n\nBehind 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.\n\nMultiple 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.\n\nEnjoy your highly durable one spot instance with AWS CDK!\n\n# Constructs\n\nThis library provides two major constructs:\n\n## SpotInstance\n\n* Create a spot instance **without** any fleet\n* Does **NOT** support [Spot Block](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-requests.html#fixed-duration-spot-instances)\n* Support `stop` or `hibernate` instance\n\nScenario: To leverage the `stop` or `hibernate` capabilities of the spot instance to persist the data in the ebs volume.\n\n## SpotFleet\n\n* Create a spot instance with a [Spot Fleet](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-fleet.html)\n* Support [Spot Block](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-requests.html#fixed-duration-spot-instances)\n* Does **NOT** support `stop` or `hibernate` instance\n\nScenario: To ensure the availability with no disruption with defined period up to 6 hours.\n\n# Sample\n\n## SpotInstance\n\n```python\nimport { SpotInstance, AmazonMachineImage } from 'cdk-spot-one';\n\n// Default use Amazon Linux 2\nnew SpotInstance(stack, 'SpotInstance');\n\n\n// Custom Id use Ubuntu 20.04 Arm64 Server.\nnew SpotInstance(stack, 'SpotInstanceUbuntu', {\n      vpc,\n      customAmiId: AmazonMachineImage.UBUNTU_20_04_ARM64.getImage(stack).imageId,\n      defaultInstanceType: new InstanceType('t4g.medium'),\n      keyName,\n      blockDeviceMappings: [{ deviceName: '/dev/sda1', ebs: { volumeSize: 20 } }],\n      additionalUserData: ['curl -fsSL https://get.docker.com -o get-docker.sh', 'sudo sh get-docker.sh'],\n    });\n```\n\n## SpotFleet\n\n```python\nimport { SpotFleet } from 'cdk-spot-one';\n\n// create the first fleet for one hour and associate with our existing EIP\nconst fleet = new SpotFleet(stack, 'SpotFleet')\n\n// configure the expiration after 1 hour\nfleet.expireAfter(Duration.hours(1))\n\n\n// create the 2nd fleet with single Gravition 2 instance for 6 hours and associate with new EIP\nconst fleet2 = new SpotFleet(stack, 'SpotFleet2', {\n  blockDuration: BlockDuration.SIX_HOURS,\n  eipAllocationId: 'eipalloc-0d1bc6d85895a5410',\n  defaultInstanceType: new InstanceType('c6g.large'),\n  vpc: fleet.vpc,\n})\n// configure the expiration after 6 hours\nfleet2.expireAfter(Duration.hours(6))\n\n// print the instanceId from each spot fleet\nnew CfnOutput(stack, 'SpotFleetInstanceId', { value: fleet.instanceId })\nnew CfnOutput(stack, 'SpotFleet2InstanceId', { value: fleet2.instanceId })\n```\n\n# Create spot instances without duration block\n\n```python\nconst fleet = new SpotFleet(stack, 'SpotFleet', {\n  blockDuration: BlockDuration.NONE,\n})\n```\n\nNOTE: This kind of spot instance will be interrupted by AWS. However the fleet is using type [maintain](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-fleet.html#spot-fleet-allocation-strategy), the fleet can be refulfilled.\n\n# ARM64 and Graviton 2 support\n\n`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.\n\n```python\ndefaultInstanceType: new InstanceType('c6g.large')\n```\n\n# ECS Cluster support\n\nSee https://github.com/pahud/cdk-spot-one/issues/270#issuecomment-877152685\n\n# Connect with Session Manager(recommended)\n\nYou may connect to the spot instance with [Session Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-sessions-start.html).\n\n```sh\n# make sure you have installed session-manager-plugin\n$ session-manager-plugin\n# start session\n$ aws ssm start-session --target INSTANCE_ID\n```\n\n# Connect with EC2 SSH Connect\n\nBy 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.\n\nFor example:\n\n```sh\npubkey=\"$HOME/.ssh/aws_2020_id_rsa.pub\"\necho \"sending public key to ${instanceId}\"\naws ec2-instance-connect send-ssh-public-key --instance-id ${instanceId} --instance-os-user ec2-user \\\n--ssh-public-key file://${pubkey} --availability-zone ${az}\n```\n\n## npx ec2-connect INSTANCE_ID\n\nTo connect to the instance, run `npx ec2-connect` as below:\n\n```sh\n$ npx ec2-connect i-01f827ab9de7b93a9\n```\n\nor\n\n```sh\n$ npx ec2-connect i-01f827ab9de7b93a9 ~/.ssh/other_public_key_path\n```\n\nIf you are using different SSH public key(default is ~/.ssh/id_rsa.pub)\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "One spot instance with EIP and defined duration. No interruption.",
    "version": "2.0.328",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4d3bd0a4d7090065233ff8d427aaf2088144becb41149e2775f27a463cce853c",
                "md5": "1286634390dc9522a46b87c1d8e65c49",
                "sha256": "523bdeb1a765f767bee04dec1f543debde4674983d0def6e22c86eb2378ab831"
            },
            "downloads": -1,
            "filename": "cdk_spot_one-2.0.328-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1286634390dc9522a46b87c1d8e65c49",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "~=3.7",
            "size": 74557,
            "upload_time": "2023-03-21T00:29:07",
            "upload_time_iso_8601": "2023-03-21T00:29:07.675566Z",
            "url": "https://files.pythonhosted.org/packages/4d/3b/d0a4d7090065233ff8d427aaf2088144becb41149e2775f27a463cce853c/cdk_spot_one-2.0.328-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3af0bcd0f1f43e4a29ad96a271b3beefb8ba64e0cbb1f19ac9397009553ce687",
                "md5": "c6fa64fd39730dd0de141373ce6ace4e",
                "sha256": "f6e275d214f5202f8f01cc72b536e27dbadb6e6822401063c00e4eddd1490183"
            },
            "downloads": -1,
            "filename": "cdk-spot-one-2.0.328.tar.gz",
            "has_sig": false,
            "md5_digest": "c6fa64fd39730dd0de141373ce6ace4e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "~=3.7",
            "size": 76086,
            "upload_time": "2023-03-21T00:29:10",
            "upload_time_iso_8601": "2023-03-21T00:29:10.229667Z",
            "url": "https://files.pythonhosted.org/packages/3a/f0/bcd0f1f43e4a29ad96a271b3beefb8ba64e0cbb1f19ac9397009553ce687/cdk-spot-one-2.0.328.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-21 00:29:10",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "pahud",
    "github_project": "cdk-spot-one.git",
    "lcname": "cdk-spot-one"
}
        
Elapsed time: 0.16230s