CloudFormationInit

class aws_cdk.aws_ec2.CloudFormationInit(*args: Any, **kwargs)

Bases: object

A CloudFormation-init configuration.

ExampleMetadata:

infused

Example:

# vpc: ec2.Vpc
# instance_type: ec2.InstanceType


ec2.Instance(self, "Instance",
    vpc=vpc,
    instance_type=instance_type,
    machine_image=ec2.MachineImage.latest_amazon_linux2023(),

    init=ec2.CloudFormationInit.from_elements(
        # Create a simple config file that runs a Python web server
        ec2.InitService.systemd_config_file("simpleserver",
            command="/usr/bin/python3 -m http.server 8080",
            cwd="/var/www/html"
        ),
        # Start the server using SystemD
        ec2.InitService.enable("simpleserver",
            service_manager=ec2.ServiceManager.SYSTEMD
        ),
        # Drop an example file to show the web server working
        ec2.InitFile.from_string("/var/www/html/index.html", "Hello! It's working!"))
)

Methods

add_config(config_name, config)

Add a config with the given name to this CloudFormationInit object.

Parameters:
Return type:

None

add_config_set(config_set_name, config_names=None)

Add a config set with the given name to this CloudFormationInit object.

The new configset will reference the given configs in the given order.

Parameters:
  • config_set_name (str) –

  • config_names (Optional[Sequence[str]]) –

Return type:

None

attach(attached_resource, *, instance_role, platform, user_data, config_sets=None, embed_fingerprint=None, ignore_failures=None, include_role=None, include_url=None, print_log=None, signal_resource=None)

Attach the CloudFormation Init config to the given resource.

As an app builder, use instance.applyCloudFormationInit() or autoScalingGroup.applyCloudFormationInit() to trigger this method.

This method does the following:

  • Renders the AWS::CloudFormation::Init object to the given resource’s metadata, potentially adding a AWS::CloudFormation::Authentication object next to it if required.

  • Updates the instance role policy to be able to call the APIs required for cfn-init and cfn-signal to work, and potentially add permissions to download referenced asset and bucket resources.

  • Updates the given UserData with commands to execute the cfn-init script.

Parameters:
  • attached_resource (CfnResource) –

  • instance_role (IRole) – Instance role of the consuming instance or fleet.

  • platform (OperatingSystemType) – OS Platform the init config will be used for.

  • user_data (UserData) – UserData to add commands to.

  • config_sets (Optional[Sequence[str]]) – ConfigSet to activate. Default: [‘default’]

  • embed_fingerprint (Optional[bool]) – Whether to embed a hash into the userData. If true (the default), a hash of the config will be embedded into the UserData, so that if the config changes, the UserData changes and the instance will be replaced. If false, no such hash will be embedded, and if the CloudFormation Init config changes nothing will happen to the running instance. Default: true

  • ignore_failures (Optional[bool]) – Don’t fail the instance creation when cfn-init fails. You can use this to prevent CloudFormation from rolling back when instances fail to start up, to help in debugging. Default: false

  • include_role (Optional[bool]) – Include –role argument when running cfn-init and cfn-signal commands. This will be the IAM instance profile attached to the EC2 instance Default: false

  • include_url (Optional[bool]) – Include –url argument when running cfn-init and cfn-signal commands. This will be the cloudformation endpoint in the deployed region e.g. https://cloudformation.us-east-1.amazonaws.com Default: false

  • print_log (Optional[bool]) – Print the results of running cfn-init to the Instance System Log. By default, the output of running cfn-init is written to a log file on the instance. Set this to true to print it to the System Log (visible from the EC2 Console), false to not print it. (Be aware that the system log is refreshed at certain points in time of the instance life cycle, and successful execution may not always show up). Default: true

  • signal_resource (Optional[CfnResource]) – When provided, signals this resource instead of the attached resource. You can use this to support signaling LaunchTemplate while attaching AutoScalingGroup Default: - if this property is undefined cfn-signal signals the attached resource

Return type:

None

Static Methods

classmethod from_config(config)

Use an existing InitConfig object as the default and only config.

Parameters:

config (InitConfig) –

Return type:

CloudFormationInit

classmethod from_config_sets(*, configs, config_sets)

Build a CloudFormationInit from config sets.

Parameters:
  • configs (Mapping[str, InitConfig]) – The sets of configs to pick from.

  • config_sets (Mapping[str, Sequence[str]]) – The definitions of each config set.

Return type:

CloudFormationInit

classmethod from_elements(*elements)

Build a new config from a set of Init Elements.

Parameters:

elements (InitElement) –

Return type:

CloudFormationInit