Skip to content
v2.277.0

CloudFormationInit Class ​

A CloudFormation-init configuration.

Example ​

ts
declare const vpc: ec2.Vpc;
declare const instanceType: ec2.InstanceType;

new ec2.Instance(this, 'Instance', {
  vpc,
  instanceType,
  machineImage: ec2.MachineImage.latestAmazonLinux2023(),

  init: ec2.CloudFormationInit.fromElements(
    // Create a simple config file that runs a Python web server
    ec2.InitService.systemdConfigFile('simpleserver', {
      command: '/usr/bin/python3 -m http.server 8080',
      cwd: '/var/www/html',
    }),
    // Start the server using SystemD
    ec2.InitService.enable('simpleserver', {
      serviceManager: ec2.ServiceManager.SYSTEMD,
    }),
    // Drop an example file to show the web server working
    ec2.InitFile.fromString('/var/www/html/index.html', 'Hello! It\'s working!'),
  ),
});
python
# 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!"))
)
java
Vpc vpc;
InstanceType instanceType;


Instance.Builder.create(this, "Instance")
        .vpc(vpc)
        .instanceType(instanceType)
        .machineImage(MachineImage.latestAmazonLinux2023())

        .init(CloudFormationInit.fromElements(InitService.systemdConfigFile("simpleserver", SystemdConfigFileOptions.builder()
                .command("/usr/bin/python3 -m http.server 8080")
                .cwd("/var/www/html")
                .build()), InitService.enable("simpleserver", InitServiceOptions.builder()
                .serviceManager(ServiceManager.SYSTEMD)
                .build()), InitFile.fromString("/var/www/html/index.html", "Hello! It's working!")))
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
Vpc vpc;
InstanceType instanceType;


new Instance(this, "Instance", new InstanceProps {
    Vpc = vpc,
    InstanceType = instanceType,
    MachineImage = MachineImage.LatestAmazonLinux2023(),

    Init = CloudFormationInit.FromElements(InitService.SystemdConfigFile("simpleserver", new SystemdConfigFileOptions {
        Command = "/usr/bin/python3 -m http.server 8080",
        Cwd = "/var/www/html"
    }), InitService.Enable("simpleserver", new InitServiceOptions {
        ServiceManager = ServiceManager.SYSTEMD
    }), InitFile.FromString("/var/www/html/index.html", "Hello! It's working!"))
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var vpc Vpc
var instanceType InstanceType


ec2.NewInstance(this, jsii.String("Instance"), &InstanceProps{
	Vpc: Vpc,
	InstanceType: InstanceType,
	MachineImage: ec2.MachineImage_LatestAmazonLinux2023(),

	Init: ec2.CloudFormationInit_FromElements(ec2.InitService_SystemdConfigFile(jsii.String("simpleserver"), &SystemdConfigFileOptions{
		Command: jsii.String("/usr/bin/python3 -m http.server 8080"),
		Cwd: jsii.String("/var/www/html"),
	}), ec2.InitService_Enable(jsii.String("simpleserver"), &InitServiceOptions{
		ServiceManager: ec2.ServiceManager_SYSTEMD,
	}), ec2.InitFile_FromString(jsii.String("/var/www/html/index.html"), jsii.String("Hello! It's working!"))),
})

Methods ​

addConfig(configName: string, config: InitConfig): voiddef add_config(self, config_name: str, config: InitConfig) -> Nonepublic void addConfig(String configName, InitConfig config)public void AddConfig(string configName, InitConfig config)func AddConfig(ConfigName *string, Config InitConfig)#
Add a config with the given name to this CloudFormationInit object.
addConfigSet(configSetName: string, configNames?: string[]): voiddef add_config_set(self, config_set_name: str, config_names: typing.Optional[typing.List[str]] = None) -> Nonepublic void addConfigSet(String configSetName, List<String> configNames)public void AddConfigSet(string configSetName, string[]? configNames = null)func AddConfigSet(ConfigSetName *string, ConfigNames *[]*string)#
Add a config set with the given name to this CloudFormationInit object.

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

attach(attachedResource: CfnResource, attachOptions: AttachInitOptions): voiddef attach(self, attached_resource: CfnResource, attach_options: AttachInitOptions) -> Nonepublic void attach(CfnResource attachedResource, AttachInitOptions attachOptions)public void Attach(CfnResource attachedResource, AttachInitOptions attachOptions)func Attach(AttachedResource CfnResource, AttachOptions AttachInitOptions)#
Attach the CloudFormation Init config to the given resource.
notes

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.
fromConfig(config: InitConfig): CloudFormationInit // chainable@staticmethod def from_config(config: InitConfig) -> CloudFormationInitpublic static CloudFormationInit fromConfig(InitConfig config)public static CloudFormationInit FromConfig(InitConfig config)func FromConfig(Config InitConfig) CloudFormationInitstatic#
Use an existing InitConfig object as the default and only config.
fromConfigSets(props: ConfigSetProps): CloudFormationInit // chainable@staticmethod def from_config_sets(props: ConfigSetProps) -> CloudFormationInitpublic static CloudFormationInit fromConfigSets(ConfigSetProps props)public static CloudFormationInit FromConfigSets(ConfigSetProps props)func FromConfigSets(Props ConfigSetProps) CloudFormationInitstatic#
Build a CloudFormationInit from config sets.
fromElements(...elements: InitElement[]): CloudFormationInit // chainable@staticmethod def from_elements(elements: InitElement) -> CloudFormationInitpublic static CloudFormationInit fromElements(InitElement elements)public static CloudFormationInit FromElements(InitElement elements)func FromElements(Elements InitElement) CloudFormationInitstatic#
Build a new config from a set of Init Elements.