Skip to content

photostructure/cloud-init-templates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Cloud-Init Infrastructure Templates

Reusable, generic cloud-init configuration templates for common server infrastructure patterns. These templates are designed to be included in your own cloud-init configurations via #include directives.

πŸ—οΈ Architecture

These templates follow a composable pattern:

  • Generic base templates (this repo) - safe to open source
  • Your server configurations (private repo) - domain-specific customizations
# your-private-repo/servers/web-server.yaml
#cloud-config-archive

# Environment setup (runs first)
- type: text/cloud-config
  content: |
    write_files:
      - path: /etc/environment.d/90-infrastructure.conf
        content: |
          ADMIN_EMAIL="[email protected]"
          AWS_REGION="us-west-2"
        permissions: '0644'
        owner: root:root

# Include base templates
- type: text/x-include-url
  content: |
    #include
    https://raw.githubusercontent.com/your-org/cloud-init-templates/main/base/hardening.yaml
    https://raw.githubusercontent.com/your-org/cloud-init-templates/main/base/docker.yaml

# Your application-specific configuration
- type: text/cloud-config
  content: |
    # Your app config here...

πŸ“¦ Available Base Templates

Template Variants

Some templates come in multiple variants to support different use cases:

  • Node.js: Choose between nodejs-22.yaml (LTS) or nodejs-24.yaml (Latest) based on your application requirements
  • PostgreSQL: Single template postgres.yaml installs latest version from PostgreSQL's official repository

base/hardening.yaml

  • Purpose: Security hardening for Ubuntu servers
  • Features: UFW firewall, fail2ban, automatic security updates, SSH hardening
  • Environment Variables: None required
  • Use Case: Apply to all production servers
  • ⚠️ CAUTION: This configuration reboots the server at 2AM UTC time if there are updates that required a reboot. You may not want this.

base/docker.yaml

  • Purpose: Docker CE installation and configuration
  • Features: Latest Docker CE, docker-compose, systemd integration
  • Environment Variables: None required
  • Use Case: Container-based applications
  • ⚠️ CAUTION: You should look into more secure docker-like replacements, like podman.

base/email-alerts.yaml

  • Purpose: Critical system alerts via AWS SES
  • Features: Login notifications, disk/system monitoring, service failure alerts
  • Environment Variables: DEVOPS_EMAIL, ALERTS_EMAIL, AWS_REGION
  • Use Case: Production monitoring and incident response

base/nodejs-22.yaml & base/nodejs-24.yaml

  • Purpose: Node.js runtime installation
  • Features: Node.js 22.x or 24.x LTS via NodeSource repository
  • Environment Variables: None required
  • Use Case: Node.js applications

base/postgres.yaml

  • Purpose: PostgreSQL database installation
  • Features: Latest PostgreSQL from official PostgreSQL repository
  • Environment Variables: None required
  • Use Case: Database servers

πŸ”§ Environment Variable System

Templates use environment variables loaded from /etc/environment.d/90-infrastructure.conf for customization without hardcoded values.

⚠️ Important: Using Cloud-Config-Archive

You cannot mix #include with #cloud-config in the same user-data. Instead, use #cloud-config-archive to combine multiple formats.

Correct approach with cloud-config-archive:

#cloud-config-archive

# 1. Create environment files first
- type: text/cloud-config
  content: |
    write_files:
      - path: /etc/environment.d/90-infrastructure.conf
        content: |
          DEVOPS_EMAIL="[email protected]"
        permissions: '0644'
        owner: root:root

# 2. Include base templates
- type: text/x-include-url
  content: |
    #include
    https://raw.githubusercontent.com/your-org/cloud-init-templates/main/base/email-alerts.yaml

# 3. Additional configuration
- type: text/cloud-config
  content: |
    merge_how:
      - name: list
        settings: [append]
      - name: dict
        settings: [no_replace, recurse_list]
    packages: [...]

Why this approach works: Cloud-config-archive processes each section in order, ensuring environment files are created before included templates run.

Setting Environment Variables

Create the environment file in your server configuration:

# In your private server configs (using cloud-config-archive)
#cloud-config-archive

- type: text/cloud-config
  content: |
    write_files:
      - path: /etc/environment.d/90-infrastructure.conf
        content: |
          # Email Configuration
          DEVOPS_EMAIL="[email protected]"
          ALERTS_EMAIL="[email protected]"
          ADMIN_EMAIL="[email protected]"

          # Infrastructure
          AWS_REGION="us-west-2"
          BACKUP_RETENTION_DAYS="14"
        permissions: "0644"
        owner: root:root

