MultipartUserData

class aws_cdk.aws_ec2.MultipartUserData(*, parts_separator=None)

Bases: UserData

Mime multipart user data.

This class represents MIME multipart user data, as described in. Specifying Multiple User Data Blocks Using a MIME Multi Part Archive

ExampleMetadata:

infused

Example:

boot_hook_conf = ec2.UserData.for_linux()
boot_hook_conf.add_commands("cloud-init-per once docker_options echo 'OPTIONS="${OPTIONS} --storage-opt dm.basesize=40G"' >> /etc/sysconfig/docker")

setup_commands = ec2.UserData.for_linux()
setup_commands.add_commands("sudo yum install awscli && echo Packages installed らと > /var/tmp/setup")

multipart_user_data = ec2.MultipartUserData()
# The docker has to be configured at early stage, so content type is overridden to boothook
multipart_user_data.add_part(ec2.MultipartBody.from_user_data(boot_hook_conf, "text/cloud-boothook; charset="us-ascii""))
# Execute the rest of setup
multipart_user_data.add_part(ec2.MultipartBody.from_user_data(setup_commands))

ec2.LaunchTemplate(self, "",
    user_data=multipart_user_data,
    block_devices=[]
)
Parameters:

parts_separator (Optional[str]) – The string used to separate parts in multipart user data archive (it’s like MIME boundary). This string should contain [a-zA-Z0-9()+,-./:=?] characters only, and should not be present in any part, or in text content of archive. Default: +AWS+CDK+User+Data+Separator==

Methods

add_commands(*commands)

Add one or more commands to the user data.

Parameters:

commands (str)

Return type:

None

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

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

add_part(part)

Adds a part to the list of parts.

Parameters:

part (MultipartBody)

Return type:

None

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

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

add_user_data_part(user_data, content_type=None, make_default=None)

Adds a multipart part based on a UserData object.

If makeDefault is true, then the UserData added by this method will also be the target of calls to the add*Command methods on this MultipartUserData object.

If makeDefault is false, then this is the same as calling:

# multi_part: ec2.MultipartUserData
# user_data: ec2.UserData
# content_type: str


multi_part.add_part(ec2.MultipartBody.from_user_data(user_data, content_type))

An undefined makeDefault defaults to either:

  • true if no default UserData has been set yet; or

  • false if there is no default UserData set.

Parameters:
  • user_data (UserData)

  • content_type (Optional[str])

  • make_default (Optional[bool])

Return type:

None

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()

Create a userdata object for Windows hosts.

Return type:

UserData