Amazon Elastic Compute Cloud (EC2)

Basics of EC2

EC2 User data

Amazon EC2 user data enables you to automate instance initialization by passing scripts or cloud-init directives that run when an instance first launches. This ensures your servers are pre-configured and ready to serve traffic immediately.

Common Initialization Tasks

Use user data to automate repetitive setup tasks:

TaskDescription
Download Remote FilesFetch and extract archives from S3 or HTTP URLs
Validate API HealthCheck service endpoints before application start
Install PackagesInstall OS packages or application dependencies
BootstrapConfigure the instance to its desired state

The image is a diagram illustrating the flow of EC2 user data, featuring icons representing a user, a computer, a document, and cloud computing elements. It shows a sequence of steps or components related to EC2 user data processing.

How EC2 User Data Works

  1. Provide Script
    You supply a shell script, cloud-init config, or other executable content as user data when launching the instance (via Console, CLI, SDK, or Terraform).

  2. Bootstrapping
    During the first boot, the EC2 service downloads and executes your user data. This can install software, configure services, or fetch application code.

  3. Instance Ready
    Once the script completes, your instance is fully configured and ready to handle workload.

Key Considerations

  • User data runs only once, during the initial launch of an instance.

    Warning

    Modifying user data after launch and then restarting or stopping the instance will not rerun the script.

  • Input must be Base64 encoded. The AWS Management Console handles encoding automatically; CLI or SDK users must encode manually.

    Note

    Raw user data is limited to 16 KB before Base64 encoding. Keep scripts concise to avoid boot-time delays.

  • When retrieved via instance metadata or the console, EC2 returns the decoded version of your user data.
  • EC2 treats user data as opaque; the operating system or cloud-init on the instance interprets the content.
  • Very large or complex scripts can increase boot time—consider breaking logic into smaller segments or leveraging configuration management.

Conclusion

By embedding initialization logic into EC2 user data, you automate environment provisioning and reduce manual setup errors. Your instances will launch fully configured, secure, and production-ready.

Watch Video

Watch video content

Practice Lab

Practice lab

Previous
Demo Create AMI from AWS Console