UserData

class aws_cdk.aws_ec2.UserData

Bases: object

Instance User Data.

ExampleMetadata:

infused

Example:

# cluster: eks.Cluster

user_data = ec2.UserData.for_linux()
user_data.add_commands("set -o xtrace", f"/etc/eks/bootstrap.sh {cluster.clusterName}")
lt = ec2.CfnLaunchTemplate(self, "LaunchTemplate",
    launch_template_data=ec2.CfnLaunchTemplate.LaunchTemplateDataProperty(
        image_id="some-ami-id",  # custom AMI
        instance_type="t3.small",
        user_data=Fn.base64(user_data.render())
    )
)
cluster.add_nodegroup_capacity("extra-ng",
    launch_template_spec=eks.LaunchTemplateSpec(
        id=lt.ref,
        version=lt.attr_latest_version_number
    )
)

Methods

abstract add_commands(*commands)

Add one or more commands to the user data.

Parameters:

commands (str) –

Return type:

None

abstract add_execute_file_command(*, file_path, arguments=None)

Adds commands to execute a file.

Parameters:
  • file_path (str) – The path to the file.

  • arguments (Optional[str]) – The arguments to be passed to the file. Default: No arguments are passed to the file.

Return type:

None

abstract add_on_exit_commands(*commands)

Add one or more commands to the user data that will run when the script exits.

Parameters:

commands (str) –

Return type:

None

abstract add_s3_download_command(*, bucket, bucket_key, local_file=None, region=None)

Adds commands to download a file from S3.

Parameters:
  • bucket (IBucket) – Name of the S3 bucket to download from.

  • bucket_key (str) – The key of the file to download.

  • local_file (Optional[str]) – The name of the local file. Default: Linux - /tmp/bucketKey Windows - %TEMP%/bucketKey

  • region (Optional[str]) – The region of the S3 Bucket (needed for access via VPC Gateway). Default: none

Return type:

str

Returns:

: The local path that the file will be downloaded to

abstract add_signal_on_exit_command(resource)

Adds a command which will send a cfn-signal when the user data script ends.

Parameters:

resource (Resource) –

Return type:

None

abstract render()

Render the UserData for use in a construct.

Return type:

str

Static Methods

classmethod custom(content)

Create a userdata object with custom content.

Parameters:

content (str) –

Return type:

UserData

classmethod for_linux(*, shebang=None)

Create a userdata object for Linux hosts.

Parameters:

shebang (Optional[str]) – Shebang for the UserData script. Default: “#!/bin/bash”

Return type:

UserData

classmethod for_operating_system(os)
Parameters:

os (OperatingSystemType) –

Return type:

UserData

classmethod for_windows(*, persist=None)

Create a userdata object for Windows hosts.

Parameters:

persist (Optional[bool]) – Set to true to set this userdata to persist through an instance reboot; allowing it to run on every instance start. By default, UserData is run only once during the first instance launch. For more information, see: https://aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/ https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html#user-data-scripts Default: false

Return type:

UserData