Environment Variables

Templates use environment variables for customization. Common variables include DEVOPS_EMAIL, ALERTS_EMAIL, AWS_REGION, and SSH_PORT.

πŸ“‹ Complete reference: See ENVIRONMENT_VARIABLES.md for all available variables, their purposes, and defaults.

πŸš€ Quick Start

1. Fork this repository

# Create your own copy
gh repo fork your-org/cloud-init-templates

2. Create a server configuration

# servers/web-server.yaml
#cloud-config-archive

# Environment setup (runs first)
- type: text/cloud-config
  content: |
    write_files:
      - path: /etc/environment.d/90-infrastructure.conf
        content: |
          DEVOPS_EMAIL="[email protected]"
          ALERTS_EMAIL="[email protected]"
          AWS_REGION="us-west-2"
        permissions: '0644'
        owner: root:root

# Include base templates
- type: text/x-include-url
  content: |
    #include
    https://raw.githubusercontent.com/your-org/cloud-init-templates/main/base/hardening.yaml
    https://raw.githubusercontent.com/your-org/cloud-init-templates/main/base/docker.yaml

# Your application-specific config
- type: text/cloud-config
  content: |
    packages:
      - nginx
    runcmd:
      - systemctl enable nginx
      - systemctl start nginx

3. Deploy with cloud provider

Most cloud providers accept cloud-init data during instance creation. Point to your server configuration:

# Use raw GitHub URL for your server config
https://raw.githubusercontent.com/your-org/your-servers/main/servers/web-server.yaml

πŸ”’ Security Best Practices

Base Templates (This Repo)

  • βœ… No hardcoded secrets - All sensitive values use environment variables
  • βœ… Generic configurations - Domain-agnostic settings only
  • βœ… Sensible defaults - Safe fallback values provided
  • βœ… Open source friendly - Safe to publish publicly

Server Configurations (Your Repo)

  • πŸ” Keep private - Contains your domain-specific secrets
  • πŸ” Environment variables - Store sensitive values in env files
  • πŸ” Least privilege - Only expose what's necessary for deployment

πŸ“ Repository Structure

cloud-init-templates/
β”œβ”€β”€ README.md                   # This file
β”œβ”€β”€ ENVIRONMENT_VARIABLES.md    # Environment variable reference
β”œβ”€β”€ base/                       # Base templates
β”‚   β”œβ”€β”€ docker.yaml             # Docker installation
β”‚   β”œβ”€β”€ email-alerts.yaml       # AWS SES alerting
β”‚   β”œβ”€β”€ hardening.yaml          # Security hardening
β”‚   β”œβ”€β”€ nodejs-22.yaml          # Node.js 22.x runtime
β”‚   β”œβ”€β”€ nodejs-24.yaml          # Node.js 24.x runtime
β”‚   └── postgres.yaml           # PostgreSQL from official repo
β”œβ”€β”€ examples/                   # Example server configs
β”‚   β”œβ”€β”€ search-server.yaml      # Search server with Meilisearch
β”‚   └── web-server.yaml         # Web application server
└── tests/                      # Validation test suite
    β”œβ”€β”€ README.md               # Testing documentation
    β”œβ”€β”€ run-all-tests.sh        # Comprehensive test runner
    β”œβ”€β”€ test-email-alerts.sh    # Email alerting tests
    β”œβ”€β”€ test-hardening.sh       # Security hardening tests
    β”œβ”€β”€ test-security.sh        # General security tests
    └── utils.sh                # Shared testing utilities

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/new-template
  3. Follow the template standards:
    • Use environment variables for customization
    • Provide sensible defaults
    • Include comprehensive comments
    • Test with multiple distributions
  4. Submit a pull request

Template Standards

  • Environment Loading: All scripts must load env vars:
    [ -f /etc/environment.d/90-infrastructure.conf ] && . /etc/environment.d/90-infrastructure.conf
  • Sensible Defaults: Always provide fallback values:
    EMAIL="${ADMIN_EMAIL:-admin@example.com}"
  • Documentation: Clearly document required environment variables
  • Security: No hardcoded secrets or domain-specific information

πŸ“š Examples

See the examples/ directory for complete server configuration examples showing how to combine multiple base templates.

πŸ†˜ Support

πŸ“„ License

MIT License - see LICENSE file for details.


πŸ”§ Infrastructure as Code β€’ πŸ”’ Security First β€’ πŸš€ Production Ready

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published