Package: cdk-spot-one
cdk-spot-oneOne spot instance with EIP and defined duration. No interruption.InstallUse the npm dist tag to opt in CDKv1 or CDKv2://forCDKv2
npminstallcdk-spot-one
or
npminstallcdk-spot-one@latest

//forCDKv1
npminstallcdk-spot-one@cdkv1WhySometimes 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-onehelps you reserve one single spot instance with pre-allocated or new
Elastic IP addresses(EIP) with definedblockDuration, during which time the spot instance will be secured with no spot interruption.Behind the scene,cdk-spot-oneprovisions 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 withblockDurationfrom one hour up to six hours to ensure the high availability for your spot instance.Multiple spot instances are possible by simply specifying thetargetCapacityconstruct 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!ConstructsThis library provides two major constructs:SpotInstanceCreate a spot instancewithoutany fleetDoesNOTsupportSpot BlockSupportstoporhibernateinstanceScenario: To leverage thestoporhibernatecapabilities of the spot instance to persist the data in the ebs volume.SpotFleetCreate a spot instance with aSpot FleetSupportSpot BlockDoesNOTsupportstoporhibernateinstanceScenario: To ensure the availability with no disruption with defined period up to 6 hours.SampleSpotInstanceimport{SpotInstance,AmazonMachineImage}from'cdk-spot-one';//DefaultuseAmazonLinux2newSpotInstance(stack,'SpotInstance');//CustomIduseUbuntu20.04Arm64Server.newSpotInstance(stack,'SpotInstanceUbuntu',{vpc,customAmiId:AmazonMachineImage.UBUNTU_20_04_ARM64.getImage(stack).imageId,defaultInstanceType:newInstanceType('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'],});SpotFleetimport{SpotFleet}from'cdk-spot-one';//createthefirstfleetforonehourandassociatewithourexistingEIPconstfleet=newSpotFleet(stack,'SpotFleet')//configuretheexpirationafter1hourfleet.expireAfter(Duration.hours(1))//createthe2ndfleetwithsingleGravition2instancefor6hoursandassociatewithnewEIPconstfleet2=newSpotFleet(stack,'SpotFleet2',{blockDuration:BlockDuration.SIX_HOURS,eipAllocationId:'eipalloc-0d1bc6d85895a5410',defaultInstanceType:newInstanceType('c6g.large'),vpc:fleet.vpc,})//configuretheexpirationafter6hoursfleet2.expireAfter(Duration.hours(6))//printtheinstanceIdfromeachspotfleetnewCfnOutput(stack,'SpotFleetInstanceId',{value:fleet.instanceId})newCfnOutput(stack,'SpotFleet2InstanceId',{value:fleet2.instanceId})Create spot instances without duration blockconstfleet=newSpotFleet(stack,'SpotFleet',{blockDuration:BlockDuration.NONE,})NOTE: This kind of spot instance will be interrupted by AWS. However the fleet is using typemaintain, the fleet can be refulfilled.ARM64 and Graviton 2 supportcdk-spot-oneselects the latest Amazon Linux 2 AMI for yourARM64instances. Simply select the instance types with thedefaultInstanceTypeproperty and theSpotFleetwill auto configure correct AMI for the instance.defaultInstanceType:newInstanceType('c6g.large')ECS Cluster supportSeehttps://github.com/pahud/cdk-spot-one/issues/270#issuecomment-877152685Connect with Session Manager(recommended)You may connect to the spot instance withSession Manager.# make sure you have installed session-manager-plugin$session-manager-plugin# start session$awsssmstart-session--targetINSTANCE_IDConnect with EC2 SSH ConnectBy default thecdk-spot-onedoes not bind any SSH public key for you on the instance. You are encouraged to useec2-instance-connectto 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}"awsec2-instance-connectsend-ssh-public-key--instance-id${instanceId}--instance-os-userec2-user\--ssh-public-keyfile://${pubkey}--availability-zone${az}npx ec2-connect INSTANCE_IDTo connect to the instance, runnpx ec2-connectas below:$npxec2-connecti-01f827ab9de7b93a9or$npxec2-connecti-01f827ab9de7b93a9~/.ssh/other_public_key_pathIf you are using different SSH public key(default is ~/.ssh/id_rsa.pub)