class CloudFormationInit
| Language | Type name | 
|---|---|
  .NET | Amazon.CDK.AWS.EC2.CloudFormationInit | 
  Go | github.com/aws/aws-cdk-go/awscdk/v2/awsec2#CloudFormationInit | 
  Java | software.amazon.awscdk.services.ec2.CloudFormationInit | 
  Python | aws_cdk.aws_ec2.CloudFormationInit | 
  TypeScript (source) | aws-cdk-lib » aws_ec2 » CloudFormationInit | 
A CloudFormation-init configuration.
Example
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!'),
  ),
});
Methods
| Name | Description | 
|---|---|
| add | Add a config with the given name to this CloudFormationInit object. | 
| add | Add a config set with the given name to this CloudFormationInit object. | 
| attach(attachedResource, attachOptions) | Attach the CloudFormation Init config to the given resource. | 
| static from | Use an existing InitConfig object as the default and only config. | 
| static from | Build a CloudFormationInit from config sets. | 
| static from | Build a new config from a set of Init Elements. | 
addConfig(configName, config) 
public addConfig(configName: string, config: InitConfig): void
Parameters
- configName 
string - config 
InitConfig  
Add a config with the given name to this CloudFormationInit object.
addConfigSet(configSetName, configNames?)  
public addConfigSet(configSetName: string, configNames?: string[]): void
Parameters
- 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, attachOptions)
public attach(attachedResource: CfnResource, attachOptions: AttachInitOptions): void
Parameters
- attachedResource 
CfnResource  - attachOptions 
AttachInit Options  
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::Initobject to the given resource's metadata, potentially adding aAWS::CloudFormation::Authenticationobject next to it if required. - Updates the instance role policy to be able to call the APIs required for
cfn-initandcfn-signalto work, and potentially add permissions to download referenced asset and bucket resources. - Updates the given UserData with commands to execute the 
cfn-initscript. 
static fromConfig(config) 
public static fromConfig(config: InitConfig): CloudFormationInit
Parameters
- config 
InitConfig  
Returns
Use an existing InitConfig object as the default and only config.
static fromConfigSets(props)  
public static fromConfigSets(props: ConfigSetProps): CloudFormationInit
Parameters
- props 
ConfigSet Props  
Returns
Build a CloudFormationInit from config sets.
static fromElements(...elements) 
public static fromElements(...elements: InitElement[]): CloudFormationInit
Parameters
- elements 
InitElement  
Returns
Build a new config from a set of Init Elements.

 .NET
 Go
 Java
 Python
 TypeScript (