Passing User Data to EC2 Instances via CloudFormation
Key Concepts:
- User data scripts are used to automate the initial setup of EC2 instances.
- CloudFormation allows passing user data scripts to EC2 instances within templates.
- The user data script must be encoded in Base64 when passed through CloudFormation.
- Logs of the user data script execution are stored in
/var/log/cloud-init-output.log.
Next Steps:
- Explore how to link the success of the user data script to the success of the CloudFormation stack creation.
Example CloudFormation User Data Section:
UserData:
Fn::Base64: |
#!/bin/bash
sudo dnf update -y
sudo dnf install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
echo "Hello World from user data." > /var/www/html/index.html
Important Notes:
- CloudFormation will report stack creation success even if the user data script fails.
- It is important to verify the user data script execution to ensure the EC2 instance is configured correctly.
- The next lecture will cover how to handle potential user data script failures and their impact on CloudFormation.