https://github.com/tksst/cdk-ec2-spot-simple
AWS CDK construct library to create EC2 Spot Instances simply.
https://github.com/tksst/cdk-ec2-spot-simple
aws cdk
Last synced: 4 months ago
JSON representation
AWS CDK construct library to create EC2 Spot Instances simply.
- Host: GitHub
- URL: https://github.com/tksst/cdk-ec2-spot-simple
- Owner: tksst
- License: apache-2.0
- Created: 2022-12-04T01:05:23.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2026-02-21T21:14:17.000Z (4 months ago)
- Last Synced: 2026-02-22T02:24:15.062Z (4 months ago)
- Topics: aws, cdk
- Language: TypeScript
- Homepage: https://constructs.dev/packages/cdk-ec2-spot-simple
- Size: 703 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# cdk-ec2-spot-simple
[](https://www.npmjs.com/package/cdk-ec2-spot-simple)
[](https://pypi.org/project/cdk-ec2-spot-simple)
[](https://www.nuget.org/packages/TksSt.Cdk.Ec2SpotSimple)
[](https://search.maven.org/artifact/st.tks.cdk/ec2-spot-simple)
[](https://constructs.dev/packages/cdk-ec2-spot-simple)
CDK construct library to create EC2 Spot Instances simply.
## Install
### TypeScript/JavaScript
```shell
npm install cdk-ec2-spot-simple
```
```shell
pnpm add cdk-ec2-spot-simple
```
```shell
yarn add cdk-ec2-spot-simple
```
### Python
```shell
pip install cdk-ec2-spot-simple
```
### .NET
```shell
dotnet add package TksSt.Cdk.Ec2SpotSimple
```
### Java
```xml
st.tks.cdk
ec2-spot-simple
```
### Go
```shell
go get github.com/tksst/cdk-ec2-spot-simple-go/cdkec2spotsimple/v2
```
## Usage
To set up a spot instance with default parameters, simply use "SpotInstance" instead of "ec2.Instance".
```typescript
import { SpotInstance } from "cdk-ec2-spot-simple";
import * as ec2 from "aws-cdk-lib/aws-ec2";
// Simple usage
new SpotInstance(this, "DefaultConfigSpotInstance", {
// Required properties of "ec2.Instance"
vpc: ec2.Vpc.fromLookup(this, "defaultVPC", { isDefault: true }),
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3A, ec2.InstanceSize.NANO),
machineImage: new ec2.AmazonLinuxImage()
});
// Advanced usage
new SpotInstance(this, "StoppableSpotInstance", {
// Required properties of "ec2.Instance"
vpc: ec2.Vpc.fromLookup(this, "defaultVPC", { isDefault: true }),
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3A, ec2.InstanceSize.NANO),
machineImage: new ec2.AmazonLinuxImage(),
// SpotInstance specific property
spotOptions: {
interruptionBehavior: ec2.SpotInstanceInterruption.STOP,
requestType: ec2.SpotRequestType.PERSISTENT,
maxPrice: 0.007
}
});
```
## API document
[See Construct Hub](https://constructs.dev/packages/cdk-ec2-spot-simple)
## Background
The `Instance` construct in `aws-cdk-lib/aws-ec2` does not have any spot instance functionality.
This `SpotInstance` construct creates `LaunchTemplate` that is enabled spot request internally and associate with `Instance`.
Also, `SpotInstance` creates a Lambda-backed custom resource if the spot requiest type is PERSISTENT. That resource deletes the spot request when the stack is destroyed.