AWS::CloudFormation::Init Attributes - krdheeraj51/aws-labs GitHub Wiki

Packages

  • The packages attribute allows you to install software packages using various package managers like yum, apt, or msi.

Example:

packages:
  yum:
    httpd: []
    mysql: []
  apt:
    nginx: []
  msi:
    MySoftware: "https://example.com/software.msi"

Services

  • The services attribute allows you to manage services on your instance, such as starting, stopping, enabling, or disabling them.

Example:

services:
  sysvinit:
    httpd:
      enabled: true
      ensureRunning: true
    mysqld:
      enabled: true
      ensureRunning: true
  windows:
    MyService:
      enabled: true
      ensureRunning: true

Files

  • The files attribute allows you to create or modify files on your instance. You can specify the file content, permissions, owner, and group.

Example:

files:
  "/var/www/html/index.html":
    content: |
      <html>
        <body>
          <h1>Hello, World!</h1>
        </body>
      </html>
    mode: "000644"
    owner: "root"
    group: "root"
  "/etc/myconfig.conf":
    content: "config_value=123"
    mode: "000600"
    owner: "root"
    group: "root"

Groups

  • The groups attribute allows you to create Linux/Unix groups.

Example:

groups:
  mygroup:
    gid: 1001
  anothergroup:
    gid: 1002

Users

  • The users attribute allows you to create Linux/Unix users and assign them to groups.

Example:

users:
  myuser:
    groups:
      - mygroup
    homeDir: /home/myuser
    uid: 1001
  anotheruser:
    groups:
      - anothergroup
    homeDir: /home/anotheruser
    uid: 1002

Authentication

  • The AWS::CloudFormation::Authentication metadata is used to specify authentication credentials for accessing private resources, such as private S3 buckets or Git repositories.

Example:

Metadata:
  AWS::CloudFormation::Authentication:
    S3AccessCreds:
      type: "S3"
      buckets: ["my-private-bucket"]
      roleName: "my-iam-role"
    GitHubAccess:
      type: "basic"
      username: "my-github-username"
      password: "my-github-password"

Complete Example Here is a complete example that combines all these attributes:

Resources:
  MyInstance:
    Type: "AWS::EC2::Instance"
    Metadata:
      AWS::CloudFormation::Init:
        configSets:
          default: [ "install", "configure" ]
        install:
          packages:
            yum:
              httpd: []
              mysql: []
          groups:
            mygroup:
              gid: 1001
          users:
            myuser:
              groups:
                - mygroup
              homeDir: /home/myuser
              uid: 1001
        configure:
          files:
            "/var/www/html/index.html":
              content: |
                <html>
                  <body>
                    <h1>Hello, World!</h1>
                  </body>
                </html>
              mode: "000644"
              owner: "root"
              group: "root"
          services:
            sysvinit:
              httpd:
                enabled: true
                ensureRunning: true
    Properties:
      InstanceType: "t2.micro"
      ImageId: "ami-0abcdef1234567890"
      KeyName: "my-key-pair"
      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash
          /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource MyInstance --region ${AWS::Region}
          /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource MyInstance --region ${AWS::Region}

In this example:

  • The install config set installs the httpd and mysql packages, creates a group, and creates a user.
  • The configure config set writes an HTML file and ensures the httpd service is running.
  • The UserData property includes a script that runs cfn-init to initialize the instance and cfn-signal to signal the status to CloudFormation.
⚠️ **GitHub.com Fallback** ⚠️