Skip to content

ec2-macos-init partially overwriting the userdata file #7

@agile-kass

Description

@agile-kass

I was experimenting with the capabilities of the Execute-User-Data module, and I did not want to wait for the dedicated host to be wiped between invocations.

  • I created a mac ec2 instance with userdata in the aws web console.
  • I edited the contents of /usr/local/aws/ec2-macos-init/instances/i-0a9977235e17ff7f1/userdata
  • I changed the contents of /usr/local/aws/ec2-macos-init/init.toml to only run the Execute-User-Data module
  • I ran sudo ec2-macos-init run

What I expected:

I expected ec2-macos-init to either run my newly edited userdata or to run the original userdata.

What happened:

ec2-macos-init seems to have merged the original userdata with my edited userdata, and then ran the merged userdata.

Versions

EC2 macOS Init
Version: 1.5.0 [2021-07-22 11:34:15 -0700]
macOS version: 11.5.1

Details

I started with the below userdata, entered in the aws web console.

#!/bin/bash
set -x #echo on

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

date -u +"%Y-%m-%dT%H:%M:%SZ" >> /tmp/launch.txt
date -u +"%Y-%m-%dT%H:%M:%SZ" >> /Users/Shared/launch.txt
date -u +"%Y-%m-%dT%H:%M:%SZ" >> /Users/ec2-user/Desktop/launch.txt

This worked pretty much how I expected. /tmp/launch.txt and /Users/Shared/launch.txt both existed and had reasonable timestamps in them. I don't recall, but I think /Users/ec2-user/Desktop/launch.txt did not exist after the first run. (I suspect it ran before the Desktop directory was created. )

I wanted to try a few edits of userdata to see how they were executed. (see #6 ). I had edited /usr/local/aws/ec2-macos-init/init.toml because some of the initial tasks failed. I believe those failed because I had changed the password for ec2-user.

# Default EC2 macOS Init init.toml config for mac1.metal instances

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.


### Group 4 ###
## Finally, run user data.

# Attempt to execute userdata, if provided
[[Module]]
    Name = "ExecuteUserData"
    PriorityGroup = 4 # Fourth group
    RunPerInstance = true # Run once per instance
    FatalOnError = false # Best effort, don't fatal on error
    [Module.UserData]
        ExecuteUserData = true # Execute the userdata
  • I removed /usr/local/aws/ec2-macos-init/instances/i-0a9977235e17ff7f1/history.json.
  • I made userdata writeable sudo chmod 755 userdata
  • I edited the contents of userdata, then I made it read only and cated to verify my changes.
ec2-user@ip-172-31-26-0 i-0a9977235e17ff7f1 % sudo chmod 555 userdata
ec2-user@ip-172-31-26-0 i-0a9977235e17ff7f1 % ls -lah
total 8
drwxr-xr-x  3 root  wheel    96B Sep  2 22:30 .
drwxr-xr-x  3 root  wheel    96B Sep  2 18:58 ..
-r-xr-xr-x  1 root  wheel   338B Sep  2 22:27 userdata
ec2-user@ip-172-31-26-0 i-0a9977235e17ff7f1 % cat userdata
#!/bin/bash
set -x #echo on

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

date -u +"%Y-%m-%dT%H:%M:%SZ" >> /tmp/launch-4.txt
date -u +"%Y-%m-%dT%H:%M:%SZ" >> /Users/Shared/launch-4.txt
date -u +"%Y-%m-%dT%H:%M:%SZ" >> /Users/ec2-user/Desktop/launch-4.txt

echo "this line is entirely new"
touch /tmp/last-line
ec2-user@ip-172-31-26-0 i-0a9977235e17ff7f1 % sudo ec2-macos-init run
2021/09/02 22:31:45.227416 Fetching instance ID from IMDS...
2021/09/02 22:31:45.229604 Running on instance i-0a9977235e17ff7f1
2021/09/02 22:31:45.229625 Reading init config...
2021/09/02 22:31:45.229994 Successfully read init config
2021/09/02 22:31:45.230006 Validating config...
2021/09/02 22:31:45.230031 Successfully validated config
2021/09/02 22:31:45.230060 Prioritizing modules...
2021/09/02 22:31:45.230064 Successfully prioritized modules
2021/09/02 22:31:45.230084 Creating instance history directories for current instance...
2021/09/02 22:31:45.230121 Successfully created directories
2021/09/02 22:31:45.230142 Getting instance history...
2021/09/02 22:31:45.230279 Successfully gathered instance history
2021/09/02 22:31:45.230292 Processing priority level 1 (0 modules)...
2021/09/02 22:31:45.230298 Successfully completed processing of priority level 1
2021/09/02 22:31:45.230302 Processing priority level 2 (0 modules)...
2021/09/02 22:31:45.230306 Successfully completed processing of priority level 2
2021/09/02 22:31:45.230309 Processing priority level 3 (0 modules)...
2021/09/02 22:31:45.230313 Successfully completed processing of priority level 3
2021/09/02 22:31:45.230337 Processing priority level 4 (1 modules)...
2021/09/02 22:31:45.230369 Running module [ExecuteUserData] (type: userdata, group: 4)
2021/09/02 22:31:45.242850 Successfully completed module [ExecuteUserData] (type: userdata, group: 4) with message: successfully ran user data with stdout: [this line is entirely new
] and stderr: [+++ dirname /usr/local/aws/ec2-macos-init/instances/i-0a9977235e17ff7f1/userdata
++ cd /usr/local/aws/ec2-macos-init/instances/i-0a9977235e17ff7f1
++ pwd
+ DIR=/usr/local/aws/ec2-macos-init/instances/i-0a9977235e17ff7f1
+ date -u +%Y-%m-%dT%H:%M:%SZ
+ date -u +%Y-%m-%dT%H:%M:%SZ
+ date -u +%Y-%m-%dT%H:%M:%SZ
+ echo 'this line is entirely new'
+ touch /tmp/last-line
]
2021/09/02 22:31:45.242902 Successfully completed processing of priority level 4
2021/09/02 22:31:45.242927 Writing instance history for instance i-0a9977235e17ff7f1...
2021/09/02 22:31:45.243164 Successfully wrote instance history
2021/09/02 22:31:45.243177 EC2 macOS Init completed in 13.551741ms

Afterward, I looked at the contents of userdata, and was surprised to see it was a mix of my original and edited userdata. Note the date -u +"%Y-%m-%dT%H:%M:%SZ" >> /Users/ec2-user/Desktop/launch.txt-4.txt line, which is not in either the original or edited userdata.

#!/bin/bash
set -x #echo on

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

date -u +"%Y-%m-%dT%H:%M:%SZ" >> /tmp/launch.txt
date -u +"%Y-%m-%dT%H:%M:%SZ" >> /Users/Shared/launch.txt
date -u +"%Y-%m-%dT%H:%M:%SZ" >> /Users/ec2-user/Desktop/launch.txt-4.txt

echo "this line is entirely new"
touch /tmp/last-line

The combined contents were executed. /tmp/launch.txt, /Users/Shared/launch.txt, /Users/ec2-user/Desktop/launch.txt-4.txt and touch /tmp/last-line were all either created or updated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions