By using AWS re:Post, you agree to the Terms of Use

How to attach an EBS volume to Cloud9 via CDK stack?

2

I am creating a cloud9 environment from CDK as below. I want to attach/adjust EBS volume size and attach a IAM Role to the EC2 of the cloud9 from the CDK stack. Can anyone help?

import { aws_cloud9, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';

export class Cloud9CdkStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    // cloud9 environment 
    const cloud9 = new aws_cloud9.CfnEnvironmentEC2(
      this,
      'CdkCloud9Example',
      {
        automaticStopTimeMinutes: 30,
        instanceType: 't2.large',
        name: 'CdkCloud9Example',
        connectionType: 'CONNECT_SSM',
        ownerArn: 'arn:aws:iam::IAM_USER_ID:user/IAM_USER_NAME'
      }
    )

    // access the ec2 and attach volume ??

  }
}

  • I also hope to have a tutorial about how to reduce the size of root EBS for windows instances also.

1 Answer
0
Accepted Answer

There is no CDK API to do this directly. However, you can use one of the following approaches to implement it by yourself.

A Lambda-backed custom CDK resource in your CDK stack

Implement a custom resource using this example as a reference. In the Lambda function, you can modify the underlying EC2 instance by using the AWS SDK. The EC2 instance running a Cloud9 environment has a tag aws:cloud9:environment with the value of the environment ID, so you can use this tag to obtain the EC2 instance ID. Using the Lambda function and the AWS SDK, you can modify the underlying EC2 instance in the way you want.

Systems Manager Document

You can leverage AWS Systems Manager and its Documents for setting up the instance. Take a look on this CDK construct on ConstructHub. This construct provides a method resizeEBSTo for configuring the instance's EBS volume. You can add another Document steps using the addDocumentSteps method for any other required changes.

profile picture
answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions