EC2 Instance Metadata & Userdata
- Instance metadata and user data can be used for Self Configuration allowing EC2 instances answer the question Who am I? What should I do?
- Instance metadata and user data can be accessed from within the instance itself
- Data is not protected by authentication or cryptographic methods. Anyone who can access the instance can view its metadata and should not be used to any store sensitive data, such as passwords, as user data.
- Both the metadata and user data are available from the IP address 169.254.169.254 and have the latest as well as previous versions available
- Metadata and User data can be retrieved using simple curl or GET command and these requests are not billed
Instance Metadata
- Instance metadata is data about the instance and allows you to get answers to the Who am I?
- is divided into two categories
- Instance metadata
- includes metadata about the instance such as instance id, AMI id, hostname, IP address, role, etc
- Can be accessed from http://169.254.169.254/latest/meta-data/
- Dynamic data
- is generated when the instances are launched such as instance identity documents, instance monitoring, etc
- Can be accessed from http://169.254.169.254/latest/dynamic/
- Instance metadata
- can be used for managing and configuring running instances
- allows access to user data that specified when launching the instance
Instance Metadata Access Methods
- Instance metadata can be accessed from a running instance using one of the following methods:
- Instance Metadata Service Version 2 (IMDSv2) – a session-oriented method
- Instance Metadata Service Version 1 (IMDSv1) – a request/response method
- By default, either IMDSv1 or IMDSv2, or both can be used.
- Instance metadata service distinguishes between IMDSv1 and IMDSv2 requests based on whether, for any given request, either the
PUT
orGET
headers, which are unique to IMDSv2, are present in that request. - Instance metadata service can be configured on each instance so that local code or users must use IMDSv2. When IMDSv2 is enforced, IMDSv1 no longer works.
IMDSv2
- IMDSv2 uses session-oriented requests.
- With session-oriented requests, a session token that defines the session duration is created, which can be a minimum of one second and a maximum of six hours.
- During the specified duration, the same session token can be used for subsequent requests.
- After the specified duration expires, a new session token to use for future requests must be created.
User Data
- User data can be used for bootstrapping (launching commands when the machine starts) EC2 instance and helps answer the What should I do?
- is supplied when launching a EC2 instance and executed at boot time
- can be in the form of parameters or user defined script executed when the instance is launched for e.g. perform software patch updates, load and update the application from an S3 bucket etc
- can be used to build more generic AMIs, which can then be configured at launch time dynamically
- can be retrieved from http://169.254.169.254/latest/user-data
- By default, user data scripts and cloud-init directives run only during the first boot cycle when an EC2 instance is launched.
- If you stop an instance, modify the user data, and start the instance, the new user data is not executed automatically.
- However, user data script and cloud-init directives can be configured with a mime multi-part file. A mime multi-part file allows the script to override how frequently user data is executed in the cloud-init package.
- is treated as opaque data and returned as is.
- is limited to 16 KB. This limit applies to the data in raw form, not base64-encoded form.
- must be base64-encoded before being submitted to the API. EC2 command line tools perform the base64 encoding. The data is decoded before being presented to the instance.
Cloud-Init & EC2Config
- Cloud-Init and EC2Config provides the ability to parse the user-data script on the instance and run the instructions
- Cloud-Init
- Amazon Linux AMI supports Cloud-Init, which is an open source application built by Canonical.
- is installed on Amazon Linux, Ubuntu and RHEL AMIs
- enables using the EC2 UserData parameter to specify actions to run on the instance at boot time
- User data is executed on first boot using Cloud-Init, if the user data begins with
#!
- EC2Config
- EC2Config is installed on Windows Server AMIs
- User data is executed on first boot using Cloud-Init (technically EC2Config parses the instructions) if the user data begins with
<script>
or<powershell>
- EC2Config service is started when the instance is booted. It performs tasks during initial instance startup (once) and each time you stop and start the instance.
- It can also perform tasks on demand. Some of these tasks are enabled automatically, while others must be enabled manually.
- uses settings files to control its operation
- service runs
Sysprep
, a Microsoft tool that enables creation of customized Windows AMI that can be reused. - When EC2Config calls Sysprep, it uses the settings files in EC2ConfigService\Settings to determine which operations to perform.
AWS Certification Exam Practice Questions
- Questions are collected from Internet and the answers are marked as per my knowledge and understanding (which might differ with yours).
- AWS services are updated everyday and both the answers and questions might be outdated soon, so research accordingly.
- AWS exam questions are not updated to keep up the pace with AWS updates, so even if the underlying feature has changed the question might not be updated
- Open to further feedback, discussion and correction.
- How can software determine the public and private IP addresses of the Amazon EC2 instance that it is running on?
- Query the local instance metadata
- Query the appropriate Amazon CloudWatch metric.
- Query the local instance userdata.
- Use ipconfig or ifconfig command.
- The base URI for all requests for instance metadata is ___________
- http://254.169.169.254/latest/
- http://169.169.254.254/latest/
- http://127.0.0.1/latest/
- http://169.254.169.254/latest/
- Which Amazon Elastic Compute Cloud feature can you query from within the instance to access instance properties?
- Instance user data
- Resource tags
- Instance metadata
- Amazon Machine Image
- You need to pass a custom script to new Amazon Linux instances created in your Auto Scaling group. Which feature allows you to accomplish this?
- User data
- EC2Config service
- IAM roles
- AWS Config
- By default, when an EBS volume is attached to a Windows instance, it may show up as any drive letter on the instance. You can change the settings of the _____ Service to set the drive letters of the EBS volumes per your specifications.
- EBSConfig Service
- AMIConfig Service
- EC2Config Service
- Ec2-AMIConfig Service
- How can software determine the public and private IP addresses of the Amazon EC2 instance that it is running on?
- Query the appropriate Amazon CloudWatch metric.
- Use ipconfig or ifconfig command.
- Query the local instance userdata.
- Query the local instance metadata.
References
AWS Documentation – EC2_Instance_Metadata_Userdata
Awesome!
Very Well explained
As per “If you stop an instance, modify the user data, and start the instance, the new user data is not executed automatically”. But in lifecycle blog it has been mentioned that when instance is at the stop state it’s user data can be modified. Then what is the use of that modification if that is not going to take any affect? also for Ec2config it has been mentioned ”
EC2Config service is started when the instance is booted. It performs tasks during initial instance startup (once) and each time you stop and start the instance.” – Could you please clarify?
There seems to be an enhancement with a workaround to execute the user data scripts on every restart/stop-start action now. Will check and update accordingly.