INFRASTRUCTURE LEARNINGINFRASTRUCTURE LEARNING
インフラ大全インフラ大全IT INFRASTRUCTURE / LEARNING
Infrastructure learning guide
Find a lesson or lab/

Esc to close · Search terms stay on your device

LEARNING日本語

Topic lessonTopic: EC2: launch and operation

EC2 user data explained: first-boot execution, a safe example and logs

User data supplies launch-time configuration or instructions to EC2. The operating system’s initialization tooling interprets the supplied data. This lesson uses Amazon Linux 2023, cloud-init and a Bash script that creates one file, separating execution identity, first boot and outcome verification.

What you will learn

Explain who runs user data and when, then understand a small initialization example without secrets and verify it through logs and an output artifact.

Prerequisite

Know EC2 launch/connection and basic Linux files and permissions. The example assumes a fresh disposable AL2023 instance, not Windows or an arbitrary custom AMI. This site neither executes the code nor connects to AWS.

Separate the EC2 input from execution inside the OS

AL2023 example: cloud-init processes the supplied instructions
AL2023 example: cloud-init processes the supplied instructions1: User data — EC2 holds the input; OS tooling interprets it. — cloud-init. 2: cloud-init — Assumes first boot on AL2023; distinct from SSH login. — Run as root. 3: Run as root — Supplying the script alone does not prove success. — Logs and artifact123User datacloud-initRun as rootLogs and artifactAL2023 example: cloud-init processes the supplied instructions1: User data — EC2 holds the input; OS tooling interprets it. — cloud-init. 2: cloud-init — Assumes first boot on AL2023; distinct from SSH login. — Run as root. 3: Run as root — Supplying the script alone does not prove success. — Logs and artifact123User datacloud-initRun as rootLogs andartifact
  1. User data → cloud-initEC2 holds the input; OS tooling interprets it.
  2. cloud-init → Run as rootAssumes first boot on AL2023; distinct from SSH login.
  3. Run as root → Logs and artifactSupplying the script alone does not prove success.
User data
Configuration or a script supplied at launch.
cloud-init
OS-side initialization tooling.
Run as root
Runs this form of shell user-data script.
Logs and artifact
Check processing evidence and intended output.

A normal AL2023 shell user-data script runs as root during the initial boot cycle. Root has elevated OS privileges. Avoid interactive prompts and do not add sudo inside this script. It is not automatically an every-reboot task; frequency depends on initialization tooling and configuration.

Example: create a completion file without network access

Assume a fresh disposable AL2023 instance where /var/lib/infra-lesson is not used for anything else. The example creates that directory and status.txt with a fixed completion message. It installs no packages, starts no public server, calls no AWS API and reads no secrets. Do not paste it into an existing system without review.

For first boot on AL2023; review the directory and file being written
#!/bin/bash
set -eu
umask 027
install -d -m 0750 /var/lib/infra-lesson
printf '%s\n' 'initialization complete' > /var/lib/infra-lesson/status.txt
chmod 0640 /var/lib/infra-lesson/status.txt
printf '%s\n' 'lesson-bootstrap: complete'
Review what each part changes
PartMeaningBoundary in this example
#!/bin/bashInterpret the input as BashShell user data, not cloud-config or Windows syntax
set -euExpose undefined variables and failed commandsIt does not roll back earlier changes
umask / installSet creation permissions and a dedicated directoryRoot-owned exercise path; no conflict with other uses
printf / chmodWrite a fixed value and set mode 0640Overwrite status.txt rather than appending duplicates
Final printfEmit a completion marker to standard outputDo not log secrets or customer data

Check the input before launch and the result afterward

  1. Review the OS/AMI, cloud-init, Bash, output path and side effects; read the entire script first.
  2. For a real exercise, supply the plain shell example in User data under advanced launch settings. Let the normal console input handle Base64 rather than encoding twice.
  3. Allow initialization time, then inspect the instance through an approved connection method. Running state alone is not initialization success.
  4. Inspect /var/log/cloud-init-output.log for errors and the marker, then check the artifact’s content, owner and permissions.
  5. Reconcile retained data/resources and clean up after the exercise. Remove secrets and real-environment values before sharing logs.
Read-only inspection after connection; sudo reads the root-owned artifact
sudo cat /var/lib/infra-lesson/status.txt
sudo ls -ld /var/lib/infra-lesson
sudo ls -l /var/lib/infra-lesson/status.txt
sudo tail -n 40 /var/log/cloud-init-output.log

Choose an option to see the result and explanation.

EC2 is running. Does that prove the user-data file was created?
Read the answer and reason

Answer: No: inspect logs and the artifact · EC2 state and initialization outcome are separate. For this example, inspect the marker and the file’s content and permissions.

Worked example: editing and rebooting are not the same as running

When output is missing, establish facts before making more changes
ObservationWhat to check
User data was edited while stopped, but the file did not change after startNormal first-boot frequency: editing alone does not imply execution
The log shows a failure and no final markerThe failed command and artifacts/permissions created before it
The SSH ec2-user cannot read the fileRoot ownership, file mode 0640 and directory mode 0750 are intentional
You want to run the same script manuallyRecheck existing state, overwrite path, privileges and side effects; this is not necessarily equivalent to first boot

Idempotency means designing repeated identical input to preserve the intended state. This example overwrites a fixed value instead of appending duplicates. That does not make every initialization task safe to rerun, nor does it roll back earlier changes after a failure.

Choose an option to see the result and explanation.

With normal first-boot-only configuration, you edit user data and reboot. Can you conclude the revised script ran?
Read the answer and reason

Answer: No: verify execution conditions and logs · Changing the supplied value and executing it inside the OS are different events. Verify the OS/tooling configuration and execution record.

Keep secrets out; separate OS root from AWS permissions

Running as OS root does not grant unrestricted AWS API access. A task that calls AWS APIs needs appropriate IAM-role and instance-profile conditions. Hardcoding an access key in the script is not a substitute.

Check, then explain in your own words

  1. Explain the OS tooling and execution identity.
  2. Verify frequency, logs and the output artifact separately.
  3. Review rerun side effects and secret protection.
Check your understanding: Does supplying user data make the same task run on every AMI?

No. It depends on the OS, initialization tooling, input format and execution conditions.

This example is scoped to AL2023, cloud-init, Bash and a fresh exercise path. Reassess execution identity and side effects whenever those conditions change.

Sources and conditions checked

The official references used in this lesson are collected here with their scope, conditions and corresponding sections. Independent expert review and live AWS comparison have not been performed.