Skip to content

Commit

Permalink
feat(cloud9): support setting automaticStopTimeMinutes (#25593)
Browse files Browse the repository at this point in the history
Add parameter automaticStopTimeMinutes to specify the number of minutes until the running instance is shut down after the environment was last used.

Closes #25592.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
ba032759 authored Jun 5, 2023
1 parent 8b97bdf commit 437345e
Show file tree
Hide file tree
Showing 13 changed files with 1,585 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-cloud9-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,17 @@ new cloud9.Ec2Environment(this, 'C9Env', {
owner: cloud9.Owner.user(user)
})
```

## Auto-Hibernation

A Cloud9 environemnt can automatically start and stop the associated EC2 instance to reduce costs.

Use `automaticStop` to specify the number of minutes until the running instance is shut down after the environment was last used.

```ts
const defaultVpc = ec2.Vpc.fromLookup(this, 'DefaultVPC', { isDefault: true });
new cloud9.Ec2Environment(this, 'Cloud9Env2', {
vpc: defaultVpc,
imageId: cloud9.ImageId.AMAZON_LINUX_2,
automaticStop: Duration.minutes(30),
});
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-cloud9-alpha/lib/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ export interface Ec2EnvironmentProps {
*
*/
readonly imageId: ImageId

/**
* The number of minutes until the running instance is shut down after the
* environment was last used.
*
* Setting a value of 0 means the instance will never be automatically shut down."
*
* @default - The instance will not be shut down automatically.
*/
readonly automaticStop?: cdk.Duration
}

/**
Expand Down Expand Up @@ -200,6 +210,7 @@ export class Ec2Environment extends cdk.Resource implements IEc2Environment {
})) : undefined,
connectionType: props.connectionType ?? ConnectionType.CONNECT_SSH,
imageId: props.imageId,
automaticStopTimeMinutes: props.automaticStop?.toMinutes(),
});
this.environmentId = c9env.ref;
this.ec2EnvironmentArn = c9env.getAtt('Arn').toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Fixture with packages imported, but nothing else
import { CfnOutput, Stack } from 'aws-cdk-lib';
import { CfnOutput, Duration, Stack } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as cloud9 from '@aws-cdk/aws-cloud9-alpha';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
Expand Down
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-cloud9-alpha/test/cloud9.environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ test('environment owner can be account root', () => {
});
});

test('can set automaticStop', () => {
// WHEN
const automaticStop = cdk.Duration.minutes(30);
new cloud9.Ec2Environment(stack, 'C9Env', {
vpc,
imageId: cloud9.ImageId.AMAZON_LINUX_2,
automaticStop,
});
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Cloud9::EnvironmentEC2', {
AutomaticStopTimeMinutes: automaticStop.toMinutes(),
});
});

test.each([
[ConnectionType.CONNECT_SSH, 'CONNECT_SSH'],
[ConnectionType.CONNECT_SSM, 'CONNECT_SSM'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "32.0.0",
"files": {
"d302c7f90912c93bef6f8fda089adb33206a67ff3bdb6ce885b1f90a7fac7e25": {
"source": {
"path": "C9automaticStopStack.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "d302c7f90912c93bef6f8fda089adb33206a67ff3bdb6ce885b1f90a7fac7e25.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
}
},
"dockerImages": {}
}
Loading

0 comments on commit 437345e

Please sign in to comment